Android animation补间动画

来源:互联网 发布:seo的意思 编辑:程序博客网 时间:2024/05/21 17:53

MainActivity 

package cn.bgs.fouranimation;



import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.LinearInterpolator;
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 Activity implements OnClickListener {
private Button mBtn_shen,mBtn_jian,mBtn_wei,mBtn_xuan;
private ImageView mImg;
private TranslateAnimation translate;//位移
private RotateAnimation rotate;//旋转
private ScaleAnimation scale;//伸缩
private AlphaAnimation alpha;//渐变
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBtn_shen=(Button) findViewById(R.id.mBtn_shen);
mBtn_jian=(Button) findViewById(R.id.mBtn_jian);
mBtn_wei=(Button) findViewById(R.id.mBtn_wei);
mBtn_xuan=(Button) findViewById(R.id.mBtn_xuan);
mImg=(ImageView) findViewById(R.id.mImg);


mBtn_shen.setOnClickListener(this);
mBtn_jian.setOnClickListener(this);
mBtn_wei.setOnClickListener(this);
mBtn_xuan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int ID =v.getId();
switch (ID) {
case R.id.mBtn_shen:
scale=new ScaleAnimation(1f, 5f, 1f, 5f);
scale.setDuration(3000);
scale.setInterpolator(new LinearInterpolator());
scale.setRepeatCount(6);
mImg.startAnimation(scale);
break;
case R.id.mBtn_jian:
alpha=new AlphaAnimation(0f, 1.0f);
alpha.setDuration(3000);
alpha.setInterpolator(new LinearInterpolator());
alpha.setRepeatCount(5);
mImg.startAnimation(alpha);
break;
case R.id.mBtn_wei:
translate=new TranslateAnimation(0, 100, 0, 100);
translate.setDuration(3000);
translate.setInterpolator(new LinearInterpolator());
translate.setRepeatCount(5);
translate.setFillAfter(true);
mImg.startAnimation(translate);
break;
case R.id.mBtn_xuan:
rotate=new RotateAnimation(0, 180);
rotate.setDuration(3000);
rotate.setInterpolator(new LinearInterpolator());
rotate.setRepeatCount(5);
mImg.startAnimation(rotate);
break;
default:
break;
}
}

}


XML

activity_main

<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"
    tools:context=".MainActivity" 
    android:orientation="vertical">
<Button 
   android:id="@+id/mBtn_shen"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="伸缩"
   />
<Button 
   android:id="@+id/mBtn_jian"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="渐变"
   />
<Button 
   android:id="@+id/mBtn_wei"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="位移"
   />
<Button 
   android:id="@+id/mBtn_xuan"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="旋转"
   />
<ImageView 
   android:id="@+id/mImg"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher"
   android:layout_gravity="center"
   />
</LinearLayout>