使用几个 ImageView-s 时活动运行缓慢 [英] Activity runs slow with a couple of ImageView-s

查看:12
本文介绍了使用几个 ImageView-s 时活动运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是设置:

I have an activity containing 4 images in total. They are all matching the resolution of a 1080x1920 device. When I run the activity with those images, which are loaded directly in my activity via the XML, it runs tremendously slow in my Genymotion emulator and lags on a real Android device.

Here is the setup:

<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>

第一张图片位于 CollapsingToolbar 布局中.图片的分辨率为 1080x649 PNG.

内容活动:
此图像填充父宽度.其分辨率为 1080x772 PNG.

The first image is in a CollapsingToolbarlayout. The resolution of the image is 1080x649 PNG.

The content_activity:
This image fills the parent width.It's resolution is 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

The other 2 images are in a LinearLayout, their resolution is 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 个 ImageView 的活动,其中填充了适当大小的图像,这对于现代 Android 设备应该没有问题.问题是由于高内存消耗,此活动运行非常缓慢且滞后.

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

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

To summarize, I have an activity with 4 ImageViews, populated with properly sized images, which should no problem for a modern Android device. The problem is that this activity is running extremely slow and lagging due to a high memory consumption.

Am I doing something wrong? How can I further optimize those images?

I looked into other threads- out of memory issue but none seems to propose a solution to such a problem.

推荐答案

问题是图片的分辨率,如果你可以降低图片的分辨率就可以了,这是一些减少图像分辨率和尺寸的例子.

Problem is resolution of the image, if you can reduce resolution of the image then work fine, here is some example for reducing image resolution and size.

如果你传递位图的宽度和高度,那么使用下面的函数.

If you pass bitmap width and height then use below function.

    public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
            int bitmapHeight) {
        return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
                true);
    } 

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

if you want bitmap ratio same and reduce bitmap size. then pass your maximum size bitmap. you can use this function

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);
}

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

or if you are using drawable resources then use this method

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;
}

这篇关于使用几个 ImageView-s 时活动运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆