Android GridView滚动不平滑
我有一个从sd卡加载图像的gridview…。问题是滚动一点也不平滑,我不知道它有什么问题…有人能帮我解决吗Android GridView滚动不平滑,android,gridview,scroll,imageview,Android,Gridview,Scroll,Imageview,我有一个从sd卡加载图像的gridview…。问题是滚动一点也不平滑,我不知道它有什么问题…有人能帮我解决吗 private Cursor cursor; private int columnIndex; private GridView gridView; ProgressDialog pd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
private Cursor cursor;
private int columnIndex;
private GridView gridView;
ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridview);
new LoadImages().execute();
/**
* Adapter for our image files.
*/
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(5, 5, 5, 5);
picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));
}
else {
picturesView = (ImageView)convertView;
}
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image
picturesView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
imageID, MediaStore.Images.Thumbnails.MINI_KIND, null));
return picturesView;
}
}
我不知道这个函数作为后台任务是如何工作的!!!!它只是工作!!有什么见解吗
private void LoadImagesFromSDCard(){
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); //to update the database or rescan the database to get the captured image
//delay given for the sdcard to reload as per the above statement
try {
Thread.sleep(300L);
}
catch (Exception e) {}
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Images.Media._ID);
// Get the column index of the Media Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
}
这是我的任务
class LoadImages extends AsyncTask<String, String, String> {
ProgressDialog progDailog = new ProgressDialog(PrintBrushActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
progDailog.setMessage("Loading.....");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.show();
}
@Override
protected String doInBackground(String... aurl) {
//do something while spinning circling show
LoadImagesFromSDCard();
return null;
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
gridView.setAdapter(new ImageAdapter(PrintBrushActivity.this));
progDailog.dismiss();
}
}
}
class LoadImages扩展了异步任务{
ProgressDialog progDailog=新建ProgressDialog(PrintBrushActivity.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setMessage(“加载…”);
progDailog.setUndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_微调器);
progDailog.show();
}
@凌驾
受保护的字符串背景(字符串…aurl){
//旋转旋转时做些什么
loadImagesFromsCard();
返回null;
}
@凌驾
受保护的void onPostExecute(字符串未使用){
super.onPostExecute(未使用);
setAdapter(新的ImageAdapter(PrintBrushActivity.this));
progDailog.disclose();
}
}
}
在我的图库中,我使用MediaStore.Images.Thumbnails.MICRO\u KIND而不是MediaStore.Images.Thumbnails.MINI\u KIND
我还使用了一个缓存,也就是说,一个HashMap,在这里我保存加载的缩略图,如果我需要再次显示相同的缩略图,我将从缓存中加载它,不再生成它。我的缓存是:
cache = new HashMap<Integer, SoftReference<Bitmap>>();
cache=newhashmap();
其中,在整数位置保存图像id,在软参考位置保存缩略图位图。这两件事让我的应用程序更流畅
祝你好运 我也遇到了同样的问题,我在另一个线程中看到了这个答案,我尝试了一下,结果好多了 简而言之,创建一个
ListView
,然后自己编程创建列,这样您就创建了GridView
。
主要问题是SDK中的
Gridview
,速度有点慢您可以像这样使用Android Universal Image Loader主库
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
final ProgressBar spinner = (ProgressBar)findViewById(R.id.loading_image_fromsdcard);
if (convertView == null) {
picturesView = new ImageView(context);
picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
picturesView.setLayoutParams(new LayoutParams(250, 250));
}
else {
picturesView = (ImageView)convertView;
}
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
//picturesView.setImageURI(Uri.withAppendedPath( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
imageLoader.displayImage(Uri.withAppendedPath( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID).toString(), picturesView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(Sdcard.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
return picturesView;
}
}
请详细解释一下。谢谢