Android简单属性动画--平移

Android简单属性动画

效果图:

在这里插入图片描述
绿色那块图片(随便贴上去的…)原本在按钮的下方

xml代码(比较简单,不一一解释了)

      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/move"
            android:text="RightMove"/>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher_background"
            android:layout_below="@+id/move"
            android:id="@+id/picture"/>
    </RelativeLayout>

Java代码:

public class MainActivity extends AppCompatActivity {

    private Button move;
    private ImageView picture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        move=(Button)findViewById(R.id.move);
        picture=(ImageView)findViewById(R.id.picture);

        OnClick onClick=new OnClick();
        move.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.move:
                    RightMove(picture);
                    break;
            }
        }
    }

    public void RightMove(View v){

        //v.setTranslationX();//平移。当旋转时,下面的方法要对应的修改
        ObjectAnimator animator=ObjectAnimator.ofFloat(v,"translationX",0,v.getWidth());
        ObjectAnimator animator12=ObjectAnimator.ofFloat(v,"TranslationY",0,v.getHeight());
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.playTogether(animator,animator12);
        animatorSet.start();
    }
}

上面那一块也比较简单,也不解释了
说一下,解释下下面那个函数

  1. 首先有个ObjectAnimator
  2. 之后有“.”过后有很多静态方法,选择的方法与上图注释的 “v.setTranslation()”的方法一致,所以悬着ofFloat方法
  3. ofFloat方法参数解释:
    第一个参数是:选择你要操作的目标(图片);
    第二个参数是:你要选择的方式(我选择的是平移x轴);
    第三个参数是:起始位置(0表示自己当前的位置);
    第四个参数是:移动后的位置(我写的是移动到控件狂赌的位置)。
  4. 如果现在想看下效果可以直接.start(),即可看到向右平移的效果了
  5. 之后我们想要向左且向下平移,我们就得创建多个ObjectAnimator对象,之后一起放在AnimatorSet对象的playTogether方法里,对象.start即可
发布了49 篇原创文章 · 获赞 0 · 访问量 1438

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/103973546