我是android新手。如何从图库视图中弹出所选图像。我附上了我的编码。
public class mygallerycode extends Activity
{
Integer[] a={R.drawable.hotel1,R.drawable.hotel2,R.drawable.hotel3};
Gallery gallery;
ImageView imageview;
public void onCreate(Bundle bb) {
super.onCreate(bb);
setContentView(R.layout.mygallery);
gallery=(Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
imageview=(ImageView)findViewById(R.id.imageView1);
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),"picture" + (position+1) +"selected",Toast.LENGTH_SHORT).show();
//imageview.setImageResource(a[position]);
}
});
}
class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray ta = obtainStyledAttributes(R.styleable.gallery1);
itemBackground = ta.getResourceId(
R.styleable.gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return a.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 = new ImageView(context);
imageView.setImageResource(a[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
答案 0 :(得分:1)
适用于相对较小图像的解决方案。
如果你要加载大图片,那么你应该阅读http://developer.android.com/training/displaying-bitmaps/load-bitmap.html(看看decodeSampledBitmapFromResource
和calculateInSampleSize
方法并使用AsyncTask)
但是对于相对较小的图像,这是一种可能的解决方案:
mygallerycode活动类:
public class mygallerycode extends Activity
{
Integer[] a={R.drawable.hotel1,R.drawable.hotel2,R.drawable.hotel3};
Gallery gallery;
ImageView imageview;
public void onCreate(Bundle bb) {
super.onCreate(bb);
setContentView(R.layout.mygallery);
gallery=(Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
imageview=(ImageView)findViewById(R.id.imageView1);
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
showPopupImage(a[position]);
}
});
}
private void showPopupImage(Integer imageResource) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_fullimage_dialog,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
image.setImageResource(imageResource);
imageDialog.setView(layout);
imageDialog.setCancelable(true);
imageDialog.create();
imageDialog.show();
}
class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray ta = obtainStyledAttributes(R.styleable.gallery1);
itemBackground = ta.getResourceId(
R.styleable.gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return a.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 = new ImageView(context);
imageView.setImageResource(a[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
custom_fullimage_dialog XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/fullimage" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
</LinearLayout>
如果您因某些原因想跳过XML布局,那么您可以在一个方法中完成所有操作(只需用上面的Java代码替换showPopupImage
方法):
private void showPopupImage(Integer imageResource) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
ImageView image = new ImageView(this);
image.setImageResource(imageResource);
imageDialog.setView(image);
imageDialog.setCancelable(true);
imageDialog.create();
imageDialog.show();
}