Android的简单的抽签 [英] Android simple draw

查看:386
本文介绍了Android的简单的抽签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不起作用?我想画在这里我把我的手指,写的东西。例如,当我试图做一个C,他看起来像这样: http://postimg.org/image/ 5obyif4o1 / ............................................ ............................

 公共类BlackPixel扩展视图实现OnTouchListener {
    私人位图mBitmap;
    帆布mCanvas =新的Canvas();
    路径的mpath =新路径();
    涂料mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
    涂料mPaint =新的油漆();
    涂料circlePaint =新的油漆();
    路径circlePath =新路径();    上下文语境;     @覆盖
     保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    super.onSizeChanged(W,H,oldw,oldh);    mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
    mCanvas =新的Canvas(mBitmap);    }
    公共BlackPixel(上下文的背景下){
        超级(上下文);
        setFocusable(真);
        setFocusableInTouchMode(真);
        this.setOnTouchListener(本);
    }    @覆盖
    保护无效的onDraw(帆布油画)
    {
        super.onDraw(画布);        canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);        canvas.drawPath(的mpath,mPaint);        canvas.drawPath(circlePath,circlePaint);
    }// @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){        浮X = event.getX();
        浮Y = event.getY();
           INT行动= event.getAction();
            开关(动作){
            案例MotionEvent.ACTION_DOWN:
                touch_start(X,Y​​);
                无效();
                返回true;            案例MotionEvent.ACTION_MOVE:
                TOUCH_MOVE(X,Y);
                无效();
                打破;
            案例MotionEvent.ACTION_UP:
                touch_up(X,Y);
                无效();
                打破;
            }
            返回true;
    }
    私人无效touch_start(浮法X,浮法Y)
    {
           mPath.reset();
           mPath.moveTo(X,Y);
           MX = X;
           我= Y;    }    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 4;
    私人无效TOUCH_MOVE(浮法X,浮法Y){          浮DX = Math.abs(X - MX);
          浮DY = Math.abs(Y - 我的);
          如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
              mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
              MX = X;
              我= Y;
          }
    }
    私人无效touch_up(浮法X,浮法Y)
    {
          mPath.lineTo(MX,我的);
          //提交的路径,我们的屏幕外
          mCanvas.drawPath(的mpath,mPaint);
          //杀死这个,所以我们不要双击平局
          mPath.reset();    }    类Point
    {
        浮动的x,y;
    }


解决方案

编辑:这反映在您的code所做的更改。

当您创建mPaint,你不设置它的'风格,默认情况下为填充。将其更改为中风。您还可以设置描边宽度。做到这一点在构造函数:

 公共BlackPixel(上下文的背景下){
    超级(上下文);
    setFocusable(真);
    setFocusableInTouchMode(真);
    this.setOnTouchListener(本);    //改变涂料的风格中风
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(12);
}

此外,您还可以设置背景颜色onSizeChanged:

  @覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    super.onSizeChanged(W,H,oldw,oldh);    mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
    //设置背景颜色
    mBitmap.eraseColor(0xFFAAAAAA);
    mCanvas =新的Canvas(mBitmap);
}

检查FingerSample在Android SDK中的样本文件夹中。

Why it doesn't work ? I'm trying to "paint" where i put my finger, to write something. For example, when i am trying to make a C he looks like this: http://postimg.org/image/5obyif4o1/ ........................................................................

 public class BlackPixel extends View implements OnTouchListener{
    private Bitmap  mBitmap;
    Canvas mCanvas=new Canvas();
    Path    mPath=new Path();
    Paint   mBitmapPaint=new Paint(Paint.DITHER_FLAG);  
    Paint mPaint=new Paint();
    Paint circlePaint=new Paint();
    Path circlePath = new Path();

    Context context;

     @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);

    }


    public BlackPixel(Context context) {
        super(context);
        setFocusable(true);     
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
    }



    @Override
    protected void onDraw(Canvas canvas)
    {       
        super.onDraw(canvas);

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

        canvas.drawPath( mPath, mPaint );

        canvas.drawPath( circlePath,circlePaint  );
    }



//  @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x=event.getX();
        float y=event.getY();


           int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x,y);
                invalidate();
                return true;                

            case MotionEvent.ACTION_MOVE:
                touch_move(x,y);             
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up(x,y);
                invalidate();
                break;              
            }
            return true;
    }


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

    }

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


    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(float x , float y)
    {
          mPath.lineTo(mX, mY);
          // commit the path to our offscreen
          mCanvas.drawPath(mPath, mPaint);
          // kill this so we don't double draw
          mPath.reset();

    }



    class Point
    {
        float x,y;
    }

解决方案

EDIT: this reflects changes you made in your code.

When you create mPaint, you're not setting its' style, which by default is FILL. Change it to STROKE. You can also set the stroke width. Do it in your constructor:

public BlackPixel(Context context) {
    super(context);
    setFocusable(true);     
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    //change paint style to STROKE
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(12);
}

Also, you can set the background color in onSizeChanged:

@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);
    //set background color
    mBitmap.eraseColor(0xFFAAAAAA);
    mCanvas = new Canvas(mBitmap);
}

Check FingerSample in Android SDK samples folder.

这篇关于Android的简单的抽签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆