4.16 猜猜红心A在哪里--ImageView的使用

package com.chaowen;

import android.app.Activity;
import android.os.Bundle;
import android.text.StaticLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Ex04_17_ImageView extends Activity {
    /** Called when the activity is first created. */
	private ImageView i1;
	private ImageView i2;
	private ImageView i3;
	private Button b1;
	private TextView t1;
	 //存入三张牌的ID
	 private static int[] ss=new int[]{R.drawable.p01,R.drawable.p02,R.drawable.p03};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        i1=(ImageView)findViewById(R.id.mImage01);
        i2=(ImageView)findViewById(R.id.mImage02);
        i3=(ImageView)findViewById(R.id.mImage03);
        b1=(Button)findViewById(R.id.mButton);
        t1=(TextView)findViewById(R.id.mText);
        
        //开始洗牌
        randon();
        
        i1.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				 //三张牌同 时翻面,并将未选择的两张变透明
				 i1.setImageDrawable(getResources().getDrawable(ss[0]));
				 i2.setImageDrawable(getResources().getDrawable(ss[1]));
				 i3.setImageDrawable(getResources().getDrawable(ss[2]));
				 
				 
				 i2.setAlpha(100);
				 i3.setAlpha(100);
				 
				 //判断有没猜对
				 if(ss[0]==R.drawable.p01){
					 t1.setText("恭喜,你猜对了,拍拍手!!!");
				 }else {
					t1.setText("猜错啦,再来一次吧!!!");
				}
			}
        	
        });
        
        i2.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				 //三张牌同 时翻面,并将未选择的两张变透明
				 i1.setImageDrawable(getResources().getDrawable(ss[0]));
				 i2.setImageDrawable(getResources().getDrawable(ss[1]));
				 i3.setImageDrawable(getResources().getDrawable(ss[2]));
				 
				 
				 i1.setAlpha(100);
				 i3.setAlpha(100);
				 
				 //判断有没猜对
				 if(ss[1]==R.drawable.p01){
					 t1.setText("恭喜,你猜对了,拍拍手!!!");
				 }else {
					t1.setText("猜错啦,再来一次吧!!!");
				}
			}
        	
        });
        
        i2.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				 //三张牌同 时翻面,并将未选择的两张变透明
				 i1.setImageDrawable(getResources().getDrawable(ss[0]));
				 i2.setImageDrawable(getResources().getDrawable(ss[1]));
				 i3.setImageDrawable(getResources().getDrawable(ss[2]));
				 
				 
				 i1.setAlpha(100);
				 i2.setAlpha(100);
				 
				 //判断有没猜对
				 if(ss[2]==R.drawable.p01){
					 t1.setText("恭喜,你猜对了,拍拍手!!!");
				 }else {
					t1.setText("猜错啦,再来一次吧!!!");
				}
			}
        	
        });
        
        
        b1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				t1.setText("猜猜看,红心A是哪一张?");
				i1.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				i2.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				i3.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				i1.setAlpha(255);
				i2.setAlpha(255);
				i3.setAlpha(255);
				randon();
			}
        	
        });
    }
    
    
    private void randon()
    {
      for(int i=0;i<3;i++)
      {
        int tmp=ss[i];
        int s=(int)(Math.random()*2);
        ss[i]=ss[s];
        ss[s]=tmp;
      }        
    }
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
      android:id="@+id/mText"
      android:layout_width="270px"
      android:layout_height="40px"
      android:text="@string/str_title"
      android:textSize="18sp"
      android:layout_x="20px"
      android:layout_y="32px"
    >
    </TextView>
    <ImageView
      android:id="@+id/mImage01"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="20px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <ImageView
      android:id="@+id/mImage02"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="126px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <ImageView
      android:id="@+id/mImage03"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="232px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <Button
      android:id="@+id/mButton"
      android:layout_width="118px"
      android:layout_height="wrap_content"
      android:text="@string/str_button"
      android:layout_x="100px"
      android:layout_y="302px"
    >
    </Button>
</AbsoluteLayout>

  strings.xml

  <?xml version="1.0" encoding="utf-8"?>

  <resources>
    <string name="hello">Hello World, EX04_16</string>
    <string name="app_name">EX04_16</string>
    <string name="str_title">猜猜看紅心A是哪一張?</string>
    <string name="str_button">再玩一次</string> 
  </resources>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值