2

露骨な n00b の質問: 写真のディレクトリがいくつかあり、ラジオ ボタンのセットで選択した 1 つのみからランダムに写真を表示したいと考えています。使用時にディレクトリを指定するにはどうすればよいですか:

//"ha" is ha.png, which I would like to be at drawable/1/ha.png
image.setImageResource(R.drawable.ha);

これに使えますsetImageResourceか?もしそうなら、どのように?そうでない場合、何をどのように使用すればよいですか?

演習の目的は、最初のアクティビティで選択可能なさまざまなレッスン (したがってイメージの分割) を含むフラッシュカード プログラムです。

4

2 に答える 2

3

apk で drawables フォルダーを参照している場合、res/drawable の下にサブフォルダーを作成することはできません。

SD カードのランダムなフォルダーを参照している場合は、サブフォルダーを使用しても問題ありませんが、そのアプローチで画像を参照するために R.drawable.* を使用することはできません。

その場合、次を使用して画像をロードする必要があります

Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");

これは、次のように使用できるビットマップを返します

image.setImageBitmap(bmp)

http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)を参照してください。

ラジオ ボタンに加えられた変更に対応するには 、Android のラジオ ボタンで On click リスナーを設定する方法を参照してください。

于 2012-08-05T07:25:04.713 に答える
1

GridView を使用して、ラジオ ボタンから選択したディレクトリから画像を表示できます (要件に応じて)。GridView を作成したら、アダプターを関連付けます。アダプターの例については、以下を参照してください。

public class ImageAdapter extends BaseAdapter {

    /** LayoutInflater. */
    private LayoutInflater mInflater;

    /** The i. */
    private ImageView i;

    /**
     * Instantiates a new image adapter.
     * 
     * @param c
     *            the c
     */
    public ImageAdapter(Context c) {
        mInflater = LayoutInflater.from(c);
    }

    public int getCount() {
                    // scaled pictures will have the list of
                    // which you have from the directory
        return scaledPictures.size();
    }

    public Bitmap getItem(int position) {
        return scaledPictures.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
        convertView = mInflater.inflate(R.layout.image, parent, false);
        } else {
            i = (ImageView) convertView;
        }

        Bitmap bitmap = getItem(position);
        i = (ImageView) convertView.findViewById(R.id.galleryimage);
        i.setImageBitmap(bitmap);
        bitmap = null;

        return i;
    }
}
于 2012-08-05T07:28:27.230 に答える