我正在尝试在我的 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 );
}
canvas.drawRect( RectF, Paint ) 方法正确地绘制了矩形,但位图根本不显示。任何见解将不胜感激,在此先感谢。
编辑:我根据要求从我的 getImageMap() 方法中添加了代码。它基本上只是将我的资产/图像/文件夹中的所有图像作为地图返回,因此我可以轻松地从文件夹中提取我想要的任何图像。
private Map<String, Bitmap> getImageMap()
{
if (imageMap == null)
{
imageMap = new HashMap<String, Bitmap>();
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:我发现了我的错误,我在代码中更新了它。我只想感谢那些给我快速反馈的人,我很感激。