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();
}
}
}
}
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>
白色小方块。