當我回顧我見過的代碼TextView
和ImageView
每次調用getView
時都會被誇大。通貨膨脹是一項非常昂貴的任務,所以我們希望儘可能避免使用它。
之前別的確保你會在你的搖籃聲明它
compile 'com.android.support:support-v4:21.0.0'
添加這個輔助類的VideoFilesAdapter.java
class Helper {
public TextView textView;
public ImageView image;
}
然後,我們將使用LRUcache在存儲圖像,這樣我們就可以使用這個圖像一遍又一遍,沒有做任何事情。
在項目中添加MyLRU.java
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class MyLRU {
private LruCache<String, Bitmap> cache = null;
private static MyLRU myLru = null;
private MyLRU() {
int availableMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
cache = new LruCache<String, Bitmap>(availableMemory/8) {
@Override
protected int sizeOf(String key, Bitmap value) {
// this is the only way to get the Bitmap size below API 12
return (value.getRowBytes() * value.getHeight())/1024;
}
};
}
public static MyLRU getInstance() {
if (myLru == null) {
myLru = new MyLRU();
}
return myLru;
}
public void addImage(String key, Bitmap image) {
myLru.addImage(key, image);
}
public Bitmap getImage(String key) {
return myLru.getImage(key);
}
}
在VideoFilesAdapter.java
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth/sourceWidth;
float yScale = (float) newHeight/sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth)/2;
float top = (newHeight - scaledHeight)/2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
添加一個實用的方法在這裏是你的VideoFilesAdapter.java
public class VideoFilesAdapter extends ArrayAdapter<String> {
private List<String> mpath;
private Context mContext;
public static ArrayList<String> mSelectedPaths = null;
private MyLRU lruCache;
public VideoFilesAdapter(Context context, List<String> path) {
super(context, R.layout.fileadapter_list, path);
this.mContext = context;
this.mpath = path;
// My LRU
lruCache = MyLRU.getInstance();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.fileadapter_list, null);
Helper h = new Helper();
h.textView = (TextView) convertView.findViewById(R.id.txt);
h.textView = (ImageView) convertView.findViewById(R.id.img);
v.setTag(h);
} else {
v = convertView;
}
Helper myHelper = (Helper) v.getTag();
File file = new File(mpath.get(position));
String fullPath = file.getAbsolutePath();
if (file.exists()) {
myHelper.textView.setText(file.getName());
Bitmap cacheImage = lruCache.getImage(fullPath);
if (cacheImage == null) {
Bitmap bm = scaleCenterCrop(ThumbnailUtils.createVideoThumbnail(fullPath, MediaStore.Video.Thumbnails.MICRO_KIND),100,100);
lruCache.addImage(fullPath, bm);
cacheImage = bm;
}
myHelper.image.setImageBitmap(cacheImage);
}
return v;
}
class Helper {
public TextView textView;
public ImageView image;
}
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth/sourceWidth;
float yScale = (float) newHeight/sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth)/2;
float top = (newHeight - scaledHeight)/2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
}
完全修改正如你可以看到我暫時取消getImageUri
方法和畢加索,因爲它已經沒有意義了,因爲我們已經有一個緩存,它是LRU緩存。
我希望這將提高應用性能:)
你的代碼的問題是,你的膨化的TextView和ImageView的細胞每次視圖被回收,基本上就會放慢您的滾動體驗。 – neferpitou
那麼這個解決方案是什麼? @Dragon –
林編碼現在,我可能會在明天寄給你:)我會盡我所能解釋每一個部分。 – neferpitou