属性动画,四种变化(平移,旋转,渐变,缩放,一起执行)

例:



MainActivity 主方法类:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mpy;//平移
    private Button mxz;//旋转
    private Button mjb;//渐变
    private Button msf;//缩放
    private Button myq;//一起执行

    private ImageView mimg;//图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        mpy = findViewById(R.id.py);//平移
        mxz = findViewById(R.id.xz);//旋转
        mjb = findViewById(R.id.jb);//渐变
        msf = findViewById(R.id.sf);//缩放
        myq = findViewById(R.id.yq);//一起执行

        mimg = findViewById(R.id.img);//图片

        //设置点击监听
        mpy.setOnClickListener(this);//平移
        mxz.setOnClickListener(this);//旋转
        mjb.setOnClickListener(this);//渐变
        msf.setOnClickListener(this);//缩放
        myq.setOnClickListener(this);//一起执行
    }

    @Override//点击事件
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.py:
                //图片平移出去还能弹回来
                //如果没有mimg.getTranslationX()则图片不会弹回来
                ObjectAnimator translationX = ObjectAnimator.ofFloat(mimg, "translationX",mimg.getTranslationX(), 200f,mimg.getTranslationX());
                ObjectAnimator translationY = ObjectAnimator.ofFloat(mimg, "translationY",mimg.getTranslationY(), 200f,mimg.getTranslationY());
                //动画时间
                translationX.setDuration(3000);//时间
                translationY.setDuration(3000);//时间
                //执行动画
                translationY.start();//开始执行
                translationX.start();//开始执行
                break;
            case R.id.xz:
                //旋转
                ObjectAnimator rotation = ObjectAnimator.ofFloat(mimg, "rotation", 0.0f, 360f);
                rotation.setDuration(3000);//时间
                rotation.start();//开始执行
                break;
            case R.id.jb:
                //渐变透明
                ObjectAnimator alpha = ObjectAnimator.ofFloat(mimg, "alpha", 0.0f, 1.0f);
                alpha.setDuration(3000);//时间
                alpha.start();//开始执行
                break;
            case R.id.sf:
                //缩放
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(mimg, "scaleX", 1.0f, 0.0f, 2.0f, 1.0f);
                scaleX.setDuration(3000);//时间
                scaleX.start();//开始执行
                break;
            case R.id.yq:
                //一起执行
                PropertyValuesHolder alpha1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
                PropertyValuesHolder scaleX1 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
                PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
                PropertyValuesHolder rotation1 = PropertyValuesHolder.ofFloat("rotation", 0.0f, 360f);
                PropertyValuesHolder translationX1 = PropertyValuesHolder.ofFloat("translationX",200f);
                PropertyValuesHolder translationY1 = PropertyValuesHolder.ofFloat("translationY",200f);
                PropertyValuesHolder translationX2 = PropertyValuesHolder.ofFloat("translationX",0f);
                PropertyValuesHolder translationY2 = PropertyValuesHolder.ofFloat("translationY",0f);
                ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX1, translationY1);
                ObjectAnimator objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX2, translationY2);
                //可以直接执行,不过不能拼接动画,这是组合动画
                //ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX1, translationY1).setDuration(3000).start();
                //实例化AnimatorSet
                AnimatorSet animatorSet = new AnimatorSet();
                //使用play方法把两个动画拼接起来
                animatorSet.play(objectAnimator1).after(objectAnimator);
                //时间
                animatorSet.setDuration(3000);
                //开始执行
                animatorSet.start();
                break;
        }
    }
}
activity_main布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:id="@+id/img"
        android:src="@drawable/pp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <Button
            android:id="@+id/py"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="平移" />
        <Button
            android:id="@+id/xz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转" />
        <Button
            android:id="@+id/jb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="渐变" />
        <Button
            android:id="@+id/sf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放" />
        <Button
            android:id="@+id/yq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一起执行" />
    </LinearLayout>
</RelativeLayout>

















猜你喜欢

转载自blog.csdn.net/jun_tong/article/details/80434341
今日推荐