Android 第三十四章 动画

一、补间动画

public class MainActivity extends AppCompatActivity {

    private Button btn_alpha, btn_scale, btn_rotate, btn_translate;
    private ImageView iv_Tween;
    private Animation animation = null;

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

    private void initView() {
        btn_alpha = findViewById(R.id.btn_alpha);
        btn_scale = findViewById(R.id.btn_scale);
        btn_rotate = findViewById(R.id.btn_rotate);
        btn_translate = findViewById(R.id.btn_translate);
        iv_Tween = findViewById(R.id.iv_Tween);
    }

    private void initClick() {
        btn_alpha.setOnClickListener(v -> {
//            //代码
//            animation = new AlphaAnimation(0.1f, 1f);
//            animation.setDuration(3000);
            //xml
            animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
            iv_Tween.startAnimation(animation);
        });

        btn_scale.setOnClickListener(v -> {
//            animation=new ScaleAnimation(0f,2f,0f,2f);
//            animation.setDuration(3000);
            animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
            iv_Tween.startAnimation(animation);
        });
        btn_rotate.setOnClickListener(v -> {
//            animation=new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0f,1,Animation.RELATIVE_TO_SELF);
//            animation.setDuration(3000);
            animation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
            iv_Tween.startAnimation(animation);
        });
        btn_translate.setOnClickListener(v -> {
//            animation= new TranslateAnimation(0f,360f,0f,0f);
//            animation.setDuration(3000);
            animation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
            iv_Tween.startAnimation(animation);
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:layout_margin="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:text="透明度"
            android:textSize="12sp"/>

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="缩放"
            android:textSize="12sp"/>

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="旋转"
            android:textSize="12sp"/>

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="移动"
            android:textSize="12sp"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ImageView
            android:id="@+id/iv_Tween"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_noodles" />
    </LinearLayout>

</LinearLayout>
<!--    在res下创建anim文件夹,创建透明度、旋转、缩放、移动等文件-->
    <!--anim_alpha.xml 透明度-->
    <alpha
        android:duration="1000"
        android:fromAlpha="0.1"
        android:toAlpha="1" />

    <!--anim_rotate.xml 旋转-->
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:repeatCount="0"
        android:repeatMode="restart"
        android:toDegrees="360" />

    <!--anim_scale.xml 缩放-->
    <scale
        android:duration="6000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="2"
        android:toYScale="2" />

    <!--anim_translate.xml 移动-->
    <translate
        android:duration="8000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="0" />

二、帧动画

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable = null;
    private Button btn_start, btn_stop;
    private ImageView iv_frame;

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

    private void initView() {
        btn_start = findViewById(R.id.btn_start);
        btn_stop = findViewById(R.id.btn_stop);
        iv_frame = findViewById(R.id.iv_frame);
    }

    private void initData() {
        iv_frame.setBackgroundResource(R.drawable.anim_frame);
        animationDrawable = (AnimationDrawable) iv_frame.getBackground();
    }

    private void initClick() {
        btn_start.setOnClickListener(v -> {
            if (!animationDrawable.isRunning()) {
                //一组动画是否只播放一次
                animationDrawable.setOneShot(false);
                animationDrawable.start();
            }
        });

        btn_stop.setOnClickListener(v -> {
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!animationDrawable.isRunning()) {
            animationDrawable.setOneShot(false);
            animationDrawable.start();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:layout_margin="10dp"
        android:gravity="center">

        <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始" />

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="停止" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ImageView
            android:id="@+id/iv_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <!--在res->drawable 文件夹下创建 anim_frame.xml-->
    <item
        android:drawable="@mipmap/ic_anim1"
        android:duration="500" />
    <item
        android:drawable="@mipmap/ic_anim2"
        android:duration="500" />
    <item
        android:drawable="@mipmap/ic_anim3"
        android:duration="500" />
    <item
        android:drawable="@mipmap/ic_anim4"
        android:duration="500" />

</animation-list>

三、属性动画

        objectAnimator=ObjectAnimator.ofFloat(iv_saber,"alpha",1f,0f,1f);
//        objectAnimator = ObjectAnimator.ofFloat(iv_saber, "rotation", 0f, 180f, 360f, 180f, 0f);
//        objectAnimator = ObjectAnimator.ofFloat(iv_saber, "scaleX", 0.5f, 4f, 2f, 1f);
//        objectAnimator = ObjectAnimator.ofFloat(iv_saber, "scaleY", 0f, 1.5f, 2f, 1f);
//        objectAnimator = ObjectAnimator.ofFloat(iv_saber, "translationY", 0f,300f, -300f, 0f);
//        objectAnimator = ObjectAnimator.ofFloat(iv_saber, "translationX", 0f,300f, -300f, 0f);
        objectAnimator.setDuration(3000);
        objectAnimator.start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值