1,acitivity_main.xm 里面内容也很简单,就是透明度,移位,缩放,旋转按钮,还有一个imageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.edu.annimotiontest.MainActivity">
<Button
android:id="@+id/bt_touming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度"/>
<Button
android:id="@+id/bt_xiwei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移位"/>
<Button
android:id="@+id/bt_xuanzhuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"/>
<Button
android:id="@+id/bt_suofang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
2,mainAcitivity.java 先初始化移位,缩放,旋转,透明度几个按钮,在为每个按钮添加点击事件
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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button bt_touming;
private Button bt_yiwei;
private Button bt_xuanzuan;
private Button bt_suofang;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
bt_yiwei = (Button) findViewById(R.id.bt_xiwei);
bt_xuanzuan = (Button) findViewById(R.id.bt_xuanzhuan);
bt_suofang = (Button) findViewById(R.id.bt_suofang);
bt_touming = (Button) findViewById(R.id.bt_touming);
/**
* 为缩放按钮加点击事件
*/
bt_suofang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScaleAnimation animation = new ScaleAnimation(0.0f, 4.0f, 0.0f, 4.0f, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT
, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT);
animation.setRepeatCount(3);
animation.setFillBefore(true);
animation.setDuration(4000);
img.startAnimation(animation);
}
});
/**
* 为旋转按钮添加点击事件
*/
bt_xuanzuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setRepeatCount(3);
animation.setFillBefore(true);
animation.setDuration(4000);
img.startAnimation(animation);
}
});
/**
* 为移位按钮添加点击事件
*/
bt_yiwei.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT
, 1.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 1.0f
);
animation.setRepeatCount(3);
animation.setFillBefore(true);
animation.setDuration(4000);
img.startAnimation(animation);
}
});
/**
* 为透明添加点击事件
*/
bt_touming.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setRepeatCount(3);
animation.setFillBefore(true);
animation.setDuration(4000);
img.startAnimation(animation);
}
});
}
}
3 本人学生,正在自学中,有错误请指正,