How to make such animation in android? [closed]

限于喜欢 提交于 2019-12-03 22:00:32

Suppose D is an imageview and its move from 0 to 200 in x direction. See below code.

public class DActivity extends Activity {
ImageView D;
int x=0,y=0;
int a=0;
int newx=0;
TranslateAnimation TA;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    D=(ImageView)findViewById(R.id.d);
    RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
    D.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            x=v.getLeft();
            y=v.getTop();
            Toast.makeText(getApplicationContext(), "X="+x+"y="+y, Toast.LENGTH_LONG).show();
            if(x==0){
                D.setEnabled(false);
                a=200;
                newx=200;
                Anim();
            }
            if(x==200){
                D.setEnabled(false);
                a=-200;
                newx=0;
                Anim();
            }
            return true;
        }
    });

}
public void Anim(){
    TranslateAnimation TAnimation=new TranslateAnimation(0, a, 0,0);
    TAnimation.setInterpolator(new LinearInterpolator());
    TAnimation.setDuration(5000);
    TAnimation.setFillAfter(false);
    TAnimation.setFillEnabled(true);
    TAnimation.setFillBefore(true);
    D.startAnimation(TAnimation);

    TAnimation.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation animation) { }

        public void onAnimationRepeat(Animation animation) {}

        public void onAnimationEnd(Animation animation) {               
            LayoutParams param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            param.setMargins(newx, y, 0, 0);
                D.setLayoutParams(param);
                D.setEnabled(true);
        }
    });
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!