Android 值动画一(ObjectAnimator)

说明:
值动画分:平移动画,旋转动画,缩放动画,透明动画,颜色渐变动画
1)animator.setTarget(mIVHandle); 表示把动画添加到要实现动画的view上面
2) translationX 是view源码里的setTranslationX的translationX部分

———————————————————————
有需求者请加qq:136137465,非诚勿扰
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–

1.值动画Activity对象

public class PropertyAnimActivity extends Activity {
    private ImageView mIVHandle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_properties_anim);
        mIVHandle = (ImageView) findViewById(R.id.iv_hand);
    }
    //位移
    public void clickTraslate(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "translationX", 0, 200);
        animator.setDuration(3000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.start();
    }
    //缩放
    public void clickScale(View view){
        //mIVHandle.setPivotX(0.5f);
        //mIVHandle.setPivotY(0.5f);
        ViewHelper.setPivotX(mIVHandle, 0.5f);
        ViewHelper.setPivotY(mIVHandle, 0.5f);
        ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "scaleX", 0, 2);
        animator.setDuration(3000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.start();
    }
    //旋转
    public void clickRotate(View view){
        //ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "rotationX", 0, 360);
        ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "rotation", 0, 360);
        animator.setDuration(3000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.start();
    }
    //透明
    public void clickAlpha(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "alpha", 0, 1);
        animator.setDuration(3000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.start();
    }
    //集合
    public void clickSet(View view){
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mIVHandle, "alpha", 0, 1);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(mIVHandle, "rotation", 0, 360);
        ObjectAnimator scale = ObjectAnimator.ofFloat(mIVHandle, "scaleX", 0, 2);
        //1.按循序播放
        set.playSequentially(alpha, rotation, scale);
        //2.按先后播放
        set.play(alpha).after(rotation).before(scale);
        //3.同时播放
        set.playTogether(alpha, rotation, scale);
        set.setDuration(2000);
        set.start();
    }
    //color
    public void clickColor(View view){
        //setBackgroundColor
        ObjectAnimator animator = ObjectAnimator.ofObject(mIVHandle, "backgroundColor", new ArgbEvaluator(), Color.RED, Color.BLUE);
        animator.setDuration(3000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.start();
    }
    //xml
    public void clickXml(View view){
        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.translator);
        animator.setTarget(mIVHandle);
        animator.start();
        //AnimatorInflater.loadAnimator(this, R.animator.scale).setTarget(mIVHandle).start();
    }
}

2.布局文件activity_properties_anim.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickTraslate"
            android:text="位移" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickScale"
            android:text="缩放" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickRotate"
            android:text="旋转 " />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickAlpha"
            android:text="透明" />
    LinearLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickSet"
            android:text="集合" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickColor"
            android:text="color" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickXml"
            android:text="xml" />
    LinearLayout>

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

        <ImageView
            android:id="@+id/iv_hand"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" />
    RelativeLayout>

LinearLayout>

3.调用的平移动画xml文件translator.xml,在res文件夹下添加animator目录


<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:propertyName="translationX"
    android:repeatMode="reverse"
    android:valueFrom="0"
    android:valueTo="100"
    android:valueType="floatType" >

objectAnimator>

你可能感兴趣的:(android,动画)