作者:CK92_474 | 来源:互联网 | 2023-01-20 14:13
I'm trying to get a bitmap image to be drawn on my canvas in my android project. I've been at it for over two days now and just can't seem to figure it out. I attached the code where the function's being called.
我试图在我的android项目的画布上画一个位图图像。我已经做了两天多了,似乎就是搞不清楚。我附加了调用函数的代码。
private void drawLogo(Canvas canvas)
{
Paint test = new Paint();
test.setColor(Color.RED);
test.setStrokeWidth(4);
test.setStyle(Style.FILL_AND_STROKE);
//Here was my problem
//Changed to --> new RectF(scale_x, scale_y, 5*scale_x, 5*scale_y) and works now
RectF logoSize = new RectF(scale_x, 5*scale_y, 5*scale_x, scale_y);
Bitmap logoBitmap = getImageMap().get("LOGO");
canvas.drawRect(logoSize, test);
canvas.drawBitmap( logoBitmap, null, logoSize, null );
}
The canvas.drawRect( RectF, Paint ) method draws the rectangle correctly, but the bitmap does not show up at all. Any insight would be greatly appreciated, thanks in advance.
画布上。绘制矩形(RectF, Paint)方法正确绘制矩形,但是位图根本不显示。如有任何见解,我们将不胜感激,谢谢。
EDIT: I added code from my getImageMap() method as requested. It basically just returns all the images in my assets/images/ folder as a Map, so I can easily pull any image I want from the folder.
编辑:我按照要求添加了getImageMap()方法中的代码。它基本上只返回我的资产/图像/文件夹中的所有图像作为映射,因此我可以轻松地从文件夹中提取任何图像。
private Map getImageMap()
{
if (imageMap == null)
{
imageMap = new HashMap();
try
{
String[] files = getContext().getAssets().list("images");
for (String imageName : files)
{
// Construct a BitMap from an asset
Bitmap bitmap = BitmapFactory.decodeStream(
getContext().getAssets().open("images/" + imageName));
imageMap.put(imageName.replaceFirst("\\..*", ""), bitmap);
}
}
catch (IOException e) {Log.e("assets/images/ is empty", "IOException", e);}
}
return imageMap;
}
EDIT2: I found my misstake, I updated it in the code. I just wanna thank those who gave me speedy feedback, I appreciate it.
EDIT2:我发现了我的错误,我在代码中更新了它。我只是想感谢那些给我快速反馈的人,我很感激。
1 个解决方案