如何在Android中设置图像资源?

时间:2015-07-15 14:01:22

标签: java android eclipse

我正在为eclipse上的扑克游戏编写代码而且我仍然坚持如何让cardHand区域中的卡片点击显示在playedCards区域并将其从甲板上移除我正在画它。这是代码:

package com.viktorengineering.poker254;

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;

public class Poker extends Activity {
    private ImageView mViewDeck;
    private ImageView mViewHand;
    private ImageView mViewPlayedCards;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.getWindow().setBackgroundDrawableResource(R.drawable.green_back);

        mViewDeck = (ImageView) findViewById(R.id.deckImage);
        mViewDeck.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int[] imagesArray = {R.drawable.clubs_ace, R.drawable.hearts_seven, R.drawable.diamonds_five,
                        R.drawable.clubs_three, R.drawable.hearts_eight, R.drawable.spades_six};
                Random random = new Random();
                mViewHand.setImageResource(imagesArray[new Random().nextInt(6)]);
            }
        });
        mViewHand = (ImageView) findViewById(R.id.cardHand);
        mViewHand.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mViewPlayedCards.setImageResource(0);
                mViewHand.setImageResource(0);
            }
        });
        mViewPlayedCards = (ImageView) findViewById(R.id.playedCards);

    }

}

1 个答案:

答案 0 :(得分:0)

可绘制文件夹中的图像表示使用以下代码

mViewPlayedCards.setImageResource(R.drawable.icon);  mViewHand.setImageResource(R.drawable.icon_home);