【问题标题】:Android simple draw安卓简单画法
【发布时间】:2013-11-30 18:34:21
【问题描述】:

为什么它不起作用?我试图在我放手指的地方“画画”,写点东西。例如,当我尝试制作 C 时,他看起来像这样: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;
    }

【问题讨论】:

  • 定义“不起作用”?你期待什么结果?你实际上得到了什么?请具体点,否则这里得不到答案……
  • 当我尝试编写 C 时,它看起来不正确。当我用手指触摸时,我想将我的白色像素更改为黑色

标签: java android multithreading path draw


【解决方案1】:

编辑:这反映了您在代码中所做的更改。

当您创建 mPaint 时,您并没有设置它的样式,默认情况下是 FILL。将其更改为 STROKE。您还可以设置笔画宽度。在您的构造函数中执行此操作:

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

另外,您可以在 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);
}

检查 Android SDK 示例文件夹中的 FingerSample。

【讨论】:

  • 你知道我找到的是什么安卓版本的 FingerSample 吗?
  • 找到你安装Android SDK的文件夹,然后它在/samples/android-18/legacy/ApiDemos/src/com/example/android/apis/graphics(这是在我的mac上,只是使用搜索功能寻找 FingerSample)
  • 谢谢我发现......这是代码:
  • Thanx .... 我复制了触摸功能,它像图片中一样绘画(postimg.org/image/5obyif4o1);我更新了上面的代码
  • 在 onDraw() 中将 canvas.drawPath( mPath, circlePaint ) 更改为 canvas.drawPath( mPath, mPaint )
猜你喜欢
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多