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

    xiaoxiao2024-11-29  5

    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(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-1294098.html
    最新回复(0)