- package com.lee.vivid;
- import android.app.Activity;
- import android.os.Bundle;
- 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.ImageButton;
- public class Vivid extends Activity {
- /** Called when the activity is first created. */
- ImageButton img1;
- ImageButton img2;
- ImageButton img3;
- ImageButton img4;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- img1=(ImageButton)findViewById(R.id.img1);
- img2=(ImageButton)findViewById(R.id.img2);
- img3=(ImageButton)findViewById(R.id.img3);
- img4=(ImageButton)findViewById(R.id.img4);
- //四个图片按钮都是自带的icon,用同一个监听器
- img1.setImageResource(R.drawable.icon);
- img1.setOnClickListener(new c());
- img2.setImageResource(R.drawable.icon);
- img2.setOnClickListener(new c());
- img3.setImageResource(R.drawable.icon);
- img3.setOnClickListener(new c());
- img4.setImageResource(R.drawable.icon);
- img4.setOnClickListener(new c());
- }
- private class c implements OnClickListener{
- //当你点击的图片按钮,会向监听器传入自身的View对象
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //根据获得的对象ID判断执行哪个CASE;
- int id=v.getId();
- switch(id){
- case R.id.img1:
- AlphaAnimation alpha=new AlphaAnimation(1,0);
- alpha.setDuration(1000);
- img1.setAnimation(alpha);
- break;
- case R.id.img2:
- ScaleAnimation scale=new ScaleAnimation(1, 0.1f, 1, 0.1f,
- Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF, 0.5f);
- scale.setDuration(2000);
- img2.setAnimation(scale);
- break;
- case R.id.img3:
- RotateAnimation rotate=new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT,
- 0.25f, Animation.RELATIVE_TO_PARENT, 0.25f);
- rotate.setDuration(4000);
- img3.setAnimation(rotate);
- break;
- case R.id.img4:
- TranslateAnimation translate=new TranslateAnimation(Animation.RELATIVE_TO_SELF,
- 0, Animation.RELATIVE_TO_SELF, 0,
- Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
- translate.setDuration(2000);
- img4.setAnimation(translate);
- break;
- }
- }
- }
- }
转载于:https://blog.51cto.com/javiylee/565492