android设置图片变化的四种效果代码
activity代码如下:
package com.example.chapter12_graphic_animation;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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 Activity {
private Button b1,b2,b3,b4;
private ImageView girlImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.test);
this.getWindow().setBackgroundDrawable(drawable);
setContentView(R.layout.main);
girlImage=(ImageView)findViewById(R.id.ImageView01);
b1=(Button)findViewById(R.id.Button01);
b2=(Button)findViewById(R.id.Button02);
b3=(Button)findViewById(R.id.Button03);
b4=(Button)findViewById(R.id.Button04);
b1.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//创建Scale尺寸变化动画
Animation scaleAnimation =new ScaleAnimation(0f,1f,0f,1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
//设置动画持续的时常
scaleAnimation.setDuration(3000);
//开始动画
girlImage.startAnimation(scaleAnimation);
}
});
b2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//创建Scale尺寸变化动画
Animation alphaAnimation =new AlphaAnimation(0.1f,1.0f);
//设置动画持续的时常
alphaAnimation.setDuration(3000);
//开始动画
girlImage.startAnimation(alphaAnimation);
}
});
b3.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//创建Scale尺寸变化动画
Animation translateAnimation =new TranslateAnimation(10,100,10,100);
//设置动画持续的时常
translateAnimation.setDuration(3000);
//开始动画
girlImage.startAnimation(translateAnimation);
}
});
b4.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//创建Scale尺寸变化动画
Animation rotateAnimation =new RotateAnimation(0f,+360f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
//设置动画持续的时常
rotateAnimation.setDuration(3000);
//开始动画
girlImage.startAnimation(rotateAnimation);
}
});
}
//为按钮添加监听事件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML代码设置如下:
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Scale.." />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Alpha..." />
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Translate..." />
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Rotate..." />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/girl" />
</LinearLayout>