【发布时间】:2014-02-10 06:26:33
【问题描述】:
如何在画布上绘制我的可绘制图像?我从资源中获取我的图像并在画布上绘制该图像。有可能我们可以声明多个画布并将所有画布设置为一个吗? 我已经在画布上画了一张图片,想在上面画一个可绘制的图片吗?
private Bitmap drawtextonimage(Bitmap bitmap, String text, String caption) {
caption = caption.replace("\n", " ");
声明 Canvas 和 Paint ?
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;
}
【问题讨论】:
-
不工作是什么意思?你有任何异常吗?如果它不起作用,你需要先检查你的 text.equals("Good") 条件是否起作用。
-
试试 cs.setBitMap(bitmapx);相反...
-
不,它没有显示可绘制的图像。
-
尝试在drawBitmap方法中设置图片的宽高
-
你需要在 drawBitmap() 中设置一些有效值而不是你的 0, 0。
标签: android bitmap android-canvas android-drawable