活动运行缓慢,几个ImageView-s
我有一个活动,共包含4个图像。它们都与1080x1920设备的分辨率相匹配。当我使用这些图像(这些图像通过XML直接加载到我的活动中)运行该活动时,它会在我的Genymotion模拟器中运行极其缓慢的,并且在真实的Android设备上运行滞后。
这里是设置:活动运行缓慢,几个ImageView-s
<android.support.design.widget.AppBarLayout
...>
<android.support.design.widget.CollapsingToolbarLayout
...>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
app:layout_collapseMode="parallax">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/shot_header"
android:scaleType="centerCrop" />
</LinearLayout>
<android.support.v7.widget.Toolbar
.../>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
第一图像处于CollapsingToolbarlayout。该图像的分辨率为1080x649 PNG。
的content_activity:
此图片填充母width.It的分辨率为1080x772 PNG。
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/main_image"
android:layout_below="@+id/shot_error_field"
android:src="@drawable/forehand_midpng"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_marginTop="15dp"/>
其他2个图像是在一个的LinearLayout,其分辨率为500x399
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_image">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView3"
android:src="@drawable/forehand_mid_wrong"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView4"
android:src="@drawable/forehand_mid_wrong"
android:layout_weight="1"/>
</LinearLayout>
总之,我有4个ImageViews的活动,填入适当大小的图像,这应该现代Android设备没有问题。问题是由于高内存消耗,此活动运行非常缓慢且滞后。
我做错了什么?我怎样才能进一步优化这些图像?
我看着其他线程-out of memory issue,但似乎没有提出解决这样的问题。
问题是图像的resolution
,如果你可以将图像的reduce resolution
然后工作罚款这里是减少image resolution
ans大小的一些例子。
如果您传递位图宽度和高度,则使用下面的函数。
public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
int bitmapHeight) {
return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
true);
}
如果您想要位图比例相同并减少位图大小。然后传递最大尺寸的位图。您可以使用此功能
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width/(float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width/bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
,或者如果您使用的是绘图资源,然后用这个方法
public Drawable resizeImage(int imageResource) {// R.drawable.icon
// Get device dimensions
Display display = getWindowManager().getDefaultDisplay();
double deviceWidth = display.getWidth();
BitmapDrawable bd = (BitmapDrawable) this.getResources().getDrawable(
imageResource);
double imageHeight = bd.getBitmap().getHeight();
double imageWidth = bd.getBitmap().getWidth();
double ratio = deviceWidth/imageWidth;
int newImageHeight = (int) (imageHeight * ratio);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageResource);
Drawable drawable = new BitmapDrawable(this.getResources(),
getResizedBitmap(bMap, newImageHeight, (int) deviceWidth));
return drawable;
}
/************************ Resize Bitmap *********************************/
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
优秀! 'resizeImage()'方法可行地工作于 – BabbevDan
@DannyBabbev。感谢buddy接受我的答案。 –
TRY
- 绝对降低图像的大小。
- 如果可以,缓存图像。
- 将图像下载到其他线程上。存储一个HashMap将 很容易让你
当你得到图像的URL列表,遍历它们:
pictures.put(id,view);
try{
FileInputStream in = openFileInput(id);
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeStream(in, null, null);
view.setImageBitmap(bitmap);
}catch(Exception e){
new Thread(new PictureGetter(this,mHandler,id)).start();
}
代码来更新图像视图:
if(id!=null){
ImageView iv = pictures.get(id);
if(iv!=null){
try{
FileInputStream in = openFileInput(id);
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeStream(in, null, null);
iv.setImageBitmap(bitmap);
}catch(Exception e){
}
}
我的图片尺寸很小。最大的是** 351 KB ** – BabbevDan
这就是原因。它应该是平稳的工作,而不是那个 –
尝试Facebook的壁画库。它可以处理大型图像和我的项目之一,减少大约50%的内存消耗。
其实这不应该是一个问题,这些图像是不是很大,让你手机laggy。查看应用程序中的其他内容,可能会在UI线程中出现一些繁重的操作(如数据库写入/读入,API请求)。
如果没有这样的操作,并且您看到了性能问题,请尝试通过一些库(如Picasso)设置这些图像。
摇篮依赖性:
compile 'com.squareup.picasso:picasso:2.4.0'
和代码如下所示:
Picasso.with(this).load(R.drawable.my_image).into(toolbar);
兄弟!你说1080x1920不是问题!真?在任何标准清晰度设备中检查分辨率为1080x1920的任何图像,然后让我知道会发生什么 –
首先,我不是你的兄弟,伙伴。其次 - 你读过这个问题了吗?他有一个分辨率为1080x1920的现代Android设备,而不是图像。优化总是需要的,但总是有可能在UI线程中有更重要的问题,队友。 –
ImageView的一般不喜欢大的图像。 –
你用'android:largeHeap =“true”'尝试吗?这是不值得推荐的,但如果没有任何作用,它可能是有帮助的。 – Stanojkovic
问题是图像分辨率..善意调整大小 –