如何在GridView适配器中存储位图?

时间:2016-03-21 00:23:32

标签: android gridview bitmap

我有一个gridview菜单,其中有6个网格项,每个网格项都包含一个Bitmap背景图像,此gridView的gridView适配器如下所示...

public final class GridAdapter扩展了BaseAdapter {

private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
private Bitmap bitTemp;

public GridAdapter(Context context) {

    //Create a bit object
    Bit bit = new Bit(R.drawable.rec2);

    //Store bitmap inside a bitmap object
    bitTemp = bit.blurredBitmap1;



    //Inflater
    mInflater = LayoutInflater.from(context);


    //Add data items here
    mItems.add(new Item("Mon",bitTemp,R.drawable.pic1));
    mItems.add(new Item("Tues",bitTemp,R.drawable.pic2));
    mItems.add(new Item("Wednesday",bitTemp,R.drawable.pic3));
    mItems.add(new Item("Thursday",bitTemp,R.drawable.pic4));
    mItems.add(new Item("Friday",bitTemp,R.drawable.pic5l));
    mItems.add(new Item("saturday", bitTemp,R.drawable.pic6));


}

@Override
public int getCount() {
    return mItems.size();
}

@Override
public Item getItem(int i) {
    return mItems.get(i);
}

@Override
public long getItemId(int i) {
    return mItems.get(i).drawableId2;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    View v = view;
    ImageView picture,mainBtn;
    TextView name;


    if (v == null) {

        //Inflate gridItem layout
        v = mInflater.inflate(R.layout.grid_item, viewGroup, false);

        //Set Tag
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
        v.setTag(R.id.mainBtn,v.findViewById(R.id.mainBtn));


    }


    //Cast items
    picture = (ImageView) v.getTag(R.id.picture);
    name = (TextView) v.getTag(R.id.text);
    mainBtn = (ImageView)v.getTag(R.id.mainBtn);



    Item item = getItem(i);


    //Link to item class
    name.setText(item.name);
    picture.setImageBitmap(item.bitmap1);
    mainBtn.setImageResource(item.drawableId2);


    return v;
}


//Create Bitmap images class

public class Bit extends Object{


   public Bitmap blurredBitmap1;

   Context context;



    Bit(int resId) {

       Bitmap bitmapTemp;

      //Convert image into Bitmap
      bitmapTemp =BitmapFactory.decodeResource(context.getResources(),resId);

      //Then blur image
      blurredBitmap1 = BlurBuilder.blur(context,bitmapTemp);





    }




}

}

下面是包含将传递给List items ....的数据项的类。

// Grid data class 

       class Item {

        public final String name;
        public final Bitmap bitmap1;
        public final int drawableId2;




           Item(String name, Bitmap bitmap1,int drawableId2) {

               this.name = name;
               this.bitmap1 = bitmap1;
               this.drawableId2 = drawableId2;





           }




    }

类“bit”构造模糊的位图图像,它接受资源id参数,这在GridAdapter构造函数中初始化

   public GridAdapter(Context context) {

        //Create a bit object
        Bit bit = new Bit(R.drawable.rec2);

        //Store bitmap inside a bitmap object
        bitTemp = bit.blurredBitmap1;
......

}

然而,当我运行此代码时,我收到错误...

error log

请提供一个简单的答案,因为我对android很新,

谢谢

1 个答案:

答案 0 :(得分:0)

类Bit中的上下文为null,因为您没有初始化 所以代码

bitmapTemp =BitmapFactory.decodeResource(context.getResources(),resId);  

抛出NullPointException。