Android动画之RotateAnimation

来源:互联网 发布:mac电脑卸载软件 编辑:程序博客网 时间:2024/05/22 14:22

1.旋转动画RotateAnimation

rotate_animation.xml

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

android:fromDegrees——开始的角度
android:toDegrees——结束的角度
android:pivotX——旋转轴点的x轴的坐标
android:pivotY——旋转轴点的y轴的坐标
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”即为加速减速插值器

2.在代码中加入动画效果

package com.zhoujian.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.LinearInterpolator;import android.view.animation.RotateAnimation;import android.widget.Button;import android.widget.ImageView;import butterknife.ButterKnife;import butterknife.InjectView;public class MainActivity extends Activity {    @InjectView(R.id.start)    Button mStart;    @InjectView(R.id.img)    ImageView mImg;    @InjectView(R.id.start_one)    Button mStartOne;    private Animation mAnimation;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.inject(this);        clickEvent();    }    private void clickEvent() {        mStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //mAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_animation);                //mImg.startAnimation(mAnimation);                //创建动画                AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);                //动画持续时间                alphaAnimation.setDuration(5000);                //动画停留在结束的位置                alphaAnimation.setFillAfter(true);                //开启动画                mImg.startAnimation(alphaAnimation);            }        });        mStartOne.setOnClickListener(new View.OnClickListener()        {            @Override            public void onClick(View view)            {                //利用资源文件加载动画                //mAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate_animation);                //mImg.startAnimation(mAnimation);                //在代码中使用动画                RotateAnimation animation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);                //不停顿                animation.setInterpolator(new LinearInterpolator());                //重复次数                animation.setRepeatCount(0);                animation.setFillAfter(true);                animation.setDuration(2000);                //开启动画                mImg.startAnimation(animation);            }        });    }}

3.运行效果截图

这里写图片描述

0 0
原创粉丝点击