Android的tween动画有四种(旋转,缩放,移动,淡入淡出),都是animotion的子类,AnimationSet也是animotion的子类。
- 要理解四种动画的参数的意思,自己修改体验下参数即可。本文不做解释,提供代码给各位直接拿去测试看效果吧。、
public class TweenAnimation extends AppCompatActivity implements View.OnClickListener{
private Button button,button2,button3,button4;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tween_animation);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
@Override
public void onClick(View view) {
AnimationSet set = new AnimationSet(true);
/**
* 动画执行后都会返回最初的形态
*相对物件的X坐标的开始位置的意思是:从物件左上角画坐标系,横线为Y坐标,竖线为X坐标,取值为0-1
*ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);的参数:
第一个参数fromX为动画起始时 X坐标上的伸缩尺寸 0.0表示收缩到没有
第二个参数toX为动画结束时 X坐标上的伸缩尺寸 1.0表示正常无伸缩
第三个参数fromY为动画起始时Y坐标上的伸缩尺寸 值小于1.0表示收缩
第四个参数toY为动画结束时Y坐标上的伸缩尺寸 值大于1.0表示放大
*AlphaAnimation的属性:1.0完全透明,0.0完全不透明
*TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)的参数:
理解了相对物件的X坐标的开始位置的意思,就知道了具体的用法了,只要修改下参数自己调试下,就明白了。
*/
switch (view.getId()){
case R.id.button:
RotateAnimation rotateAnimation = new RotateAnimation(0,200,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(500);
set.addAnimation(rotateAnimation);
imageView.startAnimation(set);
break;
case R.id.button2:
ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.5f,0,0.8f,
Animation.RELATIVE_TO_SELF,0.8f,
Animation.RELATIVE_TO_SELF,0.8f);
scaleAnimation.setDuration(500);
set.addAnimation(scaleAnimation);
imageView.startAnimation(set);
break;
case R.id.button3:
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0.5f);
alphaAnimation.setDuration(500);
set.addAnimation(alphaAnimation);
imageView.startAnimation(set);
break;
case R.id.button4:
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,1f);
translateAnimation.setDuration(500);
set.addAnimation(translateAnimation);
imageView.startAnimation(set);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_tween_animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.flw.myhello.TweenAnimation">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<Button
android:layout_weight="1"
android:text="旋转"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
<Button
android:layout_weight="1"
android:text="缩放"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2" />
<Button
android:layout_weight="1"
android:text="淡入淡出"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3" />
<Button
android:layout_weight="1"
android:text="移动"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4" />
</LinearLayout>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:srcCompat="@mipmap/ic_launcher"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:id="@+id/imageView" />
</RelativeLayout>