我想在白色畫布上設置圖像,並想繪製該圖像,我不知道該怎麼做。我嘗試了所有可能的方法,但沒有工作。這是我的代碼幫助我,如果你想要任何其他代碼,然後告訴我。 ,請您幫我......如何在畫布上設置圖像..?然後畫在圖像
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 {
\t // drawing path
\t private Path drawPath;
\t // drawing and canvas paint
\t private Paint drawPaint, canvasPaint;
\t // initial color
\t private int paintColor = 0xFF660000, paintAlpha = 255;
\t // canvas
\t private Canvas drawCanvas;
\t // canvas bitmap
\t private Bitmap canvasBitmap;
\t // brush sizes
\t private float brushSize, lastBrushSize;
\t // erase flag
\t private boolean erase = false;
\t public DrawingView(Context context, AttributeSet attrs) {
\t \t super(context, attrs);
\t \t setupDrawing();
\t }
\t // setup drawing
\t private void setupDrawing() {
\t \t // prepare for drawing and setup paint stroke properties
\t \t brushSize = getResources().getInteger(R.integer.medium_size);
\t \t lastBrushSize = brushSize;
\t \t drawPath = new Path();
\t \t drawPaint = new Paint();
\t \t drawPaint.setColor(paintColor);
\t \t drawPaint.setAntiAlias(true);
\t \t drawPaint.setStrokeWidth(brushSize);
\t \t drawPaint.setStyle(Paint.Style.STROKE);
\t \t drawPaint.setStrokeJoin(Paint.Join.ROUND);
\t \t drawPaint.setStrokeCap(Paint.Cap.ROUND);
\t \t canvasPaint = new Paint(Paint.DITHER_FLAG);
\t }
\t // draw the view - will be called after touch event
\t @Override
\t protected void onDraw(Canvas canvas) {
\t \t canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
\t \t canvas.drawPath(drawPath, drawPaint);
\t }
\t // size assigned to view
\t @Override
\t protected void onSizeChanged(int w, int h, int oldw, int oldh) {
\t \t super.onSizeChanged(w, h, oldw, oldh);
\t \t canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
\t \t drawCanvas = new Canvas(canvasBitmap);
\t }
\t // register user touches as drawing action
\t @Override
\t public boolean onTouchEvent(MotionEvent event) {
\t \t float touchX = event.getX();
\t \t float touchY = event.getY();
\t \t // respond to down, move and up events
\t \t switch (event.getAction()) {
\t \t case MotionEvent.ACTION_DOWN:
\t \t \t drawPath.moveTo(touchX, touchY);
\t \t \t break;
\t \t case MotionEvent.ACTION_MOVE:
\t \t \t drawPath.lineTo(touchX, touchY);
\t \t \t break;
\t \t case MotionEvent.ACTION_UP:
\t \t \t drawPath.lineTo(touchX, touchY);
\t \t \t drawCanvas.drawPath(drawPath, drawPaint);
\t \t \t drawPath.reset();
\t \t \t break;
\t \t default:
\t \t \t return false;
\t \t }
\t \t // redraw
\t \t invalidate();
\t \t return true;
\t }
\t // update color
\t public void setColor(String newColor) {
\t \t invalidate();
\t \t // check whether color value or pattern name
\t \t if (newColor.startsWith("#")) {
\t \t \t paintColor = Color.parseColor(newColor);
\t \t \t drawPaint.setColor(paintColor);
\t \t \t drawPaint.setShader(null);
\t \t } else {
\t \t \t // pattern
\t \t \t int patternID = getResources().getIdentifier(newColor, "drawable",
\t \t \t \t \t "com.example.drowingdemo");
\t \t \t // decode
\t \t \t Bitmap patternBMP = BitmapFactory.decodeResource(getResources(),
\t \t \t \t \t patternID);
\t \t \t // create shader
\t \t \t BitmapShader patternBMPshader = new BitmapShader(patternBMP,
\t \t \t \t \t Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
\t \t \t // color and shader
\t \t \t drawPaint.setColor(0xFFFFFFFF);
\t \t \t drawPaint.setShader(patternBMPshader);
\t \t }
\t }
\t // set brush size
\t public void setBrushSize(float newSize) {
\t \t float pixelAmount = TypedValue.applyDimension(
\t \t \t \t TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
\t \t \t \t \t \t .getDisplayMetrics());
\t \t brushSize = pixelAmount;
\t \t drawPaint.setStrokeWidth(brushSize);
\t }
\t // get and set last brush size
\t public void setLastBrushSize(float lastSize) {
\t \t lastBrushSize = lastSize;
\t }
\t public float getLastBrushSize() {
\t \t return lastBrushSize;
\t }
\t // set erase true or false
\t public void setErase(boolean isErase) {
\t \t erase = isErase;
\t \t if (erase)
\t \t \t drawPaint
\t \t \t \t \t .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
\t \t else
\t \t \t drawPaint.setXfermode(null);
\t }
\t // start new drawing
\t public void startNew() {
\t \t drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
\t \t invalidate();
\t }
\t // return current alpha
\t public int getPaintAlpha() {
\t \t return Math.round((float) paintAlpha/255 * 100);
\t }
\t // set alpha
\t public void setPaintAlpha(int newAlpha) {
\t \t paintAlpha = Math.round((float) newAlpha/100 * 255);
\t \t drawPaint.setColor(paintColor);
\t \t drawPaint.setAlpha(paintAlpha);
\t }
}
你有什麼問題? – pskink
沒問題,但我想設置圖像的立場畫布我想要在圖像上繪製 –
所以使用drawCanvas進行繪製,而不是默認View's Canvas – pskink