-1

我是安卓新手。如何从图库视图中弹出选定的图像。我附上了我的编码。

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;
            }

        }
    }
4

1 回答 1

1

适用于相对较小图像的解决方案。

如果您要加载大图像,那么您应该阅读http://developer.android.com/training/displaying-bitmaps/load-bitmap.html(查看decodeSampledBitmapFromResourcecalculateInSampleSize方法并使用 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 布局,那么您可以在一个方法中完成所有操作(只需用showPopupImage上面的 Java 代码中的方法替换):

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();
}
于 2013-07-03T07:29:35.567 回答