Android:关于Animation的几种常见的动画
适当的添加一些动画效果,能够获得更好的用户体验,这次讲讲一些常见的动画~
如:透明动画,渐变动画等等。
先看一下运行截图:
附上代码,注释写在代码中:
MainActivity.java:
package com.vrinux.animotiondemo;
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.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imgObj;
private Animation animation;
private AnimationSet animationSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgObj = (ImageView) findViewById(R.id.imgid);
}
public void animationshow(View view) {
switch (view.getId()) {
case R.id.btn01id:
/**
* 透明动画: AlphaAnimation(float fromAlpha,//变化起始状态的Alpha值; float
* toAlpha)//变化结束状态的Alpha值;
*/
animation = new AlphaAnimation(0.0f, 1.0f);
// 设置动画时长;
animation.setDuration(3000);
// 为组件添加动画;
imgObj.setAnimation(animation);
break;
case R.id.btn02id:
/**
* 渐变动画: ScaleAnimation(float fromX, //变化起始状态x轴(即宽度)占控件的x轴(即宽度)的比例,
* 1.0f为控件的宽度,0.0f表示没有宽度, 大于1.0f标识控件宽度的相应倍数; float
* toX,//变化结束状态x轴(即宽度)占控件的x轴(即宽度)的比例; float fromY,
* //变化起始状态y轴(即高度)占控件的y轴(即高度)的比例,和fromX同理; float toY,
* //变化结束状态y轴(即高度)占控件的y轴(即高度)的比例; float pivotX,//变化的原点x轴坐标; float
* pivotY)//变化的原点y轴坐标;
*/
animation = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn03id:
/**
* 位移动画: TranslateAnimation(float fromXDelta, //位移起始x轴位置; float
* toXDelta, //位移结束x轴位置; float fromYDelta, //位移起始y轴位置; float
* toYDelta)//位移结束y轴位置;
*/
animation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn04id:
/**
* 旋转动画: RotateAnimation(float fromDegrees,//起始旋转度数; float
* toDegrees, //结束旋转度数; float pivotX, //旋转原点的x轴坐标; float
* pivotY)//旋转原点的y轴坐标;
*/
animation = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn05id:
/**
* AnimationSet(true)//动画集合(组合);顾名思义;
*/
AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
ScaleAnimation animation2 = new ScaleAnimation(0.5f, 2.0f, 0.5f,
2.0f, 50.0f, 50.0f);
TranslateAnimation animation3 = new TranslateAnimation(0.0f,
100.0f, 0.0f, 100.0f);
RotateAnimation animation4 = new RotateAnimation(0.0f, 360.0f,
50.0f, 50.0f);
animationSet = new AnimationSet(true);
animationSet.addAnimation(animation1);
animationSet.addAnimation(animation2);
animationSet.addAnimation(animation3);
animationSet.addAnimation(animation4);
animationSet.setDuration(4000);
imgObj.setAnimation(animationSet);
break;
}
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgid"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_weight="1"
android:src="@drawable/img" />
<Button
android:id="@+id/btn01id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="透明动画"
android:textSize="20sp" />
<Button
android:id="@+id/btn02id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="渐变动画"
android:textSize="20sp" />
<Button
android:id="@+id/btn03id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="位移动画"
android:textSize="20sp" />
<Button
android:id="@+id/btn04id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="旋转动画"
android:textSize="20sp" />
<Button
android:id="@+id/btn05id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="动画组合"
android:textSize="20sp" />
</LinearLayout>
图片资源:
img.jpg:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。