我想获得一个imageview的多个动画。就像我希望一个图像从屏幕顶部掉落,在该图像完成动画之前,下一个动画应该已经从屏幕顶部开始。我能够使图像从顶部掉落但是不能做到以上特征。现在当图像到达终点时,下一个动画开始。请帮助
答案 0 :(得分:0)
如果您知道第一个动画的时间,可以在其onAnimationStart
中使用以下内容在当前动画完成之前开始下一个动画:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//add here your animation to start
}
}, 3000);//change the timer in milliseconds
在这种情况下,您的动画将在3秒后开始。更改此值以符合您的需求。
答案 1 :(得分:0)
public class FliperViewExampleActivity extends Activity implements OnGestureListener {
/** Called when the activity is first created. */
private int imageID[] = {R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.ic_launcher};
private ViewFlipper viewFlipper = null;
private GestureDetector gestureDetector = null;
private Button btn_next,btn_pre;
private int n;
int i=1;
private Timer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_next = (Button)findViewById(R.id.btn_next);
btn_pre = (Button)findViewById(R.id.btn_pre);
timer = new Timer();;
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(i==4)
{
}
else
{
nextImage();
}
}
});
btn_pre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(i==1)
{
}
else
{
previousImage();
}
}
});
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
// gestureDetector Object is used to detect gesture events
gestureDetector = new GestureDetector(this);
n=imageID.length;
for (int i = 0; i < imageID.length; i++)
{
ImageView image = new ImageView(this);
image.setImageResource(imageID[i]);
image.setScaleType(ImageView.ScaleType.FIT_XY);
viewFlipper.addView(image, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
//pageSwitcher(3);
}
public void pageSwitcher(int seconds) {
// At this line a new Thread will be created
timer.schedule(new RemindTask(), 4000, seconds * 1000); // delay
// in
// milliseconds
}
// this is an inner class...
class RemindTask extends TimerTask {
@Override
public void run() {
// As the TimerTask run on a seprate thread from UI thread we have
// to call runOnUiThread to do work on UI thread.
runOnUiThread(new Runnable() {
public void run() {
if(i==n)
{
timer.cancel();
}
else
{
nextImage();
}
}
});
}
}
private void nextImage(){
System.out.println("VALUE OF I IS"+i);
System.out.println("VALUE OF N IS"+n);
if(i==1)
{
/* 1.RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(0); */
//start animation
/*2. AlphaAnimation blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(300); // duration - half a second
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(1); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.ZORDER_NORMAL);*/
Toast.makeText(FliperViewExampleActivity.this,"Test Image",Toast.LENGTH_SHORT).show();
ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.loadAnimator(FliperViewExampleActivity.this, R.anim.flipper);
anim.setTarget(this.viewFlipper);
anim.setDuration(1000);
anim.start();
/*3. TranslateAnimation _tAnim = new TranslateAnimation(0,0, -500, 0);
_tAnim.setInterpolator(new AnticipateOvershootInterpolator(5));
_tAnim.setDuration(1000);*/
/*4. Animation shake = AnimationUtils.loadAnimation(FliperViewExampleActivity.this,R.anim.wobble);
shake.reset();
shake.setFillAfter(true);
*/
/*5. int fadeInDuration = 1000; // Configure time values here
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new AccelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
*/
// this.viewFlipper.setInAnimation(_tAnim);
/*this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.zoomin)); */
// this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
// R.anim.zoomout));
this.viewFlipper.showNext();
}
else
{
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.viewFlipper.showNext();
}
i++;
// pageSwitcher(3);
}
private void previousImage(){
System.out.println("VALUE OF I IS"+i);
System.out.println("VALUE OF N IS"+n);
if(i==2)
{
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.zoomin));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.viewFlipper.showPrevious();
}
else
{
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.viewFlipper.showPrevious();
}
i--;
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if (e1.getX() - e2.getX() > 60)
{
nextImage();
return true;
}
else if (e1.getX() - e2.getX() < -60)
{
previousImage();
return true;
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
}