9

Assetsフォルダーに画像が存在する画像のグリッドビューの作成に取り組んでいます。Androidリンクのアセットフォルダーからファイルを開くと、ビットマップを使用して読み取ることができました。現在持っているコードは次のとおりです。

 public View getView(final int position, View convertView, ViewGroup parent) 
{

  try 
    {
     AssetManager am = mContext.getAssets();
     String list[] = am.list("");
     int count_files = imagelist.length;
     for(int i= 0;i<=count_files; i++)
     {
      BufferedInputStream buf = new BufferedInputStream(am.open(list[i]));
      Bitmap bitmap = BitmapFactory.decodeStream(buf);
      imageView.setImageBitmap(bitmap);
      buf.close();
     }
   }   
   catch (IOException e) 
   {
   e.printStackTrace();
   }
  }

私のアプリケーションは Assets フォルダーから画像を読み取りますが、グリッド ビューのセルを反復処理していません。グリッド ビューのすべてのセルには、一連の画像から選択された同じ画像があります。セルを反復処理して、まだ異なる画像を持つ方法を教えてもらえますか?

BaseAdapterクラスを拡張するImageAdapterクラスに上記のコードがあり、メインクラスでは、次の方法でそれをグリッドビューにリンクしています。

    GridView  gv =(GridView)findViewById(R.id.gridview);
    gv.setAdapter(new ImageAdapter(this, assetlist));       

事前に助けてくれてありがとう、サラン

4

2 に答える 2

19

サラン、以下は、ギャラリーでアセットフォルダーに画像を表示するために使用するものです。グリッドビューでも同じことだと思います:

public class myActivitye extends Activity
{
    private Gallery mGallery;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGallery = (Gallery) findViewById(R.id.mygalleryinxml);

        //load images into memory
        mBitArray = new Bitmap[4];
        try
        {
            //these images are stored in the root of "assets"
            mBitArray[0] = getBitmapFromAsset("pic1.png");
            mBitArray[1] = getBitmapFromAsset("pic2.png");
            mBitArray[2] = getBitmapFromAsset("pic3.png");
            mBitArray[3] = getBitmapFromAsset("pic4.png");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        mGallery.setAdapter(new GalleryAdapter(this, mBitArray));
    }

    public class GalleryAdapter extends BaseAdapter
    {
        //member variables
        private Context mContext;
        private Bitmap[] mImageArray;

        //constructor
        public GalleryAdapter(Context context, Bitmap[] imgArray)
        {
            mContext = context;
            mImageArray = imgArray;
        }

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

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

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

        //returns the individual images to the widget as it requires them
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ImageView imgView = new ImageView(mContext);

            imgView.setImageBitmap(mImageArray[position]);

            //put black borders around the image
            final RelativeLayout borderImg = new RelativeLayout(mContext);
            borderImg.setPadding(20, 20, 20, 20);
            borderImg.setBackgroundColor(0xff000000);//black
            borderImg.addView(imgView);
            return borderImg;
        }

    }//end of: class GalleryAdapter


    /**
     * Helper Functions
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        istr.close();

        return bitmap;
    }
}
于 2011-05-05T19:40:57.327 に答える
3

毎回すべての項目を読む必要はありません。getView メソッド呼び出しで指定された位置の項目のみを読み取ります。その時そのアイテムだけを表示します。

BufferedInputStream buf = new BufferedInputStream(am.open(list[position]));
于 2010-11-12T09:01:21.453 に答える