直接上code:
Activity文件
AnimationsTest.java
package com.peggy.astest;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
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.Button;
import android.widget.ImageView;
public class AnimationsTest extends AppCompatActivity {
private Button btnRotate = null;
private Button btnAlpha = null;
private Button btnScale = null;
private Button btnTranslate = null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations_test);
btnRotate = (Button) findViewById(R.id.rotate);
btnScale = (Button) findViewById(R.id.scale);
btnAlpha = (Button) findViewById(R.id.alpha);
btnTranslate = (Button) findViewById(R.id.translate);
imageView = (ImageView) findViewById(R.id.image);
btnRotate.setOnClickListener(new RotateButtonListener());
btnAlpha.setOnClickListener(new AlphaButtonListener());
btnTranslate.setOnClickListener(new TranslateButtonListener());
btnScale.setOnClickListener(new ScaleButtonListener());
}
/*从不透明变成完全透明
*/
class AlphaButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
AnimationSet set = new AnimationSet(true);
// from 1 : in-transparent to 1 : transparent
AlphaAnimation animation = new AlphaAnimation(1,0);
animation.setDuration(2000);
set.addAnimation(animation);
imageView.startAnimation(set);
}
}
/*
* 旋转点为View的中心,旋转360度
*/
class RotateButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
AnimationSet set = new AnimationSet(true);
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
set.addAnimation(animation);
imageView.startAnimation(set);
}
}
/*
* x方向(水平方向),形变从原始大小变成0.1倍宽度的大小
* y方向(竖直方向),形变从原始大小变成0.1倍高度度的大小
*/
class ScaleButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
AnimationSet set = new AnimationSet(true);
ScaleAnimation animation = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
set.addAnimation(animation);
imageView.startAnimation(set);
}
}
/*
* x方向(水平方向),从相对于自身0倍宽度的地方,平移到,相对于自身0.5倍宽度的地方
* y方向(竖直方向),从相对于自身0倍高度的地方,平移到,相对于自身0.5倍高度的地方
*/
class TranslateButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
AnimationSet set = new AnimationSet(true);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
set.addAnimation(animation);
imageView.startAnimation(set);
}
}
}
layout文件
activity_animations_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_animations_test"
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.peggy.astest.AnimationsTest">
<LinearLayout
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rotate"/>
<Button
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scale"/>
<Button
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alpha"/>
<Button
android:id="@+id/translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/translate"/>
</LinearLayout>
<LinearLayout
android:layout_below="@id/l1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_gravity="center_horizontal"
android:src="@drawable/i5"
android:layout_width="300dp"
android:layout_height="300dp" />
</LinearLayout>
</RelativeLayout>
效果见下:
AlphaAnimation:
ScaleAimation:
RotateAnimation:
TranslateAnimation: