设置图像位图后,Android App冻结

时间:2016-05-04 13:07:47

标签: java android image bitmap freeze

我正在开发一款具有背景图片的应用。因为这个图像非常大,所以我使用了一些代码来确保它足够小,不会让应用程序耗尽内存。但是在将位图设置为ImageView之后,应用程序会冻结并开始跳过帧。当我按下按钮时,没有任何反应,没有动画,也没有执行任何方法。这是我的代码:

package com.github.legosteen11.testing;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView mImg;
        mImg = (ImageView) findViewById(R.id.backgroundImage);
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;
        mImg.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.background_1, width, height));
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
}

这是我的XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.github.legosteen11.testing.MainActivity">

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="16dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:textSize="45sp"
            android:textAlignment="center"
            android:text="Welcome!"/>

        <TextView
            android:layout_weight="0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:textSize="24sp"
            android:text="How long will you be away?"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginBottom="16dp">



            <TextView
                android:id="@+id/amountDays"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:textSize="56sp"
                android:text="0" />

            <TextView
                android:id="@+id/daysTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/amountDays"
                android:text="days"
                android:layout_toRightOf="@id/amountDays"
                android:textSize="24sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/continueDaysButton"
                android:text="Continue"
                android:layout_centerHorizontal="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="helloWorld"/>

        </RelativeLayout>


    </LinearLayout>



</RelativeLayout>

编辑: logcat,没有错误,但感谢这里的评论:)

05-04 14:58:32.354 18034-18034/com.github.legosteen11.testing W/System: ClassLoader referenced unknown path: /data/app/com.github.legosteen11.testing-2/lib/arm64
05-04 14:58:32.401 18034-18046/com.github.legosteen11.testing W/art: Suspending all threads took: 22.270ms
05-04 14:58:32.403 18034-18046/com.github.legosteen11.testing I/art: Background sticky concurrent mark sweep GC freed 1969(115KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 19MB/19MB, paused 23.077ms total 41.996ms
05-04 14:58:32.643 18034-18034/com.github.legosteen11.testing W/System: ClassLoader referenced unknown path: /data/app/com.github.legosteen11.testing-2/lib/arm64
05-04 14:58:32.758 18034-18034/com.github.legosteen11.testing W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-04 14:58:33.695 18034-18095/com.github.legosteen11.testing D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-04 14:58:33.748 18034-18095/com.github.legosteen11.testing I/Adreno: QUALCOMM build                   : 63c06b2, I8366cd0437
                                                                        Build Date                       : 12/06/15
                                                                        OpenGL ES Shader Compiler Version: XE031.05.13.02
                                                                        Local Branch                     : mybranch17112971
                                                                        Remote Branch                    : quic/LA.BF64.1.2.9_v2
                                                                        Remote Branch                    : NONE
                                                                        Reconstruct Branch               : NOTHING
05-04 14:58:33.753 18034-18095/com.github.legosteen11.testing I/OpenGLRenderer: Initialized EGL, version 1.4
05-04 14:58:35.205 18034-18034/com.github.legosteen11.testing I/Choreographer: Skipped 84 frames!  The application may be doing too much work on its main thread.
05-04 14:58:36.561 18034-18034/com.github.legosteen11.testing I/Choreographer: Skipped 80 frames!  The application may be doing too much work on its main thread.
05-04 14:58:37.956 18034-18034/com.github.legosteen11.testing I/Choreographer: Skipped 83 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

只需在不同的线程上执行代码即可 它会加载到不同的线程上,这样你的ui就不会有任何负载 在onCreate之外将其声明为全局

ImageView mImg;

    public class DownloadFromServer extends AsyncTask<String, Void, Bitmap> {
            Context mContext;

            public DownloadFromServer(Context context) {
                mContext = context;
            }


            @Override
            protected void onPostExecute(Bitmap s) {
                super.onPostExecute(s);
                 mImg.setBitmap(s);             
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected ScData doInBackground(String... params) {
              Bitmap bmp=decodeSampledBitmapFromResource(getResources(), R.drawable.background_1, width, height);
                return bmp;
            }
        }