}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.package test.IBrave.game;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class EX04_16B extends Activity {
private static ViewGroup[] viewGroup = new ViewGroup[3];
private static ImageView[][] imageViewGroup = new ImageView[3][2];
private static int[] sa = new int[]{
R.drawable.p01,R.drawable.p02,R.drawable.p03
};
private TextView textView;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewGroup[0] = (ViewGroup)findViewById(R.id.viewGroupLeft);
viewGroup[0].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
viewGroup[1] = (ViewGroup)findViewById(R.id.viewGroupMiddle);
viewGroup[1].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
viewGroup[2] = (ViewGroup)findViewById(R.id.viewGroupRight);
viewGroup[2].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
imageViewGroup[0][0] = (ImageView)findViewById(R.id.imageView1);
imageViewGroup[0][1] = (ImageView)findViewById(R.id.imageViewB1);
imageViewGroup[1][0] = (ImageView)findViewById(R.id.imageView2);
imageViewGroup[1][1] = (ImageView)findViewById(R.id.imageViewB2);
imageViewGroup[2][0] = (ImageView)findViewById(R.id.imageView3);
imageViewGroup[2][1] = (ImageView)findViewById(R.id.imageViewB3);
textView = (TextView)findViewById(R.id.text);
button = (Button)findViewById(R.id.againBtn);
//button.setClickable(false);
button.setVisibility(View.GONE);
random();
//left
imageViewGroup[0][0].setOnClickListener(listener);
//middle
imageViewGroup[1][0].setOnClickListener(listener);
//right
imageViewGroup[2][0].setOnClickListener(listener);
//濂藉儚鍙互鎵撴柇鍏剁炕杞?褰揇uration鏃堕棿璁剧疆闀夸竴鐐瑰氨鍙互鐪嬪嚭鏉?
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button.setClickable(false);
for(int i=0;i<3;i++){
//涓夊紶鐗屼笉鏄悓鏃剁炕杞?
executeRotation(v,0, -90,viewGroup[i], imageViewGroup[i][1], imageViewGroup[i][0]);
//imageViewGroup[i][0].setClickable(true);
imageViewGroup[i][1].setAlpha(255);
random();
}
}
});
}
/**
* 娲楃墝
*/
private void random(){
for(int i=0;i<3;i++){
int tmp = sa[i];
int s = (int)(Math.random()*2);
sa[i]=sa[s];
sa[s]=tmp;
}
}
/**
*
* @param start 寮€濮嬬殑瑙掑害
* @param end 缁撴潫鐨勮搴?
* @param viewGroup
* @param imageViewGone 璁剧疆涓洪潪鍙鎬х殑鍥剧墖
* @param imageViewVisible 璁剧疆涓哄彲瑙佹€х殑鍥剧墖
*/
private void executeRotation(final View v,final float start,final float end,final ViewGroup viewGroup,final ImageView imageViewGone,final ImageView imageViewVisible){
final float centerX = viewGroup.getWidth() / 2.0f;
final float centerY = viewGroup.getHeight() / 2.0f;
RotateAnimation rotation =
new RotateAnimation(start, -end, centerX, centerY, 0f, true);
//how long this animation should last.The duration cannot be negative//璐熸暟
rotation.setDuration(500);
//Sets the acceleration(鍔犻€? curve(k蓹:v鏇茬嚎) for this animation. Defaults to a linear(藞lini蓹) interpolation(绡℃敼锛涙坊鍐欙紝鎻掕ˉ).
rotation.setInterpolator(new AccelerateInterpolator());
viewGroup.startAnimation(rotation);
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
viewGroup.post(new Runnable() {
@Override
public void run() {
imageViewGone.setVisibility(View.GONE);
imageViewVisible.setVisibility(View.VISIBLE);
RotateAnimation rotatiomAnimation = new RotateAnimation(end, start, centerX, centerY, 0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
viewGroup.startAnimation(rotatiomAnimation);
rotatiomAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if( v != findViewById(R.id.againBtn) ){
boolean isWin = false;
if( v == findViewById(R.id.imageView1) ){
//璁剧疆鍥剧墖鐨勯€忔槑搴?
imageViewGroup[1][1].setAlpha(160);
imageViewGroup[2][1].setAlpha(160);
//妫€楠屾槸鍚︾寽瀵逛簡
if(sa[0]==R.drawable.p01){
isWin = true;
}
}else if( v == findViewById(R.id.imageView2) ){
imageViewGroup[0][1].setAlpha(160);
imageViewGroup[2][1].setAlpha(160);
if(sa[1]==R.drawable.p01){
isWin = true;
}
}else{
imageViewGroup[0][1].setAlpha(160);
imageViewGroup[1][1].setAlpha(160);
//if( imageViewGroup[2][1] == findViewById(R.drawable.p01) ){
if( sa[2] == R.drawable.p01 ){
isWin = true;
}
}
if(isWin){
textView.setText(R.string.win);
}else{
textView.setText(R.string.lose);
}
//鎸夐挳鍙
button.setVisibility(View.VISIBLE);
button.setClickable(true);
}else{
textView.setText(R.string.guess);
button.setVisibility(View.GONE);
}
}
});
}
});
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationStart(Animation arg0) {
if( ( button.getVisibility() == View.GONE ) ){
textView.setText("璋滈鍗冲皢鎻檽鈥︹€?);
}
}
});
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i < 3;i++){
//random()鍚庢瘡娆¢兘瑕侀噸鏂拌缃浘鐗?
imageViewGroup[i][1].setImageDrawable(getResources().getDrawable(sa[i]));
imageViewGroup[i][1].setClickable(false);//鍙互涓嶉渶瑕?
executeRotation(v,0, 90,viewGroup[i], imageViewGroup[i][0], imageViewGroup[i][1]);
}
}
};
}