本文介绍了如何使textview像图像使用Canvas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经实现了上视图,参考这个ans 5075
I already implemented upper view with reference to this ans 5075
我试过
public class CanvasView extends View {
Paint bPaint;
RectF coordbounds;
private Context mContext;
public CanvasView(Context context) {
super(context);
this.mContext = context;
}
private void init() {
bPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
bPaint.setStyle(Paint.Style.FILL_AND_STROKE);
bPaint.setColor(Color.WHITE);
}
@Override
public void onDraw(android.graphics.Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(coordbounds.left, coordbounds.centerY(),
coordbounds.right, coordbounds.centerY(), bPaint);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_approved);
int rectwidth = bitmap.getWidth();
int rectheight = bitmap.getHeight();
//Divide the line into four segments and subtract 2 * half the radii
float actualspan_image = (coordbounds.right - coordbounds.left) - (2 * rectwidth / 2);
//Segment the line into 3 parts
float interlinesegments_bitmap = actualspan_image / (5 - 1);
int circledia = 20;
//Divide the line into four segments and subtract 2 * half the radii
float actualspan = (coordbounds.right - coordbounds.left) - (2 * circledia / 2);
//Segment the line into 3 parts
float interlinesegments = actualspan / (5 - 1);
for (int i = 0; i < 5; i++) {
float left = coordbounds.left + (i * interlinesegments_bitmap);
float top = coordbounds.centerY() - rectheight / 2;
float right = coordbounds.left + (i * interlinesegments_bitmap) + rectwidth;
float bottom = coordbounds.centerY() + rectheight / 2;
if (i == 1) {
canvas.drawBitmap(bitmap, null, new RectF(left, top, right, bottom), null);
//canvas.drawLine(left, top, right, bottom, bPaint);
Paint paint = new Paint();
paint.setTextSize(20);
paint.setTextAlign(Paint.Align.CENTER);
TextView nameTv = new TextView(mContext);
nameTv.layout(0,0, (int)(coordbounds.left + circledia / 2 +
(i * interlinesegments)), (int) coordbounds.centerY());
nameTv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
nameTv.setTextColor(Color.WHITE);
nameTv.setText("Consultation");
nameTv.draw(canvas);
} else {
canvas.drawCircle(coordbounds.left + circledia / 2 +
(i * interlinesegments),
coordbounds.centerY(), 10, bPaint);
}
}
}
推荐答案
要在onDraw中执行以下操作:
To place the text do as follows in onDraw:
float midline_segment = (5-1)/2 * interlinesegments + rectwidth/2;
canvas.drawText("Order transmitted XYZ",
coordbounds.left + midline_segment,
coordbounds.centerY()+40,tPaint);
确保在Paint对象中正确设置对齐:
Make sure that you set the alignment correctly in the Paint object:
tPaint.setTextAlign(Paint.Align.CENTER);
这篇关于如何使textview像图像使用Canvas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!