Gallery使用 主要记录一些 注意的地方

xml布局
<!--<Gallery-->

<!--android:layout_below="@id/topBar"-->
<!--android:gravity="center_vertical"-->
<!--android:background="@color/black"-->
<!--android:id="@+id/my_gallery"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--&gt;-->
<!--</Gallery>-->
oncreate
bitmaps = new ArrayList<Bitmap>();
        init();
        adapter = new MyGalleryAdapter();
        myGallery.setAdapter(adapter);

初始化数据
  private void init() {
        Intent intent = getIntent();
        helpTopicImage = intent.getStringArrayListExtra("picList");

        firstPosition = intent.getIntExtra("position", 0);


        new Thread() {
            @Override
            public void run() {
                for (String img : helpTopicImage) {
                    try {
                        //必须
                        Bitmap myBitmap = Glide.with(mContext)
                                .load(img)
                                .asBitmap() //必须
                                .centerCrop()
                                .into(500, 500)
                                .get();


//                        Message message = new Message();
//                        message.obj = myBitmap;
//                        handler.sendMessage(message);
                        bitmaps.add(myBitmap);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
                //子线程循环加载完成之后,再调用adapter,或者使用更新
//                adapter.notifyDataSetChanged();//第一种

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myGallery.setAdapter(adapter);//或者直接在这写setadapter

                    }
                });
            }
        }.start();
    }

点击事件
myGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//                down.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        BitMapFile.saveBitmap2file(bitmaps.get(position), position + ".jpg");
//                    }
//                });
//
//
//            }
//        });
适配器
private class MyGalleryAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return bitmaps.size();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView view = null;
        if (convertView != null) {
            view = (ImageView) convertView;
        } else {
            view = new ImageView(mContext);
        }

        Gallery.LayoutParams params = new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);

        BitmapDrawable bd = new BitmapDrawable(bitmaps.get(position));
        bd.setAntiAlias(true);
        view.setImageDrawable(bd);
        tvNumLocation.setText(position+1+"");
        tvNumCount.setText(bitmaps.size()+"");
        return view;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return bitmaps.get(position);

    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

}
记得 退出 消除bitmap
@Override
protected void onDestroy() {
    super.onDestroy();
    for (Bitmap bitmap : bitmaps) {
        if (bitmap != null && bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值