如何绘制平滑的位图路径,没有任何中断

时间:2015-04-02 08:16:42

标签: android bitmap android-canvas

我正在处理一个绘图应用程序,我想绘制一个位图路径 我的代码是这样的:



	private void onCanvasInitialization() {
		// Main_Activity.paintButton.setEnabled(true);

		mPaint = new Paint();
		BlurMaskFilter bmf = new BlurMaskFilter(2, Blur.OUTER);
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setFilterBitmap(true);
		mPaint.setColor(Main_Activity.colorchanger);
		mCanvas = new Canvas();
		mPath = new Path();
		mPaint.setStyle(Paint.Style.STROKE);
	mPaint.setMaskFilter(bmf);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setAlpha(255);
		bmp = Bitmap.createScaledBitmap(bmp, 30, 30, false);
		bmp = bmp.copy(Config.ARGB_8888, true);
	}

	@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 boolean onTouch(View arg0, MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if (true)
			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;
	}

	private void touch_start(float x, float y) {
		Random rand = new Random();
		float r = rand.nextFloat();
		float g = rand.nextFloat();
		float b = rand.nextFloat();
		Random rnd = new Random();
		mPaint.setColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256),
				rnd.nextInt(256)));

		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;
			arrX[i] = mX;
			arrY[i] = mY;
			i++;
		}
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int k = 0; k < i; k++) {
			canvas.drawBitmap(bmp, arrX[k], arrY[k], mPaint);

		}
	}
&#13;
&#13;
&#13;

通过这个我能够在画布上绘制位图但流程不平滑它看起来不像一条线。两个连续的bitmaps之间有一个间隙。我希望它看起来像一条路径。

1 个答案:

答案 0 :(得分:1)

为什么在每个案例中调用 invalidate()方法:,尝试将其调出来。

对于前。

public boolean onTouch(View arg0, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (true)
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);                   
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                break;
            }
          invalidate();
        return true;
    }