Android自定义视图动画(一)

来源:互联网 发布:linux 格式化硬盘 编辑:程序博客网 时间:2024/06/05 18:48

一)旋转动画(Rotate Animation):以一个按钮视图为例进行旋转,点击按钮触发旋转,有两种方法可以实现,在代码中或在资源文件中,效果是一样的。

1)在代码中配置旋转参数

@Override        public void onClick(View v) {         RotateAnimation ra=new RotateAnimation(0, 360, 100, 50); //绕像素点为(100,50)的中心点360度旋转   //    RotateAnimation ra=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);  //以自身的百分之五十为中心旋转360度              ra.setDuration(2000);//旋转时间为2s                 v.startAnimation(ra);                  


2)在资源文件(xml)中配置旋转参数

新建xml文件,Resources Typed 选择Tween Animation,Root Element 选择rotate,命名为ra,注意这里的pivotX和pivotY的值如果设置为百分数,就意味着以自身的百分之多少为中心进行旋转;如果设置为整数值,代表以像素点为旋转中心

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

然后在代码中添加一行

v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra));


二)透明动画(Alpha Animation):以一个按钮视图为例,点击后显示透明动画效果

1)在代码中配置

                @Overridepublic void onClick(View v) {AlphaAnimation aa=new AlphaAnimation(0, 1);//透明值从0到1aa.setDuration(2000);//时间为2S    v.startAnimation(aa);


2)在xml文件里配置

新建xml文件,Resources Typed 选择Tween Animation,Root Element 选择alpha,命名为aa

<?xml version="1.0" encoding="utf-8"?><alpha  xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="0"    android:toAlpha="1"    android:duration="2000"></alpha>

然后在代码里添加一行

v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.aa));


三)移动动画(Translate Animation)

1)在代码中配置参数

@Overridepublic void onClick(View v) {         TranslateAnimation ta=new TranslateAnimation(0,200,0, 200);//相对于自己所在的位置往右和往下分别移动了200像素              ta.setDuration(2000);      v.startAnimation(ta);


2)在xml中配置参数

新建xml文件,Resources Typed 选择Tween Animation,Root Element 选择translate,命名为ta

<?xml version="1.0" encoding="utf-8"?><translate      xmlns:android="http://schemas.android.com/apk/res/android"     android:fromXDelta="0"     android:toXDelta="200"     android:fromYDelta="0"     android:toYDelta="200"     android:duration="2000">    </translate>

然后再代码中添加一句

v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.ta));


四)缩放动画(Scale Animation)

1)在代码中配置参数,参数里的1代表缩放之后回复到和原来一样大小

@Overridepublic void onClick(View v) {        ScaleAnimation sa=new ScaleAnimation(0, 1,0,1);//默认的从左上角开始缩放,值为1代表恢复到原状//ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1, 100, 50);//基于像素点(100,50)进行缩放//ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//基于自身的位置的0.5进行缩放        sa.setDuration(2000);v.startAnimation(sa);

2)在xml中配置

新建xml文件,Resources Typed 选择Tween Animation,Root Element 选择scale,命名为sa

<?xml version="1.0" encoding="utf-8"?><scale  xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXScale="0"    android:toXScale="1"    android:fromYScale="0"    android:toYScale="1"    android:pivotX="50%"    android:pivotY="50%"    android:duration="2000">    </scale>

然后再代码中添加一行

v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.sa));










0 0
原创粉丝点击