How does TranslateAnimation work on Android?

前端 未结 2 1168
无人及你
无人及你 2021-02-04 00:12

I went through

TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

but am still confused about how T

2条回答
  •  春和景丽
    2021-02-04 01:16

    AFAIK,there would be relative connection between this.

    That is,if you want to translate a hidden textview from right of screen to left of screen,on click of a button,you actually need to translate it from 100% of X-direction(right side of screen) to 0% of X-direction(left side of screen).

    At this point,you don't need to change Y-direction at all.so that would be 0% for both the options.So finally,you will have:

    fromXDelta 100%

    toXDelta 0%

    fromYDelta 0%

    toYDelta 0%

    you can restrict view of the component by setting this percentages between 0 to 100,as per your requirement.

    Similarly,if you need to translate your component on Y-direction as well,then you need to change 0% to some other value.

    Hope,its clear to you now.

    EDIT :

    for your requirement,you need to override onclick of button-1,and there you can control button-2's visibility as well as translation.

    create animation file in anim folder in your res.

    translate_button.xml:

    
    
    
        
    
    

    now,in your activity file,

    ...
    
    // ll  is linear layout containing button_2
    //counter is used to manage visibility of button_2 on click of button_1,i.e.1st click-button_2 would be visible,on 2nd click on button_1,it would be invisible.
    
    //you can change behavior as per your need
    
    button_2.setVisibility(View.GONE);
    button_1.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            if(counter<1)
            {
                counter++;                  
                button_2.setVisibility(View.VISIBLE);
                Animation anim=AnimationUtils.loadAnimation(context, R.anim.translate_button);
                button_2.startAnimation(anim);
            }
            else
            {
                counter=0;
                button_2.setVisibility(View.GONE);
            }
        }
    });
    ll.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            if(counter==1)
            {
                counter=0;
                button_2.setVisibility(View.GONE);
            }
        }
    });
    
    ...
    

提交回复
热议问题