开发者

placing a text value in a desired position

I have drawn a graphic object ,say rectangle. I would like write some text at each corner of the rectangle. How to achieve this ?

private static class SimpleView extends View {
    private ShapeDrawable mDrawable = new ShapeDrawable();

    public SimpleView(Context context) {
        super(context);
        setFocusable(true);
        this.mDrawable = new ShapeDrawable(new RectShape());
        this.mDrawable.getPaint().setColor(0xFF0F00FF);


}

             @Override
        protected void onDraw(Canvas canvas) {
            int x1 = 50;
            int y1 = 150;
            int width = 400;
            int height = 50;
            this.mDrawable.setBounds(x1, y1, x1 + width, y1 + height);
            this.mDrawable.draw(canvas);

            int x = 0;
            int开发者_如何学JAVA y = 0;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

etc


If you are using Canvas thenjust use the drawText() method.

drawText(String text, int start, int end, float x, float y, Paint paint) 

Source: http://developer.android.com/reference/android/graphics/Canvas.html


Use canvas.drawText with the coordinates of the corners, and with Paint set at the appropriate alignment. i.e. you'd drawText at each corner, with the right corners having paint.align = RIGHT, and the left corners having paint.align = LEFT. That way, the text is drawn to the side of the square.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜