Create a bitmap using double values instead of int

2019-09-12 11:08发布

问题:

In my code I need create a Bitmap using double values. It´s important use the correct value.

I´m using:

Bitmap bmp = Bitmap.createBitmap(int, int, Bitmap.Config.ARGB_8888);

... When i would need something like:

Bitmap bmp = Bitmap.createBitmap(double, double, Bitmap.Config.ARGB_8888);

Everything I've trying to make the conversion ( possible ? ) Only returns the initial value or integer

How I can use double values ​​when creating the bitmap?

回答1:

Perhaps I misunderstand the question, but bitmaps are rasters of whole pixels. You can't have fractions of pixels, so you can only have integer numbers of rows and columns in the parameters for createBitmap.

Having said that, you can rescale how the bitmap is drawn using float scale factors applied via Matrix drawBitmap methods, e.g.

Canvas.drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)

Matrix matrix = new android.graphics.Matrix();
matrix.postScale(3.14f, 3.14f);
canvas.drawBitmap(bitmap, matrix, paint);

However, when it comes to rendering this on screen, it will again draw only whole pixels, so you still don't get fractional pixels displayed.



回答2:

Try this, convert double to int:

double myDouble = 10.4;
int myInt = (int) (myDouble);

Using (int) casts the double into an int