博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Animation动画
阅读量:7242 次
发布时间:2019-06-29

本文共 8228 字,大约阅读时间需要 27 分钟。

  hot3.png

Android的animation由四种类型组成

XML中

  • alpha
  • 渐变透明度动画效果
  • scale
  • 渐变尺寸伸缩动画效果
  • translate
  • 画面转换位置移动动画效果
  • rotate
  • 画面转移旋转动画效果

JavaCode中

  • AlphaAnimation
  • 渐变透明度动画效果
  • ScaleAnimation
  • 渐变尺寸伸缩动画效果
  • TranslateAnimation
  • 画面转换位置移动动画效果
  • RotateAnimation
  • 画面转移旋转动画效果

Android动画模式

Animation主要有两种动画模式:

一种是tweened animation(渐变动画)

XML中      JavaCode
alpha      AlphaAnimation
scale      ScaleAnimation

一种是frame by frame(画面转换动画)

XML中          JavaCode
translate      TranslateAnimation
rotate         RotateAnimation

如何在XML文件中定义动画

① 打开Eclipse,新建Android工程
② 在res目录中新建anim文件夹
③ 在anim目录中新建一个myanim.xml(注意文件名小写)

④ 加入XML的动画代码

Android动画解析--XML

<alpha>

 <scale>

 <translate>

<rotate>

 如何使用XML中的动画效果

public static Animation loadAnimation (Context context, int id) //第一个参数Context为程序的上下文    //第二个参数id为动画XML文件的引用//例子:myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

 如何在Java代码中定义动画

//在代码中定义 动画实例对象private Animation myAnimation_Alpha;private Animation myAnimation_Scale;private Animation myAnimation_Translate;private Animation myAnimation_Rotate;    //根据各自的构造方法来初始化一个实例对象myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

Android动画解析--JavaCode

AlphaAnimation

① AlphaAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② AlphaAnimation类对象构造

AlphaAnimation(float fromAlpha, float toAlpha) //第一个参数fromAlpha为 动画开始时候透明度//第二个参数toAlpha为 动画结束时候透明度myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);//说明: //                0.0表示完全透明//                1.0表示完全不透明

③ 设置动画持续时间

myAnimation_Alpha.setDuration(5000);//设置时间持续时间为 5000毫秒

 ScaleAnimation

① ScaleAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② ScaleAnimation类对象构造

ScaleAnimation(float fromX, float toX, float fromY, float toY,           int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸    //第二个参数toX为动画结束时 X坐标上的伸缩尺寸     //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸    //第四个参数toY为动画结束时Y坐标上的伸缩尺寸  /*说明:                    以上四种属性值                        0.0表示收缩到没有                     1.0表示正常无伸缩                         值小于1.0表示收缩                      值大于1.0表示放大*///第五个参数pivotXType为动画在X轴相对于物件位置类型  //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置//第七个参数pivotXType为动画在Y轴相对于物件位置类型   //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

③ 设置动画持续时间

myAnimation_Scale.setDuration(700);//设置时间持续时间为 700毫秒

 TranslateAnimation

 ① TranslateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

 ② TranslateAnimation类对象构造

TranslateAnimation(float fromXDelta, float toXDelta,                        float fromYDelta, float toYDelta)  //第一个参数fromXDelta为动画起始时 X坐标上的移动位置     //第二个参数toXDelta为动画结束时 X坐标上的移动位置       //第三个参数fromYDelta为动画起始时Y坐标上的移动位置      //第四个参数toYDelta为动画结束时Y坐标上的移动位置

 ③ 设置动画持续时间

myAnimation_Translate.setDuration(2000);//设置时间持续时间为 2000毫秒

RotateAnimation

① RotateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② RotateAnimation类对象构造

RotateAnimation(float fromDegrees, float toDegrees,             int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)//第一个参数fromDegrees为动画起始时的旋转角度    //第二个参数toDegrees为动画旋转到的角度   //第三个参数pivotXType为动画在X轴相对于物件位置类型  //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置//第五个参数pivotXType为动画在Y轴相对于物件位置类型   //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

③ 设置动画持续时间

myAnimation_Rotate.setDuration(3000);//设置时间持续时间为 3000毫秒

如何使用Java代码中的动画效果

//使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画//效果public void startAnimation (Animation animation)
下面是例子:
新建工程 myFrameAnimation
在main.xml布局中添加view子类,调整一下,效果如下:
找几个动态图片,把它分成单个图。(主要是为了讲解/是用Frame Animation效果,AnimationDrawable)
修改mainActivity.java的代码
package zyf.my.frame.animation;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.Toast;public class myFrameAnimatino extends Activity implements Button.OnClickListener {    /** Called when the activity is first created. */    AnimationDrawable frameAnimation;    /*     * 声明AnimationDrawable 可绘制动画 对象frameAnimation     */    ImageView myImage;    /*     * 图片View ImageView     */    Button start,stop;    CheckBox Cycle;    boolean isChecked_cycle=true;    /*     * (non-Javadoc)     * @see android.app.Activity#onCreate(android.os.Bundle)     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                start=(Button) findViewById(R.id.Button_start);        stop=(Button) findViewById(R.id.Button_stop);        Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);        /*         * findViewById()从XML中获取 Button  CheckBox         */                myImage = (ImageView) findViewById(R.id.rocket_image);        /*         * findViewById()从XML中获取ImageView 对象myImage         */        myImage.setBackgroundResource(R.anim.myframeanimation);        /*         * ImageView.setBackgroundResource()设置 图片View的背景图片          * 这里是把帧动画 myframeanimation加到 图片View的背景中         */        frameAnimation = (AnimationDrawable) myImage.getBackground();        /*         * myImage.getBackground()获得背景的Drawable的对象,转换成AnimationDrawable         */                start.setOnClickListener(this);        stop.setOnClickListener(this);    }    /*     * (non-Javadoc)     * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)     */    @Override    public boolean onTouchEvent(MotionEvent event) {        frameAnimation.setOneShot(isChecked_cycle);        /*         * 添加触摸事件处理方法         */        // TODO Auto-generated method stub        if(event.getAction()==MotionEvent.ACTION_DOWN){            /*             * MotionEvent.getAction()获取事件动作             * MotionEvent.ACTION_DOWN 向下的手势动作             */        /*event.getAction() 返回正被执行的动作种类:         * 是     ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一个.         */            frameAnimation.start();            /*             * 启动帧动画效果             */            return true;        }        return super.onTouchEvent(event);    }    /*     * (non-Javadoc)     * @see android.view.View.OnClickListener#onClick(android.view.View)     */    @Override    public void onClick(View button) {        // TODO Auto-generated method stub        switch (button.getId()) {        case R.id.Button_start:{            if(Cycle.isChecked()){                Toast.makeText(this, "动画重复", Toast.LENGTH_LONG).show();                isChecked_cycle=false;            }else{                Toast.makeText(this, "不重复", Toast.LENGTH_LONG).show();                isChecked_cycle=true;            }            /*             * 复选按钮选中,  动画重复播放,  AnimationDrawable.setOneShot(false)             * 复选按钮未选中,动画不重复播放,AnimationDrawable.setOneShot(true)             */                        frameAnimation.setOneShot(isChecked_cycle);            /*             * 设置重复与否             */            frameAnimation.start();            /*             *启动帧动画效果             */                    }            break;        case R.id.Button_stop:{            if(frameAnimation.isRunning()){                /*                 * AnimationDrawable.isRunning(),判断帧动画是否在运行,true---运行中                 * 如果动画正在运行,可以停止                 */                frameAnimation.stop();                /*                 *停止帧动画效果                  */            }                    }            break;        default:            break;        }    }}

转载于:https://my.oschina.net/jerikc/blog/127041

你可能感兴趣的文章
PHP全角转半角
查看>>
GetMessage()与PeekMessage(),以及WM_PAINT消息相关
查看>>
谷歌阻止宏基搭载不兼容的Android设备
查看>>
MySql双机热备解决方案
查看>>
接受数据的三种方式
查看>>
在线参考
查看>>
关于MyEclipse工程部署不能实时同步到Tomcat问题的解决
查看>>
本地MySQL数据库要访问远程MySQL数据库的表中的数据的实现
查看>>
一次完整的HTTP请求所经历的7个步骤
查看>>
线性代数--矩阵的逆
查看>>
android 内存 转载
查看>>
javascript中encodeURI和decodeURI方法
查看>>
ubuntu中的tomcat使用apr模式
查看>>
在线广告清除助手
查看>>
java单态设计模式
查看>>
全局变量与超级全局变量
查看>>
style="visibility: hidden"和 style=“display:none区别
查看>>
去掉UCenter验证码的修改方法
查看>>
我的码云传送门
查看>>
CSS 文本
查看>>