Android Gridview滚动速度慢
我正在尝试创建一个画廊应用程序,只是为了向用户显示图像 该应用程序已在运行,但网格滚动速度慢且不平滑 我不知道我还能做些什么来提高性能。我已经在使用LruCache和Async任务来加载图像 网格适配器Android Gridview滚动速度慢,android,Android,我正在尝试创建一个画廊应用程序,只是为了向用户显示图像 该应用程序已在运行,但网格滚动速度慢且不平滑 我不知道我还能做些什么来提高性能。我已经在使用LruCache和Async任务来加载图像 网格适配器 package abcde.xyz; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.support.v4.util.LruC
package abcde.xyz;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by Lucas on 25/03/2017.
*/
public class GridImagesAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();
public static LruCache<Integer, Bitmap> mMemoryCache;
public GridImagesAdapter(Context context, int layoutResourceId, ArrayList<Integer> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 4;
if(mMemoryCache == null){
mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected int sizeOf(Integer key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};
}
mMemoryCache.evictAll();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
ImageView image = (ImageView)row.findViewById(R.id.image);
final Integer item = (Integer)data.get(position);
image.setImageBitmap(null);
ImageGridHandler handler = new ImageGridHandler(context, image);
handler.execute(item);
}
return row;
}
}
包abcde.xyz;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.support.v4.util.LruCache;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入java.util.ArrayList;
/**
*由Lucas于2017年3月25日创建。
*/
公共类GridImagesAdapter扩展了ArrayAdapter{
私人语境;
私人内部布局资源;
private ArrayList data=new ArrayList();
公共静态LruCache-mMemoryCache;
公共GridImagesAdapter(上下文上下文、int-layoutResourceId、ArrayList数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
最终int cacheSize=maxMemory/4;
if(mMemoryCache==null){
mMemoryCache=新的LruCache(缓存大小){
@凌驾
受保护的int sizeOf(整数键、位图){
返回bitmap.getByteCount()/1024;
}
};
}
mMemoryCache.executeAll();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
ImageView image=(ImageView)row.findViewById(R.id.image);
最终整数项=(整数)数据。获取(位置);
image.setImageBitmap(空);
ImageGridHandler=新的ImageGridHandler(上下文,图像);
执行(项目);
}
返回行;
}
}
ImageGridHandler
package abcde.xyz;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
/**
* Created by Lucas on 29/03/2017.
*/
public class ImageGridHandler extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Context context;
public ImageGridHandler(Context context, ImageView img){
imageViewReference = new WeakReference<ImageView>(img);
this.context = context;
}
@Override
protected Bitmap doInBackground(Integer... params) {
return loadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
final ImageView imageView = imageViewReference.get();
if(imageView != null) {
//imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageBitmap(result);
}
}
public void addBitmapToMemoryCache(Integer key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
GridImagesAdapter.mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(Integer key) {
return GridImagesAdapter.mMemoryCache.get(key);
}
public Bitmap loadBitmap(Integer resId) {
Bitmap bitmap = getBitmapFromMemCache(resId);
if (bitmap == null) {
Log.d("ImageGridHandler", "Loading: " + resId);
if(resId != null) {
bitmap = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(), resId,
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
if(bitmap != null) {
addBitmapToMemoryCache(resId, bitmap);
}
}
Log.d("GRIDADAPTER", "ImageLoaded");
}
return bitmap;
}
}
包abcde.xyz;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.provider.MediaStore;
导入android.util.Log;
导入android.widget.ImageView;
导入java.lang.ref.WeakReference;
/**
*由Lucas于2017年3月29日创建。
*/
公共类ImageGridHandler扩展异步任务{
私有最终WeakReference imageViewReference;
私人语境;
公共ImageGridHandler(上下文,ImageView img){
imageViewReference=新的WeakReference(img);
this.context=上下文;
}
@凌驾
受保护位图doInBackground(整数…参数){
返回loadBitmap(参数[0]);
}
@凌驾
受保护的void onPostExecute(位图结果){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
//imageView.setScaleType(imageView.ScaleType.CENTER);
设置图像位图(结果);
}
}
public void addBitmapToMemoryCache(整数键、位图){
if(getBitmapFromMemCache(key)==null){
GridImagesAdapter.mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(整数键){
返回GridImagesAdapter.mMemoryCache.get(键);
}
公共位图加载位图(整数剩余){
位图位图=getBitmapFromMemCache(resId);
如果(位图==null){
Log.d(“ImageGridHandler”,“加载:”+resId);
如果(剩余!=null){
位图=MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(),resId,
MediaStore.Image.Thumbnails.MICRO_类,
无效);
if(位图!=null){
addBitmapToMemoryCache(剩余,位图);
}
}
Log.d(“GRIDADAPTER”、“ImageLoaded”);
}
返回位图;
}
}
布局
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="umdev.images24.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefresh"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:focusable="true"
android:clickable="true"
android:fastScrollEnabled="true"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
grid\u item\u layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:background="#fff">
<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp" />
</LinearLayout>
因为没有缓存,所以它不是快速平滑的。我有同样的问题,我使用通用图像加载器,使用这个库来调整网格视图中的图像大小 它使用url获取图像,然后将其转换为位图,然后调整其大小。所以如果你想创建一个简单的图像库。然后,您可以像现在一样获取sd卡图像,并将其传递给universal image loader的方法ImageLoader() 比如说-
String path="file://"+String.valueOf(wholeList.get(i));//Your Image path
ImageLoader.getInstance().displayImage(path,holder.image,options,new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}});
return convertView;
}
private static class ViewHolder {
ImageView image;
ProgressBar progressBar;
}
**注意:这不是完整的代码,它只是其中的一部分,我建议您先检查Git hub链接**-
您可以使用Glide、Picassa或ImageLoader加载图像并避免
由于在映像中加载映像所花费的时间而发生的滚动问题
看法
例如在滑翔中
Glide.with(getActivity()).load(Uri.fromFile(new File(String.valueOf(getItem(position))))).into(ivPhoto);
在加载图像之前,我们必须调整其大小以避免ImageView缩放 将缩小的版本加载到内存中
感谢@GT调整添加到您的应用程序中的图像的大小library@GT,这是来自相机的图像,我无法调整大小。我已经在加载最小的MICRO_类,即96x96px缩略图。谢谢@GT。这是关于调整大小的。