安卓动画 移位,缩放,旋转,透明度简单实现

1,acitivity_main.xm 里面内容也很简单,就是透明度,移位,缩放,旋转按钮,还有一个imageView

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.edu.annimotiontest.MainActivity">

    <Button
        android:id="@+id/bt_touming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明度"/>
    <Button
        android:id="@+id/bt_xiwei"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="移位"/>
    <Button
        android:id="@+id/bt_xuanzhuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转"/>
    <Button
        android:id="@+id/bt_suofang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="缩放"/>

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>

2,mainAcitivity.java 先初始化移位,缩放,旋转,透明度几个按钮,在为每个按钮添加点击事件

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private Button bt_touming;
    private Button bt_yiwei;
    private Button bt_xuanzuan;
    private Button bt_suofang;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.img);
        bt_yiwei = (Button) findViewById(R.id.bt_xiwei);//移位
        bt_xuanzuan = (Button) findViewById(R.id.bt_xuanzhuan);//旋转
        bt_suofang = (Button) findViewById(R.id.bt_suofang);//缩放
        bt_touming = (Button) findViewById(R.id.bt_touming);//透明
        /**
         * 为缩放按钮加点击事件
         */
        bt_suofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScaleAnimation animation = new ScaleAnimation(0.0f, 4.0f, 0.0f, 4.0f, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT
                        , Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT);
                animation.setRepeatCount(3);//重复4次
                animation.setFillBefore(true);//恢复原来的状态
                animation.setDuration(4000);//设置动画时间
                img.startAnimation(animation);//启动动画
            }
        });
        /**
         * 为旋转按钮添加点击事件
         */
        bt_xuanzuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f);
                animation.setRepeatCount(3);//重复4次
                animation.setFillBefore(true);//恢复原来的状态
                animation.setDuration(4000);//设置动画时间
                img.startAnimation(animation);//启动动画
            }
        });
        /**
         * 为移位按钮添加点击事件
         */
        bt_yiwei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT
                        , 1.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 1.0f
                );
                animation.setRepeatCount(3);//重复4次
                animation.setFillBefore(true);//恢复原来的状态
                animation.setDuration(4000);//设置动画时间
                img.startAnimation(animation);//启动动画
            }
        });
            /**
            * 为透明添加点击事件
            */
        bt_touming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setRepeatCount(3);//重复4次
                animation.setFillBefore(true);//恢复原来的状态
                animation.setDuration(4000);//设置动画时间
                img.startAnimation(animation);//启动动画
            }
        });
    }
}

3 本人学生,正在自学中,有错误请指正,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值