动画效果监听事件

1.Activity类的实现:

package com.wang.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.wang.utils.ReflectUtil;

public class MainActivity extends Activity implements OnClickListener{
	private final static int SIZE = 11;
	
	private ImageView[] imageViews;
	private Animation[] animations;
	
	private Button mButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// 鍒濆鍖栨帶浠�
		initRes();
	}


	private void initRes() {
		mButton = (Button) findViewById(R.id.button1);
		mButton.setOnClickListener(this);
		
		int index = 0;
		imageViews = new ImageView[SIZE];
		for (int i = 0; i < SIZE; i++) {
			index = i + 1;
			
			int id = ReflectUtil.getResourceId("id", "white_rect" + index);
			ImageView imageView = (ImageView) findViewById(id);
			imageView.setOnClickListener(this);
			imageViews[i] = imageView;
		}
		
		
		animations = new Animation[SIZE];
		for (int i = 0; i < SIZE; i++) {
			index = i + 1;
			int id = ReflectUtil.getResourceId("anim", "my_anim" + index);
			Animation animation = AnimationUtils.loadAnimation(this, id);
			animation.setFillAfter(true);
			animation.setFillBefore(true);
			animation.setFillEnabled(true);
			animation.setAnimationListener(new MyAnimListener());
			animations[i] = animation;
		}
		
	}

	class MyAnimListener implements AnimationListener{
		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				for (int i = 0; i < SIZE; i++) {
					imageViews[i].startAnimation(animations[i]);
					imageViews[i].setColorFilter(null);
				}
			}
			
			if (msg.what == 2) {
				int index = msg.arg1;
				for (int i = 0; i < index; i++) {
					imageViews[i].setColorFilter(Color.BLUE);
					imageViews[i].setColorFilter(null);
				}
			}
		};
	};
	
	public class MyHandler implements Runnable{
		private Handler mHandler;
		private String type;
		private int index;

		public MyHandler(String type,Handler mHandler,int index) {
			super();
			this.type = type;
			this.index = index;
			this.mHandler = mHandler;
		}

		@Override
		public void run() {
			Message msg = new Message();
			if (type.equals("Button")) {
				msg.what= 1;
				mHandler.sendMessage(msg);
			}
			if (type.equals("ImageView")) {
				msg.what= 2;
				msg.arg1 = index;
				mHandler.sendMessage(msg);
			}
		}
		
	}

	@Override
	public void onClick(View v) {
		if (v instanceof Button) {
			Thread thread = new Thread(new MyHandler("Button", mHandler, 0));
			thread.start();
		}
		
		ImageView imageView = null;
		if (v instanceof ImageView) {
			int id = v.getId();
			int index = 0;
			for (int i = 0; i < SIZE; i++) {
				index = i + 1;
				
				int idTmp = ReflectUtil.getResourceId("id", "white_rect" + index);
				
				if (id == idTmp) {
					imageView = imageViews[i];
					break;
				}
			}
			
			if (imageView != null) {
				Thread thread = new Thread(new MyHandler("ImageView", mHandler, index));
				thread.start();
			}
		}
	}
}



2.anim动画xml文件

my_anim1.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="0"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="500%"
        android:toDegrees="-50" />

</set>

my_anim2.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="0"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="500%"
        android:toDegrees="-40" />

</set>

3.布局文件activity_main.xml

<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:background="@drawable/background"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/white_rect1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
    
     <ImageView
        android:id="@+id/white_rect2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
     
     <ImageView
        android:id="@+id/white_rect3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
    
     <ImageView
        android:id="@+id/white_rect4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />

	<ImageView
        android:id="@+id/white_rect5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
    
     <ImageView
        android:id="@+id/white_rect6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
     
     <ImageView
        android:id="@+id/white_rect7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
    
     <ImageView
        android:id="@+id/white_rect8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" /> 
        
     <ImageView
        android:id="@+id/white_rect9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
     
     <ImageView
        android:id="@+id/white_rect10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" />
    
     <ImageView
        android:id="@+id/white_rect11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="119dp"
        android:layout_marginTop="129dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/white_rect_y" /> 
     
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/white_rect1"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="96dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Button"/>

</RelativeLayout>

白色小方块。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值