2017-03-09 114 views
0

这是我的代码。在Android中调整图像大小时质量不佳?

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     img = (ImageView)findViewById(R.id.resizedView); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inScaled = false; 
     Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.tutimage1, options); 
     Bitmap bmp = getResizedBitmap(largeIcon, 200); 
     img.setImageBitmap(bmp); 
    } 
public Bitmap getResizedBitmap(Bitmap image, int maxSize) 
    { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float)width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 

     // return getResizedBitmapWithQuality(image, width, height); 
     return Bitmap.createScaledBitmap(image, width, height, false); 
    } 

所以我试图研究和测试,但结果仍然是一样的。

enter image description here enter image description here

是因为所需要的尺寸太小?

原始尺寸为1120×2048,其结果的大小为110×200

+0

因为你用200降低图像的高度。getResizedBitmap(largeIcon,200); –

+0

有没有办法解决这个问题? 或者我应该强制增加身高吗? 我需要真的200的尺寸。 帮助我! –

+0

[压缩您的图像,而不会丢失质量](http://stackoverflow.com/questions/28424942/decrease-image-size-without-losing-its-quality-in-android)我希望它是帮助完整的你.. –

回答

0

使用此功能可提高图像质量的位图

public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) { 
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888); 

    float scaleX = newWidth/(float) bitmap.getWidth(); 
    float scaleY = newHeight/(float) bitmap.getHeight(); 
    float pivotX = 0; 
    float pivotY = 0; 

    Matrix scaleMatrix = new Matrix(); 
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY); 

    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.setMatrix(scaleMatrix); 
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG)); 

    return scaledBitmap; 
} 
+0

我已经使用你的代码。 但是一样。 –

+0

我认为所需的尺寸太小。 –

相关问题