Android 旋转动画

1、简介

在这里插入图片描述

2、代码结构

在这里插入图片描述

1)activity_main 文件 加载图片 和 按钮
2)rotate 是定义的属性动画
3)ManiActivity 文件是 功能 动画

3、Activity_mani.xml 文件

<?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="com.example.lum.myrotate.MainActivity">


    <ImageView
        android:id="@+id/img_one_id"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/timg"/>

    <ImageView
        android:id="@+id/img_two_id"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/test"/>
    <Button
        android:id="@+id/but_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转" />

</LinearLayout>

4、rotate.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
             android:fromDegrees="0"   android:toDegrees="+360"
             android:pivotX="50%"    android:pivotY="50%"
             android:duration="5000"/>

    <!--
        旋转和缩放都需要指定中心点,同样在取值时要指定是相对父控件还是相对自己
        50% 表示自己的中心
        fromDegrees : 动画开始时的角度
        toDegrees : 动画结束时旋转的角度 可以大于 360

    -->
</set>

5、MainActivity 文件

package com.example.lum.myrotate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "MainActivity: ";
    private ImageView imageViewOne,imageViewTwo;
    private Button button;

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

        imageViewOne = (ImageView) findViewById(R.id.img_one_id);
        imageViewTwo = (ImageView) findViewById(R.id.img_two_id);
        button = (Button) findViewById(R.id.but_id);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_id:
                Log.i(TAG,"开始旋转动画");
                rotateFromXml();
                rotateFromCode();
                break;
                default:
                    break;
        }
    }

    //通过代码进行旋转都规划设定
    private void rotateFromCode() {
        //创建AninationSet 对象
        AnimationSet animationSet = new AnimationSet(true);
        //创建 RotateAnimation 对象
        RotateAnimation rotateAnimation = new RotateAnimation(0f,+360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //设置动画持续
        rotateAnimation.setDuration(5000);
        //动画插入器
        rotateAnimation.setInterpolator(this,android.R.anim.decelerate_interpolator);
        //添加到 AnimationSet
        animationSet.addAnimation(rotateAnimation);
        imageViewTwo.startAnimation(animationSet);
    }


    //通过xml 加载动画属性
    private void rotateFromXml() {
        //定义Animation对象
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.rotate);
        imageViewOne.startAnimation(animation);
    }
}

文章参考:
《Android 典型技术模块开发详解》

本人郑重声明,本博客所编文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/84544275