TranslateAnimation 移动动画

实现两张图片的交替,像左边移动交替,像右边移动交替,图片左(右)出,右(左)进等。
public class Move extends AbstractAnimation {
	private float fromXDelta;
	private float toXDelta;
	private float fromYDelta;
	private float toYDelta;
	private ImageView view;
	private Context mContext;
	private boolean filpLeft;
	private boolean filpRight;
	//receive the Activity
	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	public boolean isFilpLeft() {
		return filpLeft;
	}

	public void setFilpLeft(boolean filpLeft) {
		this.filpLeft = filpLeft;
	}

	public boolean isFilpRight() {
		return filpRight;
	}

	public void setFilpRight(boolean filpRight) {
		this.filpRight = filpRight;
	}

	TranslateAnimation translateStart,translateEnd;
	public float getFromXDelta() {
		return fromXDelta;
	}

	public void setFromXDelta(float fromXDelta) {
		this.fromXDelta = fromXDelta;
	}

	public float getToXDelta() {
		return toXDelta;
	}

	public void setToXDelta(float toXDelta) {
		this.toXDelta = toXDelta;
	}

	public float getFromYDelta() {
		return fromYDelta;
	}

	public void setFromYDelta(float fromYDelta) {
		this.fromYDelta = fromYDelta;
	}

	public float getToYDelta() {
		return toYDelta;
	}

	public void setToYDelta(float toYDelta) {
		this.toYDelta = toYDelta;
	}

	@Override
	public void performAnimation(Handler handler) {
		 view = (ImageView)getTarget();
		 //isFilpLeft: when the UI add the String to move left isFilpLeft = true;
		 if(isFilpLeft()){
			 translateStart = new TranslateAnimation(fromXDelta,toXDelta,fromYDelta,toYDelta);
			 translateEnd = new TranslateAnimation(-toXDelta,fromXDelta,-toYDelta,fromYDelta);
			 setFilpLeft(false);
		 }else if(isFilpRight()){
			 translateStart = new TranslateAnimation(fromXDelta,-toXDelta,fromYDelta,-toYDelta);
			 translateEnd = new TranslateAnimation(toXDelta,fromXDelta,toYDelta,fromYDelta);
			 setFilpRight(false);
		 } else{
			 translateStart = new TranslateAnimation(fromXDelta,toXDelta,fromYDelta,toYDelta);
			 translateEnd = new TranslateAnimation(toXDelta,fromXDelta,toYDelta,fromYDelta);
		 }
		 translateStart.setDuration((long)(getDuration()*1000));
		 translateEnd.setDuration((long)(getDuration()*1000));
		 translateStart.setRepeatCount(getRepeat());
		 
		 
			 translateStart.setAnimationListener(new AnimationListener(){

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub	
					}
					@Override
					public void onAnimationEnd(Animation animation) {
						//change the image when the activity call.
						if(mContext != null){
						Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);
						view.setImageBitmap(bmp);
						view.startAnimation(translateEnd);
						}else{
							view.startAnimation(translateEnd);
						}
					}
					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub
					}
				 });
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				view.startAnimation(translateStart);
			}			
		}, (long)(getDelay()*1000));
	}
}

你可能感兴趣的:(UI)