如何选择要在Java for Android中显示的图片目录(使用单选按钮)?
明显的n00b问题:我有几个图片目录,希望随机显示一个目录中的图片,我通过一组单选按钮选择。使用时如何指定目录:如何选择要在Java for Android中显示的图片目录(使用单选按钮)?,java,android,Java,Android,明显的n00b问题:我有几个图片目录,希望随机显示一个目录中的图片,我通过一组单选按钮选择。使用时如何指定目录: //"ha" is ha.png, which I would like to be at drawable/1/ha.png image.setImageResource(R.drawable.ha); 我可以为此使用setImageResource吗?如果是,怎么做?如果没有,我应该使用什么以及如何使用 练习的对象是一个flashcard程序,在第一个活动中可以选择不同的课程(
//"ha" is ha.png, which I would like to be at drawable/1/ha.png
image.setImageResource(R.drawable.ha);
我可以为此使用setImageResource
吗?如果是,怎么做?如果没有,我应该使用什么以及如何使用
练习的对象是一个flashcard程序,在第一个活动中可以选择不同的课程(因此可以分割图像)。如果您指的是apk中的drawables文件夹,则res/drawable下不能有子文件夹 如果您引用SD卡上的随机文件夹,则可以使用子文件夹,但不能使用R.drawable.*来引用图像 在这种情况下,您需要使用
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");
它返回一个位图,您可以像
image.setImageBitmap(bmp)
看
要对Radio N按钮所做的更改作出反应,请参阅
如果您指的是apk中的drawables文件夹,则res/drawable下不能有子文件夹 如果您引用SD卡上的随机文件夹,则可以使用子文件夹,但不能使用R.drawable.*来引用图像 在这种情况下,您需要使用
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");
它返回一个位图,您可以像
image.setImageBitmap(bmp)
看
要对Radio N按钮所做的更改作出反应,请参阅
您可以使用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;
}
}
您可以使用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;
}
}
可爱。非常感谢。这比我想要的还要好。:)可爱。非常感谢。这比我想要的还要好。:)