我正在尝试制作墙纸应用程序。我在用位图设置壁纸时遇到了大麻烦。我试图找出一个星期的答案。
我想将位图设置为壁纸
- 避免作物
- scaleType:fit_center(对齐中心垂直,纵横比)
我怎样才能做到?
PS我发现也许我可以使用Bitmap.createBitmap,但我的尝试一次又一次地失败了。我不知道我应该只使用 WallPaperManager 还是同时使用 Bitmap.createBitmp。
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
public void setScreenBitmap(final Bitmap bitmapInputed)
{
final WallpaperManager wpm = WallpaperManager.getInstance(myContext);
final Display display = ((Activity) this.myContext).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
try
{
wpm.setBitmap(Bitmap.createScaledBitmap(bitmapInputed, width, height, true));
}
catch (IOException e)
{
Log.e(TAG+".setScreenBitmap", e.toString());
e.printStackTrace();
}
wpm.setWallpaperOffsetSteps(1.0f, 1.0f);
wpm.suggestDesiredDimensions(width, height);
}
09-06 20:38:15.563: W/System.err(4892): java.lang.IllegalArgumentException: x must be >= 0
09-06 20:38:15.563: W/System.err(4892): at android.graphics.Bitmap.checkXYSign(Bitmap.java:285)
09-06 20:38:15.563: W/System.err(4892): at android.graphics.Bitmap.createBitmap(Bitmap.java:580)
09-06 20:38:15.568: W/System.err(4892): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-06 20:38:15.568: W/System.err(4892): at com.myarena.util.MyWallpaperUtil.getBitmapFromCenterAndScreenSize(MyWallpaperUtil.java:459)
09-06 20:38:15.568: W/System.err(4892): at com.myarena.util.MyWallpaperUtil.setScreenBitmap(MyWallpaperUtil.java:485)
09-06 20:38:15.568: W/System.err(4892): at com.myarena.util.MyWallpaperUtil.changeWallpaper(MyWallpaperUtil.java:231)
09-06 20:38:15.568: W/System.err(4892): at com.myarena.controller.ControllerActivity$PlaceholderFragment$2.onClick(ControllerActivity.java:213)
09-06 20:38:15.568: W/System.err(4892): at android.view.View.performClick(View.java:4489)
09-06 20:38:15.568: W/System.err(4892): at android.view.View$PerformClick.run(View.java:18803)
09-06 20:38:15.568: W/System.err(4892): at android.os.Handler.handleCallback(Handler.java:730)
09-06 20:38:15.568: W/System.err(4892): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 20:38:15.568: W/System.err(4892): at android.os.Looper.loop(Looper.java:137)
09-06 20:38:15.568: W/System.err(4892): at android.app.ActivityThread.main(ActivityThread.java:5493)
09-06 20:38:15.568: W/System.err(4892): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 20:38:15.568: W/System.err(4892): at java.lang.reflect.Method.invoke(Method.java:525)
09-06 20:38:15.568: W/System.err(4892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
09-06 20:38:15.568: W/System.err(4892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
09-06 20:38:15.568: W/System.err(4892): at dalvik.system.NativeStart.main(Native Method)