The Animation in android

简介: <p>The android provide two types of animation.Tween Animation is one,such as rotation,translation,scale,gradual change(gradient).</p> <p><br></p> <p>main types:</p> <p>Animation、AlphaAnimation、

The android provide two types of animation.Tween Animation is one,such as rotation,translation,scale,gradual change(gradient).


main types:

Animation、AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAniamtion、AnimationSet


1.AlphaAnimation

the first argument means the start transparency of animation,the second argument means the end transparency of the animation.


2.RotateAnimation

arguments:

fromDegrees: start angle of the animation

toDegrees:end angle of the animation


3.ScaleAnimation

arguments:

fromX:    start scale in X cdoordination;

toX:         end scale in X coordination;

fromY:    start scale in Y coordination;

toY:         end scale in Y coordination;

on the other hand,you can set the scale mode:pivotXType/pivotYType,scale animation relative to x,y's start position:pivotXvalue/pivotYValue ect.


4.TranslateAnimation

arguments:

fromXDelta:    start X coordination of animation

toXDelta:         end X coordination of animation

fromYDelta:    start Y coordination of animation

toYDelta:         end Y coordination of animation


the layout file's code as bellow:

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<Button 
android:id="@+id/button_scale" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="scale" 
android:layout_x="5dp" 
android:layout_y="383dp" /> 
<Button 
android:id="@+id/button_rotate" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="rotate" 
android:layout_x="158dp" 
android:layout_y="383dp" /> 
<Button 
android:id="@+id/button_alpha" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="alpha" 
android:layout_x="5dp" 
android:layout_y="331dp" /> 
<Button 
android:id="@+id/button_translate" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="translate" 
android:layout_x="160dp" 
android:layout_y="329dp" /> 
<Button 
android:id="@+id/button_alpha_translate" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="alpha_translate" 
android:layout_x="84dp" 
android:layout_y="265dp" /> 
<ImageView 
android:id="@+id/imageview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_x="105dp" 
android:layout_y="133dp" 
android:src="@drawable/ic_launcher" 
/> 
</AbsoluteLayout> 

the class code:

 
public class Animations_Activity extends Activity { 
private Button button1; 
private Button button2; 
private Button button3; 
private Button button4; 
private Button button5; 
private ImageView imageView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_animations_); 
button1=(Button)findViewById(R.id.button_alpha); 
button2=(Button)findViewById(R.id.button_rotate); 
button3=(Button)findViewById(R.id.button_scale); 
button4=(Button)findViewById(R.id.button_translate); 
button5=(Button)findViewById(R.id.button_alpha_translate); 
imageView=(ImageView)findViewById(R.id.imageview); 
button1.setOnClickListener(new MyButton()); 
button2.setOnClickListener(new MyButton()); 
button3.setOnClickListener(new MyButton()); 
button4.setOnClickListener(new MyButton()); 
button5.setOnClickListener(new MyButton()); 
} 
class MyButton implements OnClickListener{ 
@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
switch (arg0.getId()) { 
case R.id.button_alpha: 
Alpha(); 
break; 
case R.id.button_rotate: 
Rotata(); 
break; 
case R.id.button_scale: 
Scale(); 
break; 
case R.id.button_translate: 
Translate(); 
break; 
case R.id.button_alpha_translate: 
Alpha_Translate(); 
break; 
default: 
break; 
} 
} 

} 

/* 
* 1.创建一个AnimationSet对象,该对象存储的是动画的集合 
* 2.根据需要创建相应的Animation对象 
* 3.根据动画的需求,为Animation对象设置相应的数据(即执行效果) 
* 4.奖Animation对象添加到AnimationSet对象当中 
* 5.使用控件对象开始执行AnimationSet 
*/ 
public void Alpha() { 
AnimationSet animationSet=new AnimationSet(true); 
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0); 
alphaAnimation.setDuration(2000); 
animationSet.addAnimation(alphaAnimation); 
imageView.startAnimation(animationSet); 
} 
public void Rotata(){ 
AnimationSet animationSet=new AnimationSet(true); 
//后面的四个参数定义的是旋转的圆心位置 
RotateAnimation rotateAnimation=new RotateAnimation( 
0, 360, 
Animation.RELATIVE_TO_PARENT, 1f, 
Animation.RELATIVE_TO_PARENT, 0f); 
rotateAnimation.setDuration(2000); 
animationSet.addAnimation(rotateAnimation); 
imageView.startAnimation(animationSet); 
} 
public void Scale() { 
AnimationSet animationSet=new AnimationSet(true); 
ScaleAnimation scaleAnimation=new ScaleAnimation( 
1, 0.1f, 1, 0.1f, 
Animation.RELATIVE_TO_SELF, 0.5f, 
Animation.RELATIVE_TO_SELF, 0.5f); 
scaleAnimation.setDuration(2000); 
animationSet.addAnimation(scaleAnimation); 
imageView.startAnimation(scaleAnimation); 
} 
public void Translate() { 
AnimationSet animationSet=new AnimationSet(true); 
TranslateAnimation translateAnimation=new TranslateAnimation( 
Animation.RELATIVE_TO_SELF, 0f, //X轴的开始位置 
Animation.RELATIVE_TO_SELF, 0.5f, //X轴的结束位置 
Animation.RELATIVE_TO_SELF, 0f, //Y轴的开始位置 
Animation.RELATIVE_TO_SELF, 1.0f); //Y轴的结束位置 
translateAnimation.setDuration(2000); 
animationSet.addAnimation(translateAnimation); 

/* 
* 第一行的设置如果为true,则动画执行完之后效果定格在执行完之后的状态 
* 第二行的设置如果为false,则动画执行完之后效果定格在执行完之后的状态 
* 第三行设置的是一个long类型的值,是指动画延迟多少毫秒之后执行 
* 第四行定义的是动画重复几次执行 
*/ 
animationSet.setFillAfter(true); 
animationSet.setFillBefore(false); 
animationSet.setStartOffset(2000); 
animationSet.setRepeatCount(3); 

imageView.startAnimation(animationSet); 
} 
public void Alpha_Translate() { 
AnimationSet animationSet=new AnimationSet(true); 
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0); 
alphaAnimation.setDuration(2000); 
animationSet.addAnimation(alphaAnimation); 
TranslateAnimation translateAnimation=new TranslateAnimation( 
Animation.RELATIVE_TO_SELF, 0f, //X轴的开始位置 
Animation.RELATIVE_TO_SELF, 0.5f, //X轴的结束位置 
Animation.RELATIVE_TO_SELF, 0f, //Y轴的开始位置 
Animation.RELATIVE_TO_SELF, 1.0f); //Y轴的结束位置 
translateAnimation.setDuration(2000); 
animationSet.addAnimation(translateAnimation); 
imageView.startAnimation(animationSet); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.activity_animations_, menu); 
return true; 
} 
} 

if you also interest in linux and android embed system,please connection with us in QQ grounp:139761394


相关文章
|
5月前
|
Android开发 容器
android animation clipToPadding clipChildren
android animation clipToPadding clipChildren
|
XML Android开发 数据格式
|
XML API Android开发
Android 属性动画Property Animation(中)
ValueAnimator指定整形、浮点型或者颜色值作为动画值,在一定时间内平滑过渡。可以通过ofInt(),ofFloat(),或ofObject()来或得一个ValueAnimator
103 0
|
Android开发
Android Animation动画
Android Animation动画
152 0
Android Animation动画
|
XML Java API
Android 属性动画(Property Animation) 完全解析
Android 属性动画(Property Animation) 完全解析
Android 属性动画(Property Animation) 完全解析
|
Android开发
Animation插值器:解决Android Animation 循环执行的停顿问题
在Android开发中,有时候我们需要一个动画一直循环执行下去,常见的如laoding菊花一直旋转,这时候就需要使用Animation的repeat功能
798 0
|
API Android开发
【Android 属性动画】属性动画 Property Animation 与 视图动画 View Animation 区别
【Android 属性动画】属性动画 Property Animation 与 视图动画 View Animation 区别
136 0
|
XML Android开发 数据格式
Android Animation(动画)---基础二(LayoutAnimationController)
LayoutAnimationController动画效果,一次出现 一、布局文件使用 动画list_item_layout.xml 每个列表项动画list_item_alpha.
832 0
|
XML Android开发 数据格式
Android Animation(动画)---基础一
动画分类: 传统动画(帧动画(Frame Animation)/ 补间动画(Tweened Animation))。 属性动画(Attribute Animation) 帧动画 帧动画是将图片一张一张的连续播放,适当的速度,让人感觉是连续的动画。
903 0