简体   繁体   English

如何在视图上绘制文本?

[英]how to draw text on view?

i am new to android. 我是android新手。 I want to develop one simple application that can allow user to draw anything on screen like allowing user to draw shapes or signature on screen (View).in J2ME we can use pointerDragged() method but i dont know how to do it in android.i tried with onTouchEvent(MotionEvent) but not able to do. 我想开发一个简单的应用程序,它可以允许用户在屏幕上绘制任何内容,例如允许用户在屏幕上绘制形状或签名(视图)。在J2ME中,我们可以使用pointerDragged()方法,但是我不知道如何在android中进行操作。我尝试使用onTouchEvent(MotionEvent),但无法执行。 Please help. 请帮忙。


Thanks a lot. 非常感谢。 Its working very fine. 它的工作非常好。 But one problem is there, Drawing is not that much smooth, i mean when i tried to drag the screen drawing is very bold and i want to restrict the user to draw in limited area. 但是这里有一个问题,“绘图”并没有那么平滑,我的意思是当我尝试拖动屏幕时,绘图非常粗体,我想限制用户在有限的区域内绘图。 Please suggest me. 请给我建议。 Awaiting for your valuable suggestions. 等待您的宝贵建议。

One way to achieve is to create a view overriding onDraw(Canvas v),onTouchEvent(MotionEvent e) and onSizeChanged(int w, int h, int oldw, int oldh). 一种实现方法是创建一个覆盖onDraw(Canvas v),onTouchEvent(MotionEvent e)和onSizeChanged(int w,int h,int oldw,int oldh)的视图。 Below is code snippet to it. 以下是其代码段。 I believe it is self-explanatory. 我相信这是不言而喻的。

public class MyView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;

    public MyView(Context c) {
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFFFFFFF);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
        }
        return true;
    }
}

All you have to do is to call setContentView(new MyView(this)); 您要做的就是调用setContentView(new MyView(this)); from your activity class. 从您的活动课程中。 Hope this will help you. 希望这会帮助你。

**Disclaimer: The code snippet isn't mine. **免责声明:该代码段不是我的。 I also got it from somewhere from the net. 我也是从网上某个地方得到的。 The credit goes to whoever wrote it first. 功劳归谁先写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM