Android - gridview滚动问题(非常慢的滚动)

Dav*_*d.C 2 android scroll gridview cursor

这是我的第一个应用程序,所以我不习惯在java上编码.

我有一个gridview,它显示了sdcard的缩略图,但我无法顺利滚动.

如果我把/*处理部分*/放出如果滚动就像我想要的那样但它们是重复的内容并且不显示部分图片.

这是我的代码:

-活动:

        GridView gridview = (GridView) findViewById(R.id.gridview1);
     String[] projection = {MediaStore.Images.Media._ID};
     String path ="/mnt/sdcard/Pictures";
     // Create the cursor pointing to the SDCard
     cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
             projection, 
             //MediaStore.Images.Media.DATA + " like ? ",
             //new String[] {"%Pictures%Sopi%"},  
             "_data LIKE '%" + path  + "/%'",                
             null,
             MediaStore.Images.Media._ID);

    // Get the column index of the Media Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

    gridview.setAdapter(new ImageAdapterTest(RoomRawActivity.this));
Run Code Online (Sandbox Code Playgroud)

-imageAdapater(获取视图):

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

    Log.v("adapter - getView","getView start!!");
    //Toast.makeText(mContext, "getView start!!"+position, 0).show();

    //Move cursor to current position
    RoomRawActivity.cursor.moveToPosition(position);

    ImageView picturesView;

    if (convertView == null) {

        picturesView = new ImageView(mContext);

            /* treatment */

// Get the current value for the requested column
int imageID = RoomRawActivity.cursor.getInt(RoomRawActivity.columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
String url = uri.toString();

//Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1,   url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
         originalImageId, 
         MediaStore.Images.Thumbnails.MINI_KIND, 
         null);
        picturesView.setPadding(5, 5, 5, 5);
        picturesView.setBackgroundResource(R.drawable.border_grid_pics);

        picturesView.setImageBitmap(b);

        picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
            /* END  treatment */
        }
    }else{
        picturesView = (ImageView) convertView;   
    }



    return picturesView;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!大卫.

小智 5

你应该整理你的GetView方法,如;

  • 你不应该在getView上设置你的图像资源,使用AsyncTask
  • 尽量不要在imageViews上使用SET SCALE来提高性能

查看此链接可能很有用;

使用AsyncTask加载位图图像

http://developer.android.com/training/improving-layouts/smooth-scrolling.html http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html