高效位图
Efficient Bitmaps
最近,在尝试在 imageView 中加载图像时,我 运行 遇到了 "Out of Heap Memory" 错误。于是上网查了下图片优化方法
我期待找到优化位图的最佳方法。
目前,我有一个 ImageView,其高度为 200dp,宽度为 "match_parent"。基本上它水平填充屏幕并垂直占据 200dp。
我正在努力实现类似这张图片的效果。
这是我的总体规划。
1) 为 1 行创建布局(ImageView、TextView、黑色 T运行文本后面的半透明条)
2) 使用 RecyclerView 并填充数据
目前我卡在第 1 步(对代码不满意)。
我所做的是为该行创建一个 XML 布局。
<RelativeLayout
android:id="@+id/layout_adventure_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/imageView_adventure_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:background="@drawable/textview_background_gradient"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:id="@+id/textView_adventure_name_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="Demo Text"/>
</RelativeLayout>
我稍后会根据排版指南调整 TextView。
好的,基本上 ImageView 是 200dp 高。
为了压缩我的图像,我使用了 Android 培训页面上的指南,该页面使用 inSampleSize 并在 AsyncTask 中加载图像。
为了计算所需的高度,我使用了一些我不确定它是否正确的方法。由于我只想为高度压缩图像,所以我只通过了高度来计算 inSampleSize。
DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
int density = disp.densityDpi;
width = disp.widthPixels;
height = (int)((float)(200.0/160.0) * (float)density);
Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
loadBitmap(R.drawable.shake,iV);
最后,我从最终位图中提取了所需宽度和高度的缩略图,以向用户显示填充 imageView 而不是仅位于带有边距的中心的图像。
imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));
完整代码可以在这里找到:http://pastebin.com/mhKi1sKd
优化 imageView 的最佳方法是什么,以便在 RecyclerView 中显示多个图像时,堆内存不会变满,并且如果我的程序中有不必要的代码只会占用处理和宝贵的时间?
这是我到现在为止的成就。
看看 Picasso
图书馆。
Many common pitfalls of image loading on Android are handled
automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
最近,在尝试在 imageView 中加载图像时,我 运行 遇到了 "Out of Heap Memory" 错误。于是上网查了下图片优化方法
我期待找到优化位图的最佳方法。
目前,我有一个 ImageView,其高度为 200dp,宽度为 "match_parent"。基本上它水平填充屏幕并垂直占据 200dp。
我正在努力实现类似这张图片的效果。
这是我的总体规划。
1) 为 1 行创建布局(ImageView、TextView、黑色 T运行文本后面的半透明条)
2) 使用 RecyclerView 并填充数据
目前我卡在第 1 步(对代码不满意)。
我所做的是为该行创建一个 XML 布局。
<RelativeLayout
android:id="@+id/layout_adventure_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/imageView_adventure_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:background="@drawable/textview_background_gradient"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:id="@+id/textView_adventure_name_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="Demo Text"/>
</RelativeLayout>
我稍后会根据排版指南调整 TextView。
好的,基本上 ImageView 是 200dp 高。 为了压缩我的图像,我使用了 Android 培训页面上的指南,该页面使用 inSampleSize 并在 AsyncTask 中加载图像。
为了计算所需的高度,我使用了一些我不确定它是否正确的方法。由于我只想为高度压缩图像,所以我只通过了高度来计算 inSampleSize。
DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
int density = disp.densityDpi;
width = disp.widthPixels;
height = (int)((float)(200.0/160.0) * (float)density);
Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
loadBitmap(R.drawable.shake,iV);
最后,我从最终位图中提取了所需宽度和高度的缩略图,以向用户显示填充 imageView 而不是仅位于带有边距的中心的图像。
imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));
完整代码可以在这里找到:http://pastebin.com/mhKi1sKd
优化 imageView 的最佳方法是什么,以便在 RecyclerView 中显示多个图像时,堆内存不会变满,并且如果我的程序中有不必要的代码只会占用处理和宝贵的时间?
这是我到现在为止的成就。
看看 Picasso
图书馆。
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching.