How to make such animation in android? [closed]

2020-06-30 03:56发布

问题:

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

}