定义一个布局文件
<?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: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.zpj.aaa.animation.Main2Activity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="false">
<Button
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="淡入淡出"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/transtale"
android:layout_toStartOf="@+id/transtale"
android:layout_marginRight="29dp"
android:layout_marginEnd="29dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:textSize="20dp"
android:id="@+id/scale"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动"
android:id="@+id/transtale"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/rotate"
android:layout_toStartOf="@+id/rotate"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:id="@+id/rotate"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout"
android:layout_marginRight="37dp"
android:layout_marginEnd="37dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="500sp"
android:orientation="vertical"
android:id="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/img"
android:src="@mipmap/ic_x"
android:layout_below="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="181dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
实现按钮的方法
public class Main2Activity extends AppCompatActivity {
private Button buttonrotate = null;
private Button buttonscale = null;
private Button buttonalpha = null;
private Button buttontranstale = null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
buttonrotate = (Button) findViewById(R.id.rotate);
buttonscale = (Button) findViewById(R.id.scale);
buttonalpha = (Button) findViewById(R.id.alpha);
buttontranstale = (Button) findViewById(R.id.transtale);
imageView = (ImageView) findViewById(R.id.img);
//alpha 淡入淡出效果
buttonalpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
AnimationSet animationSet = new AnimationSet(true);
// 参数的意思是从 有 到 无
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//设置动画延迟的时间
alphaAnimation.setDuration(3000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
});
//旋转效果
buttonrotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//参数 0,360 表示的是从0度旋转到360度
//X轴的旋转方式 位置 y轴的方式以及位置
RotateAnimation rotateAnimation = new RotateAnimation(
0,360,
Animation.RELATIVE_TO_PARENT,0f,
Animation.RELATIVE_TO_PARENT,0f
);
rotateAnimation.setDuration(3000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
});
//缩放
buttonscale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(
Animation.RELATIVE_TO_SELF ,0f,
Animation.RELATIVE_TO_SELF,0f);
scaleAnimation.setDuration(3000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
}
});
buttontranstale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet a = new AnimationSet(true);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
TranslateAnimation t = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -80.0f, Animation.RELATIVE_TO_PARENT, 300.0f);
t.setDuration(3000);
a.addAnimation(t);
imageView.setAnimation(a);
}
});
}
}
以上就是基本的使用方法,另外还有其他使用方法 ,这种是最容易维护的所以在这我只写出了这一种方式。