2012-08-28 1 views
0

Я создаю приложение для живых обоев, которое отлично работает на устройствах xhdpi и ldpi, но проблема с закрытием силы в hdpi и на основе mdpi.Live wallpaper get force close issue

Он загружает изображение в экран предварительного просмотра, когда мы нажимаем «установить обои», он разбивается и даже когда мы меняем настройки из приложения.

Выше два условия делают приложение, чтобы заставить закрыть.

Похоже OutOfMemoryError

Ошибка не соответствует

Вот мой код:

pixeltwo.java

public class pix11two extends SurfaceView { 
private pix11three three; 
int fps; 
Bitmap mBitmap; 
int country_flag; 

public pix11two(Context context, int fps, int country) { 
    super(context); 
    this.fps = fps; 

    country_flag = country; 

    DisplayMetrics displayMetrics = new DisplayMetrics(); 
    displayMetrics = context.getResources().getDisplayMetrics(); 
    int displayWidth = displayMetrics.widthPixels; 
    int displayHeight = displayMetrics.heightPixels; 

    if (mBitmap != null) 
     mBitmap.recycle(); 

    BitmapFactory.Options options = new BitmapFactory.Options(); 

    options.inSampleSize = 1; 

    options.inPurgeable = true; 

    if (country_flag > 1) { 

     if (country_flag == 2) { 

      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.yellow, options); 
     } 

     if (country_flag == 3) { 

      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.orange, options); 
     } 

     if (country_flag == 4) { 

      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.green, options); 
     } 

     if (country_flag == 5) { 

      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.red, options); 
     } 
     if (country_flag == 6) { 
      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.purple, options); 
     } 

     if (country_flag == 7) { 
      mBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.pink, options); 
     } 

    } else { 

     mBitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.blue, options); 
    } 

    three = new pix11three(mBitmap, displayWidth, displayHeight, 0, 0, fps, 
      10); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     // handle touch 
    } 
    return true; 
} 

public void render(Canvas canvas) { 
    canvas.drawColor(Color.BLACK); 
    three.draw(canvas); 
} 

public void update() { 
    three.update(System.currentTimeMillis()); 
} 

}

pixelone.java

public class pix11one extends WallpaperService { 
public static final String SHARED_PREFS_NAME = "animation"; 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

@Override 
public Engine onCreateEngine() { 
    return new pix11engine(); 
} 

class pix11engine extends Engine implements 
     SharedPreferences.OnSharedPreferenceChangeListener { 

    boolean mVisible = false; 

    pix11two two; 

    pix11three three; 

    int country_counter = 1; 

    private final Handler mHandler = new Handler(); 

    private final Runnable mDrawPattern = new Runnable() { 
     public void run() { 

      draw(); 
     } 
    }; 
    SharedPreferences mPrefs; 

    public pix11engine() { 

     mPrefs = pix11one.this.getSharedPreferences(SHARED_PREFS_NAME, 0); 
     mPrefs.registerOnSharedPreferenceChangeListener(this); 
     onSharedPreferenceChanged(mPrefs, null); 
    } 

    public void onSharedPreferenceChanged(SharedPreferences prefs, 
      String key) { 

     String speed = prefs.getString("animate_speed", "one"); 

     String country_flag = prefs.getString("country", "English"); 

     String country_values[] = getResources().getStringArray(
       R.array.country_values); 

     // Comparing with preference value and array value 

     for (int i = 0; i < country_values.length; i++) { 

      if (country_values[i].equalsIgnoreCase(country_flag)) { 

       country_counter = i + 1; 

      } 

     } 

     if (speed.equals("one")) { 

      // Default 
      two = new pix11two(getBaseContext(), 7, country_counter); 

      draw(); 

     } else if (speed.equals("two")) { 

      // Slowest 
      two = new pix11two(getBaseContext(), 2, country_counter); 
      draw(); 
     } else if (speed.equals("three")) { 

      // Slow 
      two = new pix11two(getBaseContext(), 4, country_counter); 
      draw(); 
     } else if (speed.equals("four")) { 

      // Fast 

      two = new pix11two(getBaseContext(), 14, country_counter); 
      draw(); 
     } else if (speed.equals("five")) { 

      // Fastest 

      two = new pix11two(getBaseContext(), 18, country_counter); 
      draw(); 
     } 
    } 

    @Override 
    public void onCreate(SurfaceHolder surfaceHolder) { 
     super.onCreate(surfaceHolder); 
     setTouchEventsEnabled(true); 
    } 

    @Override 
    public void onSurfaceDestroyed(SurfaceHolder holder) { 
     super.onSurfaceDestroyed(holder); 
     mVisible = false; 
     mHandler.removeCallbacks(mDrawPattern); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mHandler.removeCallbacks(mDrawPattern); 
    } 

    @Override 
    public void onVisibilityChanged(boolean visible) { 
     mVisible = visible; 
     if (visible) { 
      draw(); 
     } else { 
      mHandler.removeCallbacks(mDrawPattern); 
     } 
    } 

    @Override 
    public void onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 

     } 
     super.onTouchEvent(event); 
    } 

    private void draw() { 
     final SurfaceHolder holder = getSurfaceHolder(); 
     Canvas c = null; 
     try { 
      c = holder.lockCanvas(); 
      if (c != null) { 

       two.render(c); 
       two.update(); 
      } 
     } catch (Exception e) { 
     } finally { 
      if (c != null) 
       holder.unlockCanvasAndPost(c); 
     } 
     mHandler.removeCallbacks(mDrawPattern); 
     if (mVisible) { 
      mHandler.postDelayed(mDrawPattern, 1000/75); 
     } 

    } 

} 

}

Я гавань показано pixelthree класс, но я думаю, что два класса являются достаточными полномочиями, чтобы решить эту проблему.

Любая помощь будет оценена по достоинству.

Thanks

ответ

0

Я попытался за счет уменьшения размера выборки до 2, и он работал для меня ..

BitmapFactory.Options варианты = новые BitmapFactory.Options();

options.inSampleSize = 2; 

options.inPurgeable = true 
1

похоже из памяти ошибка. попытайтесь уменьшить память для растрового изображения. Check this это поможет вам оптимизировать растровое изображение.

 Смежные вопросы

  • Нет связанных вопросов^_^