如何提高图像质量

时间:2016-05-19 11:19:35

标签: android image resize scale wallpaper

我在缩小图像方面遇到了一些问题。在大分辨率手机上,所有图像看起来都不错,但是当需要缩小小手机上的图像时,图像看起来并不平滑。也许有人可以帮助我,如何在缩小比例后提高图像质量。提前致谢。以下是Note 4和S2手机的一些屏幕截图:

Note 4 Screen Shoot, upscale from 3840x2160 to 4551x2560

S2 Screen Shoot, downscale from 3840x2160 to 1422x800

    Bitmap bitmap;

public void setWallpaper() {

    Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.raw.img_test));

    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();

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

    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);

    try {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        myWallpaperManager.setBitmap(bitmap);
        Toast.makeText(getApplicationContext(), "Set wallpaper successfully!", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下内容缩放图像

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
相关问题