Android动画--属性动画

属性动画:可以操作控件的属性(控件一定有该属性的get、set方法才能使用属性动画)

 注意:属性动画会改变动画真实的位置(补间动画不会改变动画真实的位置)

 效果图:电脑有点卡,大致效果就是这样,下面就直接上代码吧


1.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="alpha"
            android:text="透明" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="rotate"
            android:text="旋转" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="translate"
            android:text="平移" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="scale"
            android:text="缩放" />

    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="@mipmap/a" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    <ImageView
        android:id="@+id/image_View"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="@mipmap/a" />
</LinearLayout>

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="tog"
        android:text="薛之谦" />
</LinearLayout>

2.java代码如下:

public class Activity_Property extends Activity {
    ImageView imageView, image_View;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);
        imageView = (ImageView) findViewById(R.id.imageView);
        image_View = (ImageView) findViewById(R.id.image_View);
    }

    /**
     * 透明度
     */
    public void alpha(View view) {
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.9f, 0.8f, .07f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f, 0.1f, 0.0f);
        oa.setDuration(2000);
        oa.start();
    }

    /**
     * 旋转
     * rotationX  相对于X轴旋转
     * rotationY  相对于Y轴旋转
     * rotation   绕平面旋转
     */
    public void rotate(View view) {
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 360f);
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(image_View, "rotationX", 0f, 360f);
        oa.setDuration(2000);
        oa1.setDuration(3000);
        oa.start();
        oa1.start();
    }

    /**
     * 平移
     */
    public void translate(View view) {
        /**
         * 参数一:target   目标
         * 参数二:property  属性 控件的属性 ,这里要填写动画具体移动的方向(translationX translationY)
         * 参数三:values... (可变参数)值,第一个值是初始位置,后面依次所有的值都是这个图片依次执行的路径
         */
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "translationX", 0, 100, 150);
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(image_View, "translationY", 0, 125, -50);
        oa.setDuration(2000);//设置时间 单位为ms
        oa1.setDuration(2000);
        oa.start();//开启动画
        oa1.start();
    }

    /**
     * 缩放
     */
    public void scale(View view) {
        ObjectAnimator oa = ObjectAnimator.ofFloat(image_View, "scaleX",1.0f,0.0f,1.0f);
        oa.setDuration(2000);
        oa.start();
    }
    /**动画集
     * 注意:属性动画集是AnimatorSet
     *      补间动画是AnimationSet
     *      它们的差别很小,所以在调用的时候要注意,不要调用错了*/
    public void tog(View view){
        AnimatorSet as=new AnimatorSet();
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 360f);//旋转
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(imageView, "translationX", 0, 100, 150);//平移
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.9f, 0.8f, .07f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f, 0.1f, 0.0f);//透明度
     //   as.playTogether(oa2,oa1,oa);//同时执行
     //   as.playSequentially(oa2,oa1,oa);//依次执行
        /**play() 执行
         * with() 和xx一起执行
         * after() 在XX之后执行*/
        as.play(oa).with(oa2).after(oa1);
        as.setTarget(imageView);
        as.setDuration(6000);
        as.start();
    }
}

ObjectAnimator:

•     参数一:指定这个动画要操作的是哪个控件

•     参数二:指定这个动画要操作这个控件的哪个属性

•     参数三:是可变长参数,指这个属性值是从哪变到哪。下面我们再来看一下如何实现旋转效果:

•     ObjectAnimatoranimator = ObjectAnimator.ofFloat(tv,"rotation",0,180,0); 

•     animator.setDuration(2000); //设置时间,单位为ms

•     animator.start();//开始动画




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值