2015-04-06 218 views
1

我想在白色畫布上設置圖像,並想繪製該圖像,我不知道該怎麼做。我嘗試了所有可能的方法,但沒有工作。這是我的代碼幫助我,如果你想要任何其他代碼,然後告訴我。 ,請您幫我......如何在畫布上設置圖像..?然後畫在圖像

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

+0

你有什麼問題? – pskink

+0

沒問題,但我想設置圖像的立場畫布我想要在圖像上繪製 –

+0

所以使用drawCanvas進行繪製,而不是默認View's Canvas – pskink

回答

1

通過代碼,我沒有看到這行加載圖像。

在你的代碼,位圖設置爲畫布的方法OnSizeChanged,但位圖是一個空的位圖,您可以嘗試的代碼加載圖像

@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 
} 

此外,drawPath.reset()被調用在invalidate()之前,不要在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 
     ... 
    } 
+0

它不工作.. –

+0

請檢查新的答案,不要重置drawPath之前無效。另外,drawCanvas不能在視圖上工作,你可以刪除它。 – yummy

+0

它的作品,但一些其他的錯誤發生..例如,我畫黑色的J,然後我改變顏色綠色的畫A在的時間J也是綠色的顏色 –