首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android简单涂料

Android简单涂料
EN

Stack Overflow用户
提问于 2013-11-18 08:42:18
回答 1查看 306关注 0票数 0

为什么不能正常工作?我试着“画”我手指的地方,写点什么。例如,当我试图制作C时,它看起来如下所示:http://postimg.org/image/5obyif4o1/ http://postimg.org/image/5obyif4o1/

我认为mX和来自touch_move的mY函数在touch_start中保持不变。

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-18 08:56:22

quadTo(float x1, float y1, float x2, float y2)从最后一点添加二次bezier,接近控制点(x1,y1),以(x2,y2)结尾。

也就是说,touch_move()中的控制点是(mX,mY)。您总是从(mX,mY)到((x + mX)/2,(y + mY)/2)画一条线。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20043382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档