Android案例(属性动画)

1、实现效果
在这里插入图片描述

2、布局

donghua.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/hcr"
        android:layout_centerInParent="true"
        android:id="@+id/pineapple"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btn_one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="渐变"/>
        <Button
            android:id="@+id/btn_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转"/>
        <Button
            android:id="@+id/btn_three"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放"/>
        <Button
            android:id="@+id/btn_four"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="移动" />
    </LinearLayout>


</RelativeLayout>

3、anim
alpha_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>
</set>

rotate_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:duration="1000"/>
</set>


scale_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.5"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:duration="3000"/>
</set>


translate_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="100"
        android:toYDelta="0.0"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="4000"/>
</set>

4、java代码

package com.example.horse_run_application;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;

public class DonghuaActivity extends Activity implements View.OnClickListener{
    Button bt1,bt2,bt3,bt4;
    ImageView pineapple;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.donghua);//绑定布局

        bt1=(Button) findViewById(R.id.btn_one);           //绑定button
        bt2=(Button) findViewById(R.id.btn_two);
        bt3=(Button) findViewById(R.id.btn_three);
        bt4=(Button) findViewById(R.id.btn_four);

        pineapple=(ImageView)findViewById(R.id.pineapple);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //渐变动画
            case R.id.btn_one:
                Animation alpha= AnimationUtils.loadAnimation(this,R.anim.alpha_animation);
                pineapple.startAnimation(alpha);
                break;
            //旋转动画
            case R.id.btn_two:
                Animation rotate=AnimationUtils.loadAnimation(this,R.anim.rotate_animation);
                pineapple.startAnimation(rotate);
                break;
            //缩放动画
            case R.id.btn_three:
                Animation scale=AnimationUtils.loadAnimation(this,R.anim.scale_animation);
                pineapple.startAnimation(scale);
                break;
            //平移动画
            case R.id.btn_four:
                Animation translate=AnimationUtils.loadAnimation(this,R.anim.translate_animation);
                pineapple.startAnimation(translate);
                break;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值