我尝试使用调整大小的图像制作一个简单的GridView,因此我获得了极高的设备内存使用率(43.57 MB)我在这里做错了什么?
我的GridView片段:
public class GalleryFragment extends Fragment {
private ArrayList<String> mPathList;
private ImageView mImageView;
private GalleryAdapter mAdapter;
private GridView mGridView;
private static final String TAG = "GalleryFragment";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gallery_layout, container, false);
new AsyncGalleryLoader().doInBackground();
mGridView = (GridView) view.findViewById(R.id.gridView);
mAdapter = new GalleryAdapter(mPathList);
mGridView.setAdapter(mAdapter);
return view;
}
private class GalleryAdapter extends ArrayAdapter<String>{
public GalleryAdapter(ArrayList<String> items) {
super(getActivity(), 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.gallary_item, parent, false);
}
if (mPathList.size() > 0){
String path = mPathList.get(position);
mImageView = (ImageView) convertView.findViewById(R.id.gallary_item_imageView);
BitmapDrawable bitmap = PictureUtils.getScaledDrawable(getActivity(), path);
mImageView.setImageDrawable(bitmap);
}
return convertView;
}
}
private class AsyncGalleryLoader extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... params) {
List<PhotoData> datas = PhotoData.listAll(PhotoData.class);
mPathList = new ArrayList<String>();
for (int i = 1; i<datas.size(); i++){
try{
String path = datas.get(i).getPath();
File file = new File(path);
if (file.exists()){
mPathList.add(path);
}else {
datas.get(i).delete();
}
}catch (Exception e){
Log.d(TAG, "Error");
}
}
return mPathList;
}
@Override
protected void onPostExecute(ArrayList<String> strings) {
super.onPostExecute(strings);
}
}
}
我用来调整设备图像大小的类:
public class PictureUtils {
public static BitmapDrawable getScaledDrawable(Activity activity, String path){
Display display = activity.getWindowManager().getDefaultDisplay();
float bestWidth = display.getWidth();
float bestHeight = display.getHeight();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if(srcHeight > bestHeight || srcWidth > bestWidth){
if(srcWidth > srcHeight){
inSampleSize = Math.round(srcHeight / bestHeight);
}else {
inSampleSize = Math.round(srcWidth / bestWidth);
}
}
options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return new BitmapDrawable(activity.getResources(), bitmap);
}
public static void cleanImageView(ImageView view){
if (!(view.getDrawable() instanceof BitmapDrawable))
return;
BitmapDrawable drawable = (BitmapDrawable) view.getDrawable();
drawable.getBitmap().recycle();
view.setImageDrawable(null);
}
}
答案 0 :(得分:0)
问题是您的getScaledDrawable()
方法获取位图并将其缩放到设备屏幕的大小。据我所知,你真正需要的是只获取应该是网格视图中单个项目大小的缩略图。因此,只需重写它以获取网格项public static BitmapDrawable getScaledDrawable(Activity activity, String path, int bestWidth, int bestHeight)
的大小。