Reputation: 121
I have a layout in which i am using 6 image views and all of these 6 image views flip or animate simultaneously when I press a button. Now i want two things to happen: 1)all these image views should flip on its own say after 2 seconds without the help of a button. 2)all the image views should not flip simultaneously but at different times. Lets say one image view flips after 1 sec ,some views flip after 2 seconds and some after 3 seconds and thus the image views would appear to be flipping on the screen at all times or as long as the screen is not changed.
how to achieve this.Please guide. Here's the code that i have written to flip the image view on button click.
MainActivity.java:
public class MainActivity extends ActionBarActivity implements OnClickListener, AnimationListener {
private Animation animation1;
private Animation animation2;
private boolean isBackOfCardShowing = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF0000")));
animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
animation1.setAnimationListener(this);
animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
animation2.setAnimationListener(this);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
v.setEnabled(false);
((ImageView)findViewById(R.id.imageView1)).clearAnimation();
((ImageView)findViewById(R.id.imageView1)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView1)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView2)).clearAnimation();
((ImageView)findViewById(R.id.imageView2)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView2)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView3)).clearAnimation();
((ImageView)findViewById(R.id.imageView3)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView3)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView4)).clearAnimation();
((ImageView)findViewById(R.id.imageView4)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView4)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView5)).clearAnimation();
((ImageView)findViewById(R.id.imageView5)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView5)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView6)).clearAnimation();
((ImageView)findViewById(R.id.imageView6)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView6)).startAnimation(animation1);
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation==animation1) {
if (isBackOfCardShowing) {
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView3)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView4)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView5)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView6)).setImageResource(R.drawable.back);
} else {
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.aboutus);
((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.privacy);
((ImageView)findViewById(R.id.imageView3)).setImageResource(R.drawable.conv_up);
((ImageView)findViewById(R.id.imageView4)).setImageResource(R.drawable.volunteer);
((ImageView)findViewById(R.id.imageView5)).setImageResource(R.drawable.share);
((ImageView)findViewById(R.id.imageView6)).setImageResource(R.drawable.rateus);
}
((ImageView)findViewById(R.id.imageView1)).clearAnimation();
((ImageView)findViewById(R.id.imageView1)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView1)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView2)).clearAnimation();
((ImageView)findViewById(R.id.imageView2)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView2)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView3)).clearAnimation();
((ImageView)findViewById(R.id.imageView3)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView3)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView4)).clearAnimation();
((ImageView)findViewById(R.id.imageView4)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView4)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView5)).clearAnimation();
((ImageView)findViewById(R.id.imageView5)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView5)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView6)).clearAnimation();
((ImageView)findViewById(R.id.imageView6)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView6)).startAnimation(animation2);
} else {
isBackOfCardShowing=!isBackOfCardShowing;
findViewById(R.id.button).setEnabled(true);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
Upvotes: 0
Views: 500
Reputation: 1504
public class MainActivity extends ActionBarActivity implements OnClickListener, AnimationListener {
private Animation animation1;
private Animation animation2;
int current = 0;
int[] imageViewIds ={R.id.imageView1,R.id.imageView2,R.id.imageVie3,R.id.imageView4,R.id.imageView5,R.id.imageView6};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF0000")));
animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
animation1.setAnimationListener(this);
animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
animation2.setAnimationListener(this);
playLoopingAnimations();
}
playLoopingAnimations();
{
((ImageView)findViewById(imageViewIds[current])).clearAnimation();
((ImageView)findViewById(imageViewIds[current])).setAnimation(animation1);
((ImageView)findViewById(imageViewIds[current])).startAnimation(animation1);
}
@Override
public void onAnimationEnd(Animation animation)
{
current++;
if(current==imageViewIds.lenghth())
current=0;
playLoopingAnimations();
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationStart(Animation animation)
{
}
}
I think this set-up can be modified a bit more to hit the nail on its head!
Upvotes: 1
Reputation: 1287
You can use class CountdownTimer
.
The first parameter is how many milliseconds the timer should run, the second is how many milliseconds is one Tick.
onTick
executes every time 1 Tick is completed and onFinish
executes when the countdown finishes.
I think you can use this creatively to achieve your result.
CountdownTimer timer = new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
//Do something every tick
}
public void onFinish() {
//Do something when timer finishes
}
}.start();
Upvotes: 0