属性动画

package com.example.shuxingdonghua;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.shuxingdonghua.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mpy;
    private Button mxz;
    private Button mjb;
    private Button msf;
    private ImageView mimg;
    private Button myq;

    @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);
        mjb.setOnClickListener(this);
        mpy.setOnClickListener(this);
        msf.setOnClickListener(this);
        mxz.setOnClickListener(this);
        myq.setOnClickListener(this);
        mimg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.py:
                //图片平移出去弹不回来
//                ObjectAnimator translationX = ObjectAnimator.ofFloat(mimg, "translationX", 200f);
//                ObjectAnimator translationY = ObjectAnimator.ofFloat(mimg, "translationY", 200f);


                //图片平移出去弹回来
                //如果没有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.sf:
                //缩放
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(mimg, "scaleX", 1.0f, 0.0f, 2.0f, 1.0f);
                scaleX.setDuration(3000);
                scaleX.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.yq:
                //执行全部动画
                getquanbudonghua();
                break;
            case R.id.img:
                //图片的点击事件
                getquanbudonghua();
                Toast.makeText(this,"执行所有动画,并倒着执行一遍",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public void getquanbudonghua() {
        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();
        return ;
    }

}

<?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/qwe"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/xz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转" />

        <Button
            android:id="@+id/py"
            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/jb"
            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>

阅读更多
个人分类: 咨询项目
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭