2016-05-04 4 views
0

Ich arbeite an einer App, die ein Hintergrundbild hat. Da dieses Bild sehr groß ist, habe ich etwas Code verwendet, um sicherzustellen, dass es so klein ist, dass die App nicht zu viel Speicher hat. Aber nachdem die Bitmap auf das ImageView gesetzt wurde, friert die App ein und startet das Überspringen von Frames. Wenn ich auf einen Knopf drücke, passiert nichts, keine Animation und keine Ausführung irgendeiner Methode. Hier ist mein Code:Android App einfrieren nach der Einstellung Bild Bitmap

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

Hier ist meine 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> 

edit: logcat gibt es keine Fehler, aber danke für die Kommentare hier ist :)

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. 
+0

Add logcat für die beste Lösung. –

+0

post logcat Fehler –

Antwort

0

Führen Sie einfach Ihren Code auf anderen Thread Es wird auf anderen Thread laden, so dass Ihr UI keine Last haben wird.
erklären dies außerhalb von onCreate als globale

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