Bitmap 现场墙纸被强制关闭的问题
我正在创建一个在xhdpi和ldpi设备上完美运行的实时墙纸应用程序,但在基于hdpi和mdpi的手机上却遇到了强制关闭的问题 当我们点击“设置墙纸”时,它会在预览屏幕中加载图像,甚至当我们从应用程序更改设置时,它也会崩溃 以上两个条件使应用程序强制关闭 看起来像是OutOfMemoryError 错误不一致 这是我的密码: pixeltowo.javaBitmap 现场墙纸被强制关闭的问题,bitmap,live-wallpaper,Bitmap,Live Wallpaper,我正在创建一个在xhdpi和ldpi设备上完美运行的实时墙纸应用程序,但在基于hdpi和mdpi的手机上却遇到了强制关闭的问题 当我们点击“设置墙纸”时,它会在预览屏幕中加载图像,甚至当我们从应用程序更改设置时,它也会崩溃 以上两个条件使应用程序强制关闭 看起来像是OutOfMemoryError 错误不一致 这是我的密码: pixeltowo.java public class pix11two extends SurfaceView { private pix11three three; i
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());
}
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);
}
}
}
}
pixelone.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());
}
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);
}
}
}
公共类pix11one扩展服务{
public static final String SHARED_PREFS_NAME=“animation”;
@凌驾
public void onCreate(){
super.onCreate();
}
@凌驾
公共空间{
super.ondestory();
}
@凌驾
公共引擎onCreateEngine(){
返回新的PIX11引擎();
}
PIX11类发动机扩展了发动机机具
SharedReferences.OnSharedPreferenceChangeListener{
布尔mVisible=false;
PIX112两个;
PIX11三个三个;
int country_counter=1;
私有最终处理程序mHandler=新处理程序();
private final Runnable mDrawPattern=new Runnable(){
公开募捐{
draw();
}
};
共享参考文献;
公共PIX11引擎(){
mPrefs=pix11one.this.getSharedReferences(共享首选项名称,0);
mPrefs.RegisterOnSharedReferenceChangeListener(此);
onSharedPreferenceChanged(mPrefs,null);
}
已更改共享首选项上的公共无效(共享首选项,
字符串键){
字符串速度=prefs.getString(“动画速度”,“一”);
字符串country_flag=prefs.getString(“国家”、“英语”);
字符串country_值[]=getResources().getStringArray(
R.array.country_值);
//与首选项值和数组值进行比较
对于(int i=0;i
}
我还没有展示pixelthree类,但我认为两个类足以解决这个问题
任何帮助都将不胜感激
谢谢似乎是内存不足错误。尝试减少位图的内存。它将帮助您优化位图。我已经尝试将样本大小减少到2,它对我很有效 BitmapFactory.Options=new-BitmapFactory.Options()