使用位图设置墙纸避免裁切并设置合适的中心 [英] Set Wallpaper with bitmap avoid crop and set fit center

查看:84
本文介绍了使用位图设置墙纸避免裁切并设置合适的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作墙纸应用程序. 而且在使用位图设置墙纸时遇到了很大的麻烦.我试图找出答案一个星期.

I'm trying to make a Wallpaper Application. And I had big trouble during set wallpaper with bitmap. I try to figure out answer for a week.

我想将位图设置为墙纸

  1. 避免作物
  2. scaleType:fit_center(对齐中心垂直,宽高比)

我该怎么做?

P.S.我发现也许我可以使用Bitmap.createBitmap,但是我的尝试一遍又一遍地失败了. 我不知道我应该只使用WallPaperManager还是同时使用Bitmap.createBitmp.

P.S. And I found that maybe I can use Bitmap.createBitmap, but my try was failed over and over. I have no idea that I should use only WallPaperManager or both Bitmap.createBitmp Too.

静态位图createBitmap(位图源,int x,int y,int宽度,int高度,矩阵m,布尔过滤器)

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)

推荐答案

如果要从中心以相同的屏幕分辨率裁剪位图,请使用以下方法. 在这里,返回位图"与您的屏幕分辨率和裁剪相同.

If you want bitmap crop from centre and same screen resolution then use below method. Here Return bitmap is same your screen resolution and crop.

例如您的位图尺寸480x820

For Ex. Your Bitmap size 480x820

您的设备尺寸为480x800,返回尺寸为480x800(缩放后移除前10个像素和后10个像素).

Your device size 480x800 its Return 480x800 (top 10 pixel and bottom 10 pixel remove after scale).

您的设备尺寸为800x1280,其返回尺寸为800x1280(缩放后移除顶部43像素和底部43像素).

Your device size 800x1280 its Return 800x1280 (top 43 pixel and bottom 43 pixel remove after scale).

private Bitmap cropBitmapFromCenterAndScreenSize(Bitmap bitmap) {
    float screenWidth, screenHeight;
    float bitmap_width = bitmap.getWidth(), bitmap_height = bitmap
            .getHeight();
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();

    Log.i("TAG", "bitmap_width " + bitmap_width);
    Log.i("TAG", "bitmap_height " + bitmap_height);

    float bitmap_ratio = (float) (bitmap_width / bitmap_height);
    float screen_ratio = (float) (screenWidth / screenHeight);
    int bitmapNewWidth, bitmapNewHeight;

    Log.i("TAG", "bitmap_ratio " + bitmap_ratio);
    Log.i("TAG", "screen_ratio " + screen_ratio);

    if (screen_ratio > bitmap_ratio) {
        bitmapNewWidth = (int) screenWidth;
        bitmapNewHeight = (int) (bitmapNewWidth / bitmap_ratio);
    } else {
        bitmapNewHeight = (int) screenHeight;
        bitmapNewWidth = (int) (bitmapNewHeight * bitmap_ratio);
    }

    bitmap = Bitmap.createScaledBitmap(bitmap, bitmapNewWidth,
            bitmapNewHeight, true);

    Log.i("TAG", "screenWidth " + screenWidth);
    Log.i("TAG", "screenHeight " + screenHeight);
    Log.i("TAG", "bitmapNewWidth " + bitmapNewWidth);
    Log.i("TAG", "bitmapNewHeight " + bitmapNewHeight);

    int bitmapGapX, bitmapGapY;
    bitmapGapX = (int) ((bitmapNewWidth - screenWidth) / 2.0f);
    bitmapGapY = (int) ((bitmapNewHeight - screenHeight) / 2.0f);

    Log.i("TAG", "bitmapGapX " + bitmapGapX);
    Log.i("TAG", "bitmapGapY " + bitmapGapY);

    bitmap = Bitmap.createBitmap(bitmap, bitmapGapX, bitmapGapY,
            screenWidth,screenHeight);
    return bitmap;
}

这篇关于使用位图设置墙纸避免裁切并设置合适的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆