Gridview从系统库中平滑滚动和图像填充

时间:2013-10-23 19:49:07

标签: android android-gridview android-gallery android-cursoradapter smooth-scrolling

我创建了一个画廊。我从一个简单的游标适配器(自定义实现)填充gridview。

第一个问题是gridview在滚动时卡在中间。我认为这是因为图像在用户交互中不断地绑定到视图。如何实现平滑滚动?

其次,我没有通过Cursor从画廊中获取所有图像。我需要做什么才能获得所有图像。我还观察到此代码不会在不同的设备上返回图像,例如Samsung Galaxy s2。但在Samsung Note 2和Nexus 4上工作正常。 URI虽然相同:content:// media / external / images / thumbnails

适配器代码:

/**  
 * @author Syed Ahmed Hussain
 */
public class CustomGalleryAdapter extends SimpleCursorAdapter {

    private ContentResolver mContentResolver;
    private LayoutInflater mLayoutInflater;
    private int mImageIdColumnIndex;
    private ImageView mImageView;
    private Context mContext;
    private int mLayout;

    public CustomGalleryAdapter(Context pContext, int pLayout, Cursor pCursor, String[] pFrom, int[] pTo, int pFlags) {
        super(pContext, pLayout, pCursor, pFrom, pTo, pFlags);
        mLayout             = pLayout;
        mContext            = pContext;
        mLayoutInflater     = LayoutInflater.from(mContext);
        mContentResolver    = mContext.getContentResolver();
        mImageIdColumnIndex = pCursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
    }

    @Override
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) {
        return mLayoutInflater.inflate(mLayout, null);
    }

    @Override
    public void bindView(View pView, Context pContext, Cursor pCursor) {
        long id = pCursor.getLong(mImageIdColumnIndex);
        mImageView = (ImageView) pView;
        mImageView.setScaleType(ScaleType.CENTER_CROP);
        mImageView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, new Options()));
    }

}

活动代码:

public class GalleryActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private String[] projection = { MediaStore.Images.Thumbnails.IMAGE_ID };
    private CustomGalleryAdapter mCustomGalleryAdapter;
    private String[] selectionArgs = null;
    private String selection = "";
    private GridView mGridView;

    @Override
    protected void onCreate(Bundle pSavedInstanceState) {
        super.onCreate(pSavedInstanceState);
        setContentView(R.layout.view_gallery);
        mGridView = (GridView) findViewById(R.id.gridView);
        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int pId, Bundle pArgs) {

        return new android.support.v4.content.CursorLoader(getApplicationContext(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> pLoader, Cursor pData) {
        mCustomGalleryAdapter = new CustomGalleryAdapter(getApplicationContext(), R.layout.gallery_item, pData, projection, new int[] { R.id.galleryImageView }, 0);
        mGridView.setAdapter(mCustomGalleryAdapter);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> pLoader) {

    }

}

View_Galler.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:layoutMode="opticalBounds"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:listSelector="#00000000"
    android:id="@+id/gridView" />

gallery_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/galleryImageView"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

任何人都可以指导我做错了什么吗?我该如何解决这些问题?

2 个答案:

答案 0 :(得分:3)

以下是您可以尝试的改进:

  1. 使用ArrayAdapter。
  2. 使用ViewHolder模式重用网格单元格中的视图并在getView中重复使用convertview是非常的(**)
  3. 避免在适配器getView中实例化unnessary对象。
  4. 如果您使用LazyLoading技术和Async任务从互联网下载,则缓存imga。
  5. 当您使用本地资源的图像时,使用压缩图像。
  6. 机器人:fastScrollEnabled = “真”

答案 1 :(得分:1)

我建议你使用通用图片加载器

https://github.com/nostra13/Android-Universal-Image-Loader

还使用: 你的清单中android:largeHeap="true"

在GridView(XML)android:fastScrollEnabled="true"

中声明此属性