几个ImageView-

时间:2016-02-05 10:58:04

标签: android performance 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>

第一张图片位于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"/>

其他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个ImageView的活动,其中填充了大小合适的图像,对于现代Android设备来说应该没问题。问题是由于高内存消耗,此活动运行速度极慢且滞后。

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

我查看了其他主题 - out of memory issue,但似乎没有人提出解决这个问题的方法。

4 个答案:

答案 0 :(得分:13)

问题是图片的resolution,如果您可以reduce resolution图片然后正常工作,这里有一些减少image resolution和尺寸的示例。

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

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

答案 1 :(得分:0)

尝试

  1. 绝对减小图像的大小。
  2. 如果可以,请缓存图片。
  3. 在不同的主题上下载图像。存储HashMap会 让你轻松
  4. 获得图片网址列表后,请仔细阅读:

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

答案 2 :(得分:0)

试试facebook的壁画库。它可以处理大型图像和我的一个项目,减少大约50%的内存消耗。

答案 3 :(得分:0)

实际上这不应该是一个问题,这些图像并不是那么大,以至于让你的电话迟钝。看看你在应用程序中的其他东西,可能在UI线程中有一些繁重的操作(如DB编写/读取,API请求)。

如果没有这样的操作并且您看到性能问题,请尝试通过一些库(如Picasso)设置这些图像。

Gradle依赖:

compile 'com.squareup.picasso:picasso:2.4.0'

代码看起来像这样:

Picasso.with(this).load(R.drawable.my_image).into(toolbar);