1

如何在我的CustomView中從我的onDraw()例程中提取出Bitmap對象?如何從視圖的onDraw()方法中提取位圖?

這裏是我的代碼:

public class DrawView extends View { 
    private Paint paint = new Paint(); 
    private Point point; 
    private LinkedList<Point> listaPontos; 
    private static Context context; 

    class Point { 

     public Point(float x, float y) { 
      this.x = x; 
      this.y = y; 
     } 

     float x = 0; 
     float y = 0; 
    } 

    public DrawView(Context context) { 
     super(context); 
     this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); 
     this.context = context; 
     paint.setColor(Color.YELLOW); 
     this.listaPontos = new LinkedList<Point>(); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 

     if(listaPontos.size() != 0){ 
      for(Point point : listaPontos){ 
       canvas.drawCircle(point.x, point.y, 25, paint);  
      } 
     } 
     calculateAmount(canvas);   
    } 

    private void calculateAmount(Canvas canvas) { 
     LinkedList<Integer> colors = new LinkedList<Integer>(); 
     for(int i = 0 ; i != canvas.getWidth(); i++) 
     { 
      for(int j = 0; j != canvas.getHeight(); j++){ 

       int color = BITMAP.getPixel(i,j); //How can I get the bitmap generated on onDraw ? 

       colors.add(color); 
      } 
     } 

     int yellow = 0; 
     int white = 0; 

     for(Integer cor : colors) { 

      if(cor == Color.WHITE) { 
       white++; 
      } 
      if(cor == Color.YELLOW) { 
       yellow++; 
      } 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
       case MotionEvent.ACTION_MOVE: 
        listaPontos.add(new Point(event.getX(), event.getY())); 
        break; 
     } 
     invalidate(); 
     return true; 
    } 
} 

非常感謝提前;)

編輯:位圖的是要計算每個像素的顏色,我該怎麼添加背景圖片到我的drawView函數?我測試了this.setBackgroundResource(R.drawable.a);在構造函數但沒有工作,再次感謝;)

+1

您也可以在.xml中設置背景圖片,只需設置android:background =「@ drawable/your_drawable」。同時請注意,DrawView中的繪製內容可能覆蓋背景圖片。 –

+0

我是不使用xml,mBitmapCanvas.drawColor(Color.WHITE);擦除內容,我現在使用位圖添加圖像b = BitmapFactory.decodeResource(getResources(),R.drawable.a); mBitmapCanvas.drawBitmap(b,0,0,paint);但圖像被拉長了..沒有最大化爲什麼? – TiagoM

+1

以下是如何從資源中正確加載位圖:http://pastebin.com/LsEWpSMq只需使用位圖b = BitmapResloader.decodeBitmapFromResource(資源res,int resId,int reqWidth,int reqHeight);另外,如果回答原始問題,請選擇我的答案爲正確。 –

回答

2

沒有辦法從畫布提取一個位圖。至少不是直接。

但是,有可能在位圖上畫上Canvas然後用Bitmap。該onDraw()方法完成

Bitmap mDrawBitmap; 
Canvas mBitmapCanvas; 
Paint drawPaint = new Paint(); 

@Override 
public void onDraw(Canvas canvas) { 

    drawPaint.setColor(Color.RED); 

    if (mDrawBitmap == null) { 
     mDrawBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 
     mBitmapCanvas = new Canvas(mDrawBitmap); 
    } 

    // clear previously drawn stuff 
    mBitmapCanvas.drawColor(Color.WHITE); 

    // draw on the btimapCanvas 
    mBitmapCanvas.drawStuff(...); 
    //... and more 

    // after drawing with the bitmapcanvas, 
    //all drawn information is stored in the Bitmap  


    // draw everything to the screen 
    canvas.drawBitmap(mDrawBitmap, 0, 0, drawPaint); 
} 

後,所有的繪製信息將在屏幕上(上繪製致電canvas.drawBitmap(...),並且也可以存儲在您的Bitmap對象(因爲所有的繪製操作已經在已創建的Canvas完成與Bitmap

+0

對象bitmapCanvas在哪裏?你錯了嗎?是mBitmapCanvas? – TiagoM

+0

是的,當然,對不起,我會糾正這一點:) –

+0

我會測試我現在能否做到,我會接受,如果一切順利。謝謝你的答案:) – TiagoM

相關問題