为ListView创建缩略图的最佳方式

时间:2014-02-22 09:08:49

标签: android listview android-listview android-asynctask

我有完整尺寸的照片存储在SD卡上,然后重新调整尺寸并显示在List-view中。我遇到的麻烦是,我目前拥有的代码存在以下问题。

  1. 需要很长时间才能让屏幕呈现页面,有时我会得到一个'ANR'
  2. 一旦屏幕渲染,ListView的滚动就会非常不连贯,不适合用途。
  3. 下面是我的代码

    @Override
    public void setViewImage(ImageView v, String imagePath) {
      if(imagePath==null || "".equals(imagePath)) {
        return;
      }
    
      try {
         Drawable d = ImageUtils.getThumbnailDrawable(getResources(), imagePath,             
                                                      v.getDrawable().getIntrinsicWidth(),
                                                      v.getDrawable().getIntrinsicHeight());
         if(d!=null) {
        v.setImageDrawable(d); 
         }
      } catch (Exception e) {
         Log.e(ACTIVITY_SERVICE, e.getMessage(), e);
      }
    }
    

    ImageUtils.getThumbnailDrawable方法如下所示

    /**
     * Creates a thumbnail image that will be resized to the size of the supplied
     * ImageView
     * @param resources
     * @param path
     * @param width  - the required width of the image
     * @param height - the required height of the image 
     * @return
     */
    public static BitmapDrawable getThumbnailDrawable(Resources resources, String path, int width, int height) {
        Bitmap b = BitmapFactory.decodeFile(path);
        if(b==null) {
          return null;
        }
    
        int imageWidth  = b.getWidth();
        int imageHeight = b.getHeight();
    
        int orientation = getOrientation(path);
        Matrix mat = new Matrix();
    
        if (orientation == ExifInterface.ORIENTATION_NORMAL || orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            //landscape photo
            if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                mat.postRotate(180);
            }
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90 || orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            //portrait photo
            if(orientation == ExifInterface.ORIENTATION_ROTATE_90) {
              mat.postRotate(90);   
            } else {
              mat.postRotate(270);
            }   
    
            //Image is in landscape mode and after rotation will be portrait
            imageHeight=b.getWidth();
            imageWidth=b.getHeight();
        } else {
            return null;  //Cannot proceed
        }
    
        //Scale based on width
            float scaleWidth  = (float)(width)/imageWidth;
            float scaleHeight = (float)(height)/imageHeight;
    
            if(scaleWidth > scaleHeight) {          
                mat.postScale(scaleHeight, scaleHeight);
            } else {
    
                mat.postScale(scaleWidth, scaleWidth);
            }
    
            Bitmap resized = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
    
            b.recycle();
            b = null;
    
            return new BitmapDrawable(resources, resized);
        }   
    }
    

    此代码目前正在UI线程上运行,我怀疑如果我将其设为Asynctask它会运行得更好,但我认为它不会解决缓慢的渲染和滚动问题。

    我想知道在拍摄相机图像的同时创建缩略图是否更为理想?

    由于

    我已经尝试过Picasso,但现在ListView会显示出来,但是10行非常小,没有填充任何TextView,也没有填充ImageView。我将SimpleCursorAdapter扩展如下

    私有类CustomReportCursorAdapter扩展SimpleCursorAdapter {     私有最终上下文上下文;

    @SuppressLint("NewApi")
    public CustomReportCursorAdapter(Context context, int layout, ReportCursor cursor, String[] FROM, int[] TO, int i) {
        super(context, layout, cursor, FROM, TO, i);
        this.context=context;
    }
    
    
    @Override
    public void setViewImage(ImageView v, String imagePath) {
      if(imagePath==null || "".equals(imagePath)) {
        return;
      }
      Picasso.with(context).load(imagePath).fit().into(v);
    }
    

    }

    ListView的每一行的相应布局如下

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="horizontal"
            android:background="#FFFFFF"
            android:layout_marginBottom="5dp">
    
            <LinearLayout android:layout_width="wrap_content" 
                          android:layout_height="wrap_content"
                          android:background="#000000"
                          android:orientation="vertical"
                          android:layout_margin="1dp">
    
              <ImageView
                android:id="@+id/camera_icon1"
                android:contentDescription="@string/new_report_image_description"
                android:src="@drawable/camera_icon_list_view" 
                android:scaleType="centerCrop"
                android:layout_margin="1dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
            <View
                android:layout_height="2dp"
                android:layout_width="fill_parent"
                android:background="#FFFFFF" />
    
            <ImageView
                android:id="@+id/camera_icon2"
                android:contentDescription="@string/new_report_image_description"
                android:src="@drawable/camera_icon_list_view" 
                android:scaleType="centerCrop"
                android:layout_margin="1dp"                    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            </LinearLayout>
    
            <LinearLayout android:layout_width="fill_parent" 
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:background="#000000"
                          android:layout_margin="1dp">
           <LinearLayout android:layout_width="fill_parent" 
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:background="#CC6600"
                          android:layout_margin="1dp">
            <TextView
                android:id="@+id/report_type"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/new_report_images"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/report_sub_type"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/new_report_images"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
              </LinearLayout>     
            </LinearLayout>
        </LinearLayout>
    

    由于

0 个答案:

没有答案