Android: how to get the width of a scalable raster map in onDraw ()

A simple question is when a simple answer does not work. I have a bitmap, and I want its size to be scaled for the system to display for different DPI screens in the onDraw () method. bitmap.width () successfully returns unscaled width. However, bitmap.getScaledWidth () returns zero. I tried getScaledWidth (canvas) and getScaledWidth (canvas.getDensity ()), but both return 0. Indeed, canvas.getDensity () returns zero, so I can’t even calculate it manually.

What am I doing wrong?

Bit in more detail. I am using a custom view. A bitmap is declared in the class and loaded into the constructor.

Edit:

I found that using:

        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

bitmap.getScaledHeight (metrics) returns the same value as bitmap.getHeight ().

It seems that bitmap.getWidth () returns the size of the resident bitmap after scaling it, which means that there is no obvious way to get the original width of the bitmap.

+5
source share
2 answers

There are many overloads in the Canvas class for the drawBitmap () function. One of them allows you to scale / cut Bitmap through a rather convenient interface.

public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)

Where

  • A bitmap bitmap is your bitmap that you want to draw
  • Rect src - . , ( src)
  • RectF dst - Rect Rectangle, .
  • -

! , 1/2 2 :

float startX = 0; //the left
float startY = 0; //and top corner (place it wherever you want)
float endX = startX + bitmap.getWidth() * 0.5f; //right
float endY = startY + bitmap.getHeight() * 2.0f; //and bottom corner

canvas.drawBitmap(bitmap, null, new RectF(startX, startY, endX, endY), null);

UPDATE

, , , :

, :

BitmapFactory.Options options = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true; // bitmap wont be loaded into the memory

//won't load the Bitmap, but the options will contain the required information.
BitmapFactory.decodeStream(inputStream, null, options);
/*or*/ BitmapFactory.decodeFile(pathName, options);

int originalWidth = bitmapOptions.outWidth;
int originalHeight = bitmapOptions.outHeight;

, () Bitmap ImageView, , , ( getWidth() getHeight()):

/*Get these values*/
int originalWidth, originalHeight, scaledWidth, scaledHeight; 

float scaleWidthRatio = (float)scaledWidth / originalWidth;
float scaleHeightRatio = (float)scaledHeight / originalHeight;
+4

? .: -)

protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        Drawable drawable = getDrawable();
        if(drawable==null)
            Log.d("onDraw()","getDrawable returns null");

        Bitmap  fullSizeBitmap,scaledBitmap = null,roundBitmap = null;

        fullSizeBitmap = ((BitmapDrawable)drawable).getBitmap() ;

        //get width & height of ImageView
        int scaledWidth = getMeasuredWidth();
        int scaledHeight = getMeasuredHeight();

        //bitmap, which will receive the reference to a bitmap scaled to the bounds of the ImageView.
        if(fullSizeBitmap!=null)
        scaledBitmap= getScaledBitmap(fullSizeBitmap,scaledWidth,scaledHeight);

        //Now, draw the bitmap on the canvas
        if(roundBitmap!=null)
           canvas.drawBitmap(roundBitmap, 0,0 , null);
}
+1

All Articles