How to make such animation in android? [closed]

雨燕双飞 提交于 2019-12-09 14:00:20

问题


In My Android Application the view is like this image:

Now, Here D is small part which is any view or may ve button. I want is, If the User click on that "D" part, the view appear in the different color should be translate in to the Left to right way and Only the "D" part appear at the most right edge of the screen. And if user again click on that "D" part, the View is translate from Right to Left and seen like above image.

How it is possible to make?


回答1:


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);
        }
    });
}

}



来源:https://stackoverflow.com/questions/8291639/how-to-make-such-animation-in-android

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