Triggering android path vector animation after each other - java

I need to trigger animation drawables after each other "Queue"
like when one of the animations finish the other start to work
now all animations is working good but all start in the same time but when I tried to put AnimationListener to make them trigger each other
I got error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hesham.salahshow/com.example.hesham.salahshow.MainActivity}: java.lang.RuntimeException: Unknown animation name: objectAnimator
any one can help ?
MainActivity
public class MainActivity extends ActionBarActivity {
private ImageView vectorImage;
private ImageView vectorImage2;
private ImageView vectorImage3;
private ImageView vectorImage4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vectorImage = (ImageView) findViewById(R.id.imageid);
vectorImage2 = (ImageView) findViewById(R.id.imageid2);
vectorImage3 = (ImageView) findViewById(R.id.imageid3);
vectorImage4 = (ImageView) findViewById(R.id.imageid4);
mAnim = AnimationUtils.loadAnimation(this, R.anim.path);
mAnim.setAnimationListener((Animation.AnimationListener) this);
vectorImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vectorImage.startAnimation(mAnim);
((Animatable) vectorImage.getDrawable()).start();
// ((Animatable) vectorImage2.getDrawable()).start();
// ((Animatable) vectorImage3.getDrawable()).start();
// ((Animatable) vectorImage4.getDrawable()).start();
}});
}
}
Anim/path
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="trimPathEnd"
android:valueFrom="0"
android:valueTo="1"
android:duration="1000"
android:valueType="floatType"
android:interpolator="#android:interpolator/linear">
</objectAnimator>
Thanks in advance :)

What is your minimum SDK version? ObjectAnimator was introduced in API 11.
Also, to put animations in sequence, you could override AnimationListener.onAnimationEnd() and use that to check which animation just ended and start the next one.

I have used another way which worked good for me
public class MainActivity extends ActionBarActivity {
private ImageView vectorImage;
private ImageView vectorImage2;
private ImageView vectorImage3;
private ImageView notiImageView;
private ImageButton next;
private int loopaya = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vectorImage = (ImageView) findViewById(R.id.imageid);
vectorImage2 = (ImageView) findViewById(R.id.imageid2);
vectorImage3 = (ImageView) findViewById(R.id.imageid3);
notiImageView = (ImageView) findViewById(R.id.notification);
next = (ImageButton) findViewById(R.id.next);
notiImageView.setVisibility(View.INVISIBLE);
vectorImage2.setVisibility(View.INVISIBLE);
vectorImage.setVisibility(View.INVISIBLE);
vectorImage3.setVisibility(View.INVISIBLE);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runThread();
}
});
}
private void runThread() {
final Handler h = new Handler();
final int delay = 1001; //milliseconds
h.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (loopaya == 1) {
vectorImage.setVisibility(View.VISIBLE);
((Animatable) vectorImage.getDrawable()).start();
loopaya++;
h.postDelayed(this, delay);
} else if (loopaya == 2) {
vectorImage2.setVisibility(View.VISIBLE);
((Animatable) vectorImage2.getDrawable()).start();
loopaya++;
h.postDelayed(this, delay);
} else if (loopaya == 3) {
vectorImage3.setVisibility(View.VISIBLE);
((Animatable) vectorImage3.getDrawable()).start();
loopaya++;
// h.postDelayed(this, delay);
notiImageView.setVisibility(View.VISIBLE);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadein);
notiImageView.startAnimation(myFadeInAnimation);
}
}
});
h.postDelayed(this, delay);
}
}, delay);
}

Related

Media Player android

I'm struggling on designing a little audio player.
Just a list of 4 songs - each has it's own textView and when a user clicks on a textView a song starts playing but i have no idea how to make this. I though about making a onClick method for each textView but really dont like this idea.
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
/* MediaPlayer hell = MediaPlayer.create(this, R.raw.Hell);
MediaPlayer bells = MediaPlayer.create(this, R.raw.HellsBells);
MediaPlayer shoot = MediaPlayer.create(this, R.raw.Shoot);
MediaPlayer thunder = MediaPlayer.create(this, R.raw.Thunderstruck);*/
public void playMusic (View view) {
varMediaPlayer = MediaPlayer.create(this, R.raw.Hell);
varMediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the following variables:
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
will make your app crash as your assigning the variables when the MainActivity object is created.
move those to onCreate(Bundle) and assign a click listener to each of those TextViews
private static final int RAW_MUSIC_1 = R.raw.Hell;
private static final int RAW_MUSIC_2 = R.raw.HellsBells;
private static final int RAW_MUSIC_3 = R.raw.Shoot;
private static final int RAW_MUSIC_4 = R.raw.Thunderstruck;
private MediaPlayer varMediaPlayer;
private TextView song1, song2, song3, song4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign after setContentView(...) as findViewById scan the content view layout
song1 = (TextView) findViewById(R.id.song1);
song2 = (TextView) findViewById(R.id.song2);
song3 = (TextView) findViewById(R.id.song3);
song4 = (TextView) findViewById(R.id.song4);
// add click listeners
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_1);
}
};
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_2);
}
};
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_3);
}
};
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_4);
}
};
}
and change playMusic(View) method to playMusic(int) and add release and reset methods to release object from memory
private void playMusic(final int musicId) {
try {
if (mediaPlayer.isPlaying()) mediaPlayer.reset();
} catch (final Exception e) {}
try {
mediaPlayer.release();
} catch (final Exception e) {}
mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
}
Replace your code with this one and try running this...
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
MediaPlayer mediaPlayer = new MediaPlayer();
public void playMusic (Integer id) {
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(this , id);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.hell);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.HellsBells);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Shoot);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Thunderstruck);
}
});
}
}

app crashed when I launch another activity after a countdown of 3 sec

This is my MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main2);
CountDownTimer countDownTimer = new CountDownTimer (3000,1000) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
startActivity (new Intent (MainActivity.this, Main2Activity.class));
}
}.start ();
} }
Here is my Main2Activity.java
public class Main2Activity extends AppCompatActivity {
public static Button button;
public static TextView temp;
public static TextView cityname;
public static TextView time;
public static EditText written;
public static ImageView imageView;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
button = (Button) findViewById (R.id.button);
temp = (TextView) findViewById (R.id.temp);
cityname = (TextView) findViewById (R.id.cityname);
time = (TextView) findViewById (R.id.time);
relativeLayout = (RelativeLayout) findViewById (R.id.relative);
written = (EditText) findViewById (R.id.editText);
imageView = (ImageView) findViewById (R.id.imageView);
button.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick(View view) {
try {
if(written.getText ().toString ().isEmpty () || written.getText ().toString ().equals ("")){
Toast.makeText (Main2Activity.this, "Enter your City name", Toast.LENGTH_LONG).show ();
}else {
time.setAlpha (0f);
temp.setAlpha (0f);
cityname.setAlpha (0f);
Parsing parsing = new Parsing ();
parsing.execute ();
written.setText("");
}
}catch (Exception e){
}
}
});
} }
This app crashed when I press the button, without anything the app crashes.

Android - Possible to update image in loop?

I have the following code but the image only updates after the loop has completed not during the loop like it would in normal java - moving the image across the screen my increasing the X location. Is there anyway to make the image update during the Loop???
Thanks Again/
public class MainActivity extends Activity {
Button button;
ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//image.setImageResource(R.drawable.android);
int X = 0;
int Y = 0;
//for(;;) {
for ( int i = 0; i < 50 ; i++) {
image.setX(i);
image.setY(Y);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.android3d);
}
}
});
}
}
Try this:
import android.os.Handler;
for ( int i = 0; i < 50 ; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
image.setX(i);
image.setY(Y);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.android3d);
}
}, 200);
}

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

Creating a slideshow with 5 images in a loop

I want to create a slideshow of images in which I want to repeat 5 images to come in a loop until a button is pressed. Also I want some effect in between the images so that the transition looks nice. I am able to create a slideshow, but the problem is that its not repeating.
here is my code:
public class Slides extends Activity implements OnClickListener {
Button button;
Boolean goingOn = false;
AnimationDrawable animation;
private TransitionDrawable trans;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discolight);
final ImageView lights = (ImageView) findViewById(R.id.img);
button=(Button)findViewById(R.id.power);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(goingOn)
{
stopAnimation();
goingOn=false;
}
else
{
startAnimation();
goingOn=true;
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu m) {
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.frontscreenmenu, m);
return true;
}
class Starter implements Runnable {
public void run() {
animation.start();
}
}
private void startAnimation() {
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.a), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.b), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.c), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.d), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.e), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.f), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.g), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.h), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.i), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.j), 1000);
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
600, 800);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setImageDrawable(animation);
imageView.post(new Starter());
}
private void stopAnimation() {
animation.stop();
}
Use The FrameAnimation for this
and add frameanimatin.setOneShot(false);

Categories