android绘画铅笔效果

时间:2015-08-30 11:10:23

标签: android android-canvas android-custom-view custom-painting

我编写的代码可以在onTouch中绘制,这是我的代码:

 class DrawingPanel extends View implements View.OnTouchListener {
 private static final String TAG = "DrawView";

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

 private Canvas  mCanvas;
 private Path    mPath;
 private Paint       mPaint;   
 private LinkedList<Path> paths = new LinkedList<Path>();

 public DrawingPanel(Context context) {
 super(context);
 setFocusable(true);
 setFocusableInTouchMode(true);

 this.setOnTouchListener(this);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
mCanvas = new Canvas();
mPath = new Path();
paths.add(mPath);

}               
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {            

    for (Path p : paths){
        canvas.drawPath(p, 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 = new Path();
    paths.add(mPath);
}




 @Override
 public boolean onTouch(View arg0, 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;

} }

我的代码工作完美,但我不想改变绘画效果。我想铅笔疼痛效果。我能解决我的问题吗?如果有人知道解决方案,请帮助我。谢谢 P.S 我不知道完美的自定义视图和Android中的Canvas,我需要帮助 谢谢大家

1 个答案:

答案 0 :(得分:0)

我通过在画布上绘制位图纹理来实现此效果,它看起来像这样: -

黑色背景

enter image description here

纹理图像

enter image description here

enter image description here

纹理图像: -

enter image description here

这是我的观点类: -

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

import com.serveroverload.dali.R;

public class CanvasBrushDrawing extends View {
    private Bitmap mBitmapBrush;
    private Vector2 mBitmapBrushDimensions;

    private List<Vector2> mPositions = new ArrayList<Vector2>(100);

    private static final class Vector2 {
        public Vector2(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public final float x;
        public final float y;
    }

    public CanvasBrushDrawing(Context context) {
        super(context);

// load your brush here
        mBitmapBrush = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight());

        setBackgroundColor(0xffffffff);
    }

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

        for (Vector2 pos : mPositions) {
            canvas.drawBitmap(mBitmapBrush, pos.x, pos.y, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_MOVE:
            final float posX = event.getX();
            final float posY = event.getY();
            mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
            invalidate();
        }

        return true;
    }
}

您可以在此活动中使用此视图,如下所示: -

setContentView(new CanvasBrushDrawing(MainActivity.this));

现在您只是来自设计师的更好的纹理文件。