猜猜红桃A在哪里(ImageView单击事件与透明度处理)

猜猜红桃A在哪里(ImageView单击事件与透明度处理)

新建一个继承Activity类的ImageViewGameActivity,并设置布局文件为:imageviewgame.xml。

首先定义布局文件。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <ImageView

            android:id="@+id/imageviewgame_image01"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:layout_weight="1"

            android:src="@drawable/p_b1" />

 

        <ImageView

            android:id="@+id/imageviewgame_image02"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:layout_weight="1"

            android:src="@drawable/p_b1" />

 

        <ImageView

            android:id="@+id/imageviewgame_image03"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:layout_weight="1"

            android:src="@drawable/p_b1" />

    </LinearLayout>

 

    <Button

        android:id="@+id/imageviewgame_btn"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/sure" />

 

    <TextView

        android:id="@+id/imageviewgame_tv"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:gravity="center_horizontal"

        android:text="@string/sure"

        android:textSize="20sp" />

 

</LinearLayout>

效果:

而后在Activity获取这5个组件。

package lyx.feng.second;

......

public class ImageViewGame extends Activity implements OnClickListener {

    private ImageView image1 = null;

    private ImageView image2 = null;

    private ImageView image3 = null;

    private Button btn = null;

    private TextView tv = null;

    private List<Integer> cards = new ArrayList<Integer>();

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.imageviewgame);

       initViews();

 

       this.btn.setText("再来一次");

       this.btn.setVisibility(View.GONE);

    }

 

    private List<Integer> getRandomCards(List<Integer> cards) {

       Collections.shuffle(cards);

       return cards;

    }

 

    private void setImages(List<Integer> cards) {

       this.image1.setImageResource(cards.get(0));

       this.image2.setImageResource(cards.get(1));

       this.image3.setImageResource(cards.get(2));

    }

 

    private void initViews() {

       this.image1 = (ImageView) super

              .findViewById(R.id.imageviewgame_image01);

       this.image2 = (ImageView) super

              .findViewById(R.id.imageviewgame_image02);

       this.image3 = (ImageView) super

              .findViewById(R.id.imageviewgame_image03);

       this.btn = (Button) super.findViewById(R.id.imageviewgame_btn);

       this.tv = (TextView) super.findViewById(R.id.imageviewgame_tv);

 

       cards.add(R.drawable.p_h_2);

       cards.add(R.drawable.p_h_4);

       cards.add(R.drawable.p_h_7);

 

       this.image1.setOnClickListener(this);

       this.image2.setOnClickListener(this);

       this.image3.setOnClickListener(this);

       this.btn.setOnClickListener(this);

       this.tv.setVisibility(View.GONE);

    }

 

    @Override

    public void onClick(View v) {

       switch (v.getId()) {

       case R.id.imageviewgame_image01:

           showcard();

           break;

       case R.id.imageviewgame_image02:

           showcard();

           break;

       case R.id.imageviewgame_image03:

           showcard();

           break;

       case R.id.imageviewgame_btn:

           this.image1.setImageResource(R.drawable.p_b1);

           this.image2.setImageResource(R.drawable.p_b1);

           this.image3.setImageResource(R.drawable.p_b1);

           this.image1.setClickable(true);

           this.image2.setClickable(true);

           this.image3.setClickable(true);

           this.btn.setVisibility(View.GONE);

           break;

       }

    }

 

    private void showcard() {

       setImages(getRandomCards(cards));

       this.image1.setClickable(false);

       this.image2.setClickable(false);

       this.image3.setClickable(false);

       this.btn.setVisibility(View.VISIBLE);

    }

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值