我最近用 18 张图片创建了自己的 Android 动态壁纸项目,但它不能设置为壁纸。如果我将其设置为墙纸,我会看到以下错误消息。不幸的是,示例已停止工作。图像分辨率 480x800。我使用了 18 张图片。项目大小为 2.67 MB。但我在我的项目中找不到任何错误。有谁知道这个错误是什么?
public class star extends WallpaperService {
public void onCreate(){
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
private class WallpaperEngine extends Engine{
Bitmap i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i21,i22,i23,i24,i25,i26,i27,i28,i29,i30;
int position;
int screenwidth;
DisplayMetrics metric;
int count=0;
private final Handler hanlder=new Handler();
private boolean visible=false;
private final Runnable r=new Runnable() {
@Override
public void run() {
draw();
}
};
WallpaperEngine() {
i1=BitmapFactory.decodeResource(getResources(), R.drawable.image1);
i2=BitmapFactory.decodeResource(getResources(), R.drawable.image2);
i3=BitmapFactory.decodeResource(getResources(), R.drawable.image3);
i4=BitmapFactory.decodeResource(getResources(), R.drawable.image4);
i5=BitmapFactory.decodeResource(getResources(), R.drawable.image5);
i6=BitmapFactory.decodeResource(getResources(), R.drawable.image6);
i7=BitmapFactory.decodeResource(getResources(), R.drawable.image7);
i8=BitmapFactory.decodeResource(getResources(), R.drawable.image8);
i9=BitmapFactory.decodeResource(getResources(), R.drawable.image9);
i10=BitmapFactory.decodeResource(getResources(), R.drawable.image10);
i11=BitmapFactory.decodeResource(getResources(), R.drawable.image11);
i12=BitmapFactory.decodeResource(getResources(), R.drawable.image12);
i13=BitmapFactory.decodeResource(getResources(), R.drawable.image13);
i14=BitmapFactory.decodeResource(getResources(), R.drawable.image14);
i15=BitmapFactory.decodeResource(getResources(), R.drawable.image15);
i16=BitmapFactory.decodeResource(getResources(), R.drawable.image16);
i17=BitmapFactory.decodeResource(getResources(), R.drawable.image17);
i18=BitmapFactory.decodeResource(getResources(), R.drawable.image18);
i19=BitmapFactory.decodeResource(getResources(), R.drawable.image19);
i20=BitmapFactory.decodeResource(getResources(), R.drawable.image20);
position=0;
metric=getBaseContext().getResources().getDisplayMetrics();
screenwidth= metric.widthPixels;
}
public void onCreate(SurfaceHolder holder){
super.onCreate(holder);
}
public void onVisibilityChanged(boolean visible){
this.visible=visible;
if (visible) {
hanlder.post(r);
} else {
hanlder.removeCallbacks(r);
}
}
public void onSurfaceDestroyed(SurfaceHolder holder){
visible=false;
hanlder.removeCallbacks(r);
}
public void draw(){
Bitmap arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i19,i18,i17,i16,i15,i14,i13,i12,i11,i10,i9,i8,i7,i6,i5,i4,i3,i2};
SurfaceHolder holder=getSurfaceHolder();
Canvas c=null;
try {
c=holder.lockCanvas();
if(c!=null){
c.drawColor(Color.WHITE);
c.drawBitmap(arr[count], position,0, null);
++count;
if(count==arr.length){
count=0;
}
}
} finally {
if(c!=null){
holder.unlockCanvasAndPost(c);
}
}
hanlder.removeCallbacks(r);
if(visible){
hanlder.postDelayed(r, 70);
}
}
}
}
在我收到以下 LOGCAT 错误后:
09-26 14:07:01.005: E/AndroidRuntime(2767): FATAL EXCEPTION: main
09-26 14:07:01.005: E/AndroidRuntime(2767): java.lang.OutOfMemoryError
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-26 14:07:01.005: E/AndroidRuntime(2767): at com.example.stargate.star$WallpaperEngine.<init>(star.java:55)
09-26 14:07:01.005: E/AndroidRuntime(2767): at com.example.stargate.star.onCreateEngine(star.java:27)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1034)
09-26 14:07:01.005: E/AndroidRuntime(2767): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.os.Looper.loop(Looper.java:137)
09-26 14:07:01.005: E/AndroidRuntime(2767): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-26 14:07:01.005: E/AndroidRuntime(2767): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 14:07:01.005: E/AndroidRuntime(2767): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 14:07:01.005: E/AndroidRuntime(2767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-26 14:07:01.005: E/AndroidRuntime(2767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-26 14:07:01.005: E/AndroidRuntime(2767): at dalvik.system.NativeStart.main(Native Method)
09-26 14:08:11.935: E/Trace(4999): error opening trace file: No such file or directory (2)