1

我有图像,我希望它们在画廊中使用适配器来选择每个图像,它将使用画布绘制,我看了很多教程并编写了代码,现在我不知道我在哪里做错了

Gallery g;
ImageAdapter imageAdapter;

       Gallery g = (Gallery) findViewById(R.id.gBitmap);
       imageAdapter = new ImageAdapter(this);
       g.setAdapter(imageAdapter);

    //selected Bitmap from gallery
       g.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent,
               View v, int position, long id) {
               applyBitmap(position);
           }
       });
    }

    public class ImageAdapter extends BaseAdapter {
       int mGalleryItemBackground;
       private Context mContext;

       private Integer[] mImageIds = {
               R.drawable.first,
               R.drawable.second,
               R.drawable.fifth,
               R.drawable.third,
               R.drawable.fourth,

       public ImageAdapter(Context c) {
           mContext = c;
       }

       public int getCount() {
           return mImageIds.length;
       }

       public Object getItem(int position) {
           return position;
       }

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

       public View getView(int position,
           View convertView, ViewGroup parent) {
            return iv;
       }
    }

    public static void Bitmap(int position) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inScaled = true;
        opt.inPurgeable = true;
            opt.inInputShareable = true;
            Bitmap brightBitmap = BitmapFactory.decodeResource(null, position, opt);
        iv.setImageBitmap(brightBitmap);


    }

我需要选择mImageIds使用我的函数调用Bitmap

4

2 回答 2

1

我不知道这是否适合你,

但是对你的代码做一些改动,

1.applyFrame(int position)

该代码行应该是,

Bitmap brightBitmap = BitmapFactory.decodeResource(getResources(), position, opt); 

2.在getView()中

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(mContext);

    i.setImageResource(mImageIds[position]);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);

    return i;
}

3. setOnItemClickListener()

g.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent,
               View v, int position, long id) {
               applyFrame(mImageIds[position]);
           }
       });

试试这个,让我知道会发生什么......如果它有效,那么我会向你解释。如何??

于 2012-08-18T18:26:37.420 回答
0

您应该观看“ listView 的世界”视频并了解如何为adapterView 创建一个适配器(图库扩展了它)。

于 2012-08-18T19:59:17.827 回答