Create a bitmap using double values instead of int Android

不羁岁月 提交于 2020-01-17 04:42:11

问题


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



来源:https://stackoverflow.com/questions/35037358/create-a-bitmap-using-double-values-instead-of-int-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!