我想做什么 :: 我正在努力学习使用dialogs
,OnClick
imageview
我希望展示一个自定义对话框,其中包含存储在sdcard
中的图像。然后on-select of any one image
,我想set
imageview
public class DataAcceptActivity extends Activity {
InputStream is;
EditText name;
ImageView imageView;
int[] image_array={R.drawable.index,R.drawable.image1,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showImages();
}
});
}
protected void showImages() {
// TODO Auto-generated method stub
final Dialog dlg=new Dialog(DataAcceptActivity.this);
dlg.setContentView(R.layout.grid_view);
GridView GV=(GridView) dlg.findViewById(R.id.gridView_id);
GV.setAdapter(new ImageAdapter(DataAcceptActivity.this));
GV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
imageView.setImageResource(image_array[arg2]);
dlg.dismiss();
}
});
dlg.show();
}
class ImageAdapter extends BaseAdapter{
Context cxt;
public ImageAdapter(DataAcceptActivity dataAcceptActivity) {
// TODO Auto-generated constructor stub
this.cxt=dataAcceptActivity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return image_array.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView ;
if(convertView==null){
imageView=new ImageView(cxt);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(15, 15, 15, 15);
}else{
imageView=(ImageView) convertView;
}
imageView.setImageResource(image_array[position]);
return imageView;
}
}
}
我做了什么 :: 我已经达到了上述可绘制图像的规格
DataAcceptActivity.java
{{1}}
如何修改代码以实现我的目标!
感谢
答案 0 :(得分:1)
这是包含从图像路径
创建的位图的Arraylist
ArrayList<Bitmap> bitmaps= new ArrayList<Bitmap>();
这是您的图片文件夹路径
File file = new File(Environment.getExternalStoragePath()+"/Stampii/");
这是文件夹
中的图像列表
file imageList[] = file.listFiles();
This code take image from path and store in bitmap arraylist.
for(int i=0;i<imageList.length;i++)
{
Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());
Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
bitmaps.add(b);
}
现在,在Bitmap array
中使用此int[] image_array
并使用imageView.setImageBitmap(bitmaps.get(position));
imageView.setImageResource(image_array[position]);
由于