【问题标题】:Android WallpaperManager setBitmap fitCenterAndroid WallpaperManager setBitmap fitCenter
【发布时间】:2017-12-06 21:26:55
【问题描述】:

我正在尝试使用 WallpaperManager 将位图 FIT CENTER 到我的手机壁纸。

  • 图像尺寸:3840 x 2160
  • 手机尺寸:1080 x 1920

我尝试了很多策略:

  1. myWallpaperManager.suggestDesiredDimensions(width, height);
  2. Bitmap.createScaledBitmap(mImage, width, height, true);
  3. Set Wallpaper with bitmap avoid crop and set fit center
  4. how to fit the whole image on screen as wallpaper
  5. Wallpaper not properly fit on device screen
  6. https://developer.android.com/reference/android/app/WallpaperManager.html
  7. http://androidexample.com/How_to_Set_WallPaper_in_Android/question_answer.php?view=qad&token=39

每次我得到一个不适合中心的奇怪位图,有没有人有什么建议?

我想要的样子:

图片信息:

【问题讨论】:

    标签: android image bitmap image-resizing


    【解决方案1】:

    我找到了答案:Scaled Bitmap maintaining aspect ratio

    我做的是这样的:

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int phoneHeight = metrics.heightPixels;
        int phoneWidth = metrics.widthPixels;
    
        Bitmap mBit = returnBitmap(mImage, phoneWidth, phoneHeight);
    
    private Bitmap returnBitmap(Bitmap originalImage, int width, int height){
        Bitmap background = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
    
        float originalWidth = originalImage.getWidth();
        float originalHeight = originalImage.getHeight();
    
        Canvas canvas = new Canvas(background);
    
        float scale = width / originalWidth;
    
        float xTranslation = 0.0f;
        float yTranslation = (height - originalHeight * scale) / 2.0f;
    
        Matrix transformation = new Matrix();
        transformation.postTranslate(xTranslation, yTranslation);
        transformation.preScale(scale, scale);
    
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
    
        canvas.drawBitmap(originalImage, transformation, paint);
    
        return background;
    }
    

    【讨论】:

    • 谢谢,在搜索了一周设置适合屏幕的壁纸后,现在我找到了这个答案。非常感谢。这对我帮助很大。
    猜你喜欢
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多