使用几个ImageView-s,Activity运行缓慢

Bab*_*Dan 7 performance android imageview

我的活动总共包含4张图片.它们都与1080x1920设备的分辨率相匹配.当我使用那些通过XML直接加载到我的活动中的图像运行活动时,它在我的Genymotion模拟器中运行得非常慢并且落在真正的Android设备上.

这是设置:

<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>
Run Code Online (Sandbox Code Playgroud)

第一张图片位于CollapsingToolbarlayout中.图像的分辨率为1080x649 PNG.

content_activity:
此图像填充父宽度.它的分辨率为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"/>
Run Code Online (Sandbox Code Playgroud)

其他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>
Run Code Online (Sandbox Code Playgroud)

总而言之,我有一个带有4个ImageView的活动,填充了适当大小的图像,对于现代Android设备来说应该没问题.问题是由于高内存消耗,此活动运行速度极慢且滞后.

难道我做错了什么?如何进一步优化这些图像?

我查看了其他线程- 内存不足的问题,但似乎没有人提出解决这个问题的方法.

Bha*_*ani 13

问题resolution出在图像上,如果你可以reduce resolution对图像进行正常工作,这里有一些减少image resolution尺寸的例子.

如果您传递位图宽度和高度,请使用以下函数.

    public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
            int bitmapHeight) {
        return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
                true);
    } 
Run Code Online (Sandbox Code Playgroud)

如果您希望位图比率相同并减少位图大小.然后传递你的最大尺寸位图.你可以使用这个功能

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);
}
Run Code Online (Sandbox Code Playgroud)

或者如果您使用可绘制资源,则使用此方法

public Drawable resizeImage(int imageResource) {// R.drawable.large_image
    // 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 优秀!`resizeImage()`方法非常有效 (2认同)