如何在画布上绘制可绘制的图像?我从资源中获取我的图像并在画布上绘制该图像。这可能我们可以声明多个画布并将所有画布设置为一个吗? 我已经在画布上有图像了,想在那上画一个可绘制的图像?
private Bitmap drawtextonimage(Bitmap bitmap, String text, String caption) {
caption = caption.replace("\n", " ");
声明画布和画图?
Canvas cs = new Canvas(bitmap);
Paint tPaint = new Paint();
Paint captionPaint = new Paint();
if (text.equals("Good")) {
tPaint.setColor(Color.GREEN);
} else {
tPaint.setColor(Color.RED);
}
tPaint.setStyle(Style.FILL);
tPaint.setTextSize(30);
//
captionPaint.setColor(Color.CYAN);
captionPaint.setAlpha(100);
captionPaint.setTextSize(25);
captionPaint.setTextScaleX((float) 0.7);
captionPaint.setTextAlign(Align.CENTER);
captionPaint.setTypeface(Typeface.SERIF);
captionPaint.setShadowLayer(1f, 0, 1f, Color.WHITE);
/*Canvas cs1 = new Canvas(bitmap);
cs1.drawRect(0, bitmap.getHeight(), bitmap.getWidth(), 500, tPaint);
// canvas.drawRect(33, 33, 77, 60, paint );
Paint zPaint = new Paint();
cs1.drawARGB((int) 0.5,54,54,200);*/
// cs.drawText(text, 60, 60, tPaint);
这个方法得到我的可绘制图像并在画布上绘制,但它不起作用?
Resources res = getResources();
Bitmap bitmapx = BitmapFactory.decodeResource(res, R.drawable.overlay_good_full);
Bitmap bitmapxx = BitmapFactory.decodeResource(res, R.drawable.overlay_bad_full);
if(text.equals("Good"))
{
cs.drawBitmap(bitmapx, 0, 0, new Paint());
//cs.drawBitmap(bitmapx, 0, 0, tPaint);
}
else
{
// cs.drawBitmap(bitmapxx, 0, 0, tPaint);
}
cs.drawText(caption, (bitmap.getWidth() / 2), bitmap.getHeight()
, captionPaint);
// canvas.drawBitmap(image, 0, 0, null);
// Log.i("Caption", caption);
return bitmap;
}
答案 0 :(得分:1)
以下代码将Drawable从资源绘制到从Bitmap创建的画布上(在本例中为相机预览中的位图)。这已经过测试,适用于API 22+(尚未使用早期版本进行测试):
protected void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics papel = e.Graphics();
Pen lapiz = new Pen(Color.Black);
//Dibujo de ejes X y Y
papel.DrawLine(lapiz, 20, 425, 742, 425);
papel.DrawLine(lapiz, 20, 425, 20, 20);
papel.DrawString("X", myFont, Brushes.Black, new Point(745, 418));
papel.DrawString("Y", myFont, Brushes.Black, new Point(14, 5));
}