如何在画布上设置图像..?然后在Image上绘图

时间:2015-04-06 04:53:32

标签: android canvas

我想在白色画布上设置图像并想要在该图像上绘制我不知道该怎么做。我尝试了所有可能的方法,但没有工作。这是我的代码帮助我,如果你想要任何其他代码然后告诉我。请帮帮我......



package com.example.drowingdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class DrawingView extends View {

	// drawing path
	private Path drawPath;
	// drawing and canvas paint
	private Paint drawPaint, canvasPaint;
	// initial color
	private int paintColor = 0xFF660000, paintAlpha = 255;
	// canvas
	private Canvas drawCanvas;
	// canvas bitmap
	private Bitmap canvasBitmap;
	// brush sizes
	private float brushSize, lastBrushSize;
	// erase flag
	private boolean erase = false;

	public DrawingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupDrawing();
	}

	// setup drawing
	private void setupDrawing() {

		// prepare for drawing and setup paint stroke properties
		brushSize = getResources().getInteger(R.integer.medium_size);
		lastBrushSize = brushSize;
		drawPath = new Path();
		drawPaint = new Paint();
		drawPaint.setColor(paintColor);
		drawPaint.setAntiAlias(true);
		drawPaint.setStrokeWidth(brushSize);
		drawPaint.setStyle(Paint.Style.STROKE);
		drawPaint.setStrokeJoin(Paint.Join.ROUND);
		drawPaint.setStrokeCap(Paint.Cap.ROUND);
		canvasPaint = new Paint(Paint.DITHER_FLAG);
	}

	// draw the view - will be called after touch event
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
		canvas.drawPath(drawPath, drawPaint);
	}

	// size assigned to view
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		drawCanvas = new Canvas(canvasBitmap);
	}

	// register user touches as drawing action
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float touchX = event.getX();
		float touchY = event.getY();
		// respond to down, move and up events
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			drawPath.moveTo(touchX, touchY);
			break;
		case MotionEvent.ACTION_MOVE:
			drawPath.lineTo(touchX, touchY);
			break;
		case MotionEvent.ACTION_UP:
			drawPath.lineTo(touchX, touchY);
			drawCanvas.drawPath(drawPath, drawPaint);
			drawPath.reset();
			break;
		default:
			return false;
		}
		// redraw
		invalidate();
		return true;

	}

	// update color
	public void setColor(String newColor) {
		invalidate();
		// check whether color value or pattern name
		if (newColor.startsWith("#")) {
			paintColor = Color.parseColor(newColor);
			drawPaint.setColor(paintColor);
			drawPaint.setShader(null);
		} else {
			// pattern
			int patternID = getResources().getIdentifier(newColor, "drawable",
					"com.example.drowingdemo");
			// decode
			Bitmap patternBMP = BitmapFactory.decodeResource(getResources(),
					patternID);
			// create shader
			BitmapShader patternBMPshader = new BitmapShader(patternBMP,
					Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
			// color and shader
			drawPaint.setColor(0xFFFFFFFF);
			drawPaint.setShader(patternBMPshader);
		}
	}

	// set brush size
	public void setBrushSize(float newSize) {
		float pixelAmount = TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
						.getDisplayMetrics());
		brushSize = pixelAmount;
		drawPaint.setStrokeWidth(brushSize);
	}

	// get and set last brush size
	public void setLastBrushSize(float lastSize) {
		lastBrushSize = lastSize;
	}

	public float getLastBrushSize() {
		return lastBrushSize;
	}

	// set erase true or false
	public void setErase(boolean isErase) {
		erase = isErase;
		if (erase)
			drawPaint
					.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		else
			drawPaint.setXfermode(null);
	}

	// start new drawing
	public void startNew() {
		drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
		invalidate();
	}

	// return current alpha
	public int getPaintAlpha() {
		return Math.round((float) paintAlpha / 255 * 100);
	}

	// set alpha
	public void setPaintAlpha(int newAlpha) {
		paintAlpha = Math.round((float) newAlpha / 100 * 255);
		drawPaint.setColor(paintColor);
		drawPaint.setAlpha(paintAlpha);
	}
}




1 个答案:

答案 0 :(得分:1)

通过代码,我没有看到哪一行加载图像。

在你的代码中,在方法onSizeChanged中为Canvas设置了Bitmap,但是位图是一个空位图,你可以试试代码来加载图像

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.your_image) //-->here load your image
                                .copy(Bitmap.Config.ARGB_8888, true); 
    drawCanvas = new Canvas(canvasBitmap);//-->set bitmap
}

另外,在invalidate()之前调用drawPath.reset(),不要在onDraw()之前重置路径,

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        // respond to down, move and up events
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            drawPath.lineTo(touchX, touchY);
            drawPath = new Path();//--> use a new path after ACTION_UP
            break;
        default:
            return false;
        }
        // redraw
        drawCanvas.drawPath(drawPath, drawPaint);//--> Draw on canvasBitmap
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawBitmap(canvasBitmap, 0, 0, null);//--> draw canvasBitmap on canvas
        ...
    }