2014-02-10 155 views
0

如何在畫布上繪製我的可繪製圖像?我從資源中獲取圖像並在畫布上繪製該圖像。這是可能的,我們可以聲明多個畫布並將其全部設置爲一個? 我已經在畫布上創建了圖像,並且想要繪製可繪製的圖像?在畫布上繪製我的可繪製圖像?

private Bitmap drawtextonimage(Bitmap bitmap, String text, String caption) { 
    caption = caption.replace("\n", " "); 

聲明帆布和油漆?

Canvas cs = new Canvas(bitmap); 
    Paint tPaint = new Paint(); 
    Paint captionPaint = new Paint(); 

    if (text.equals("Good")) { 
     tPaint.setColor(Color.GREEN); 
    } else { 
     tPaint.setColor(Color.RED); 
    } 

    tPaint.setStyle(Style.FILL); 
    tPaint.setTextSize(30); 

    // 

    captionPaint.setColor(Color.CYAN); 
    captionPaint.setAlpha(100); 
    captionPaint.setTextSize(25); 
    captionPaint.setTextScaleX((float) 0.7); 
    captionPaint.setTextAlign(Align.CENTER); 
    captionPaint.setTypeface(Typeface.SERIF); 
    captionPaint.setShadowLayer(1f, 0, 1f, Color.WHITE); 

    /*Canvas cs1 = new Canvas(bitmap); 

     cs1.drawRect(0, bitmap.getHeight(), bitmap.getWidth(), 500, tPaint); 

     // canvas.drawRect(33, 33, 77, 60, paint); 
     Paint zPaint = new Paint(); 
     cs1.drawARGB((int) 0.5,54,54,200);*/ 

//   cs.drawText(text, 60, 60, tPaint); 

此方法得到我的可繪製圖像並在畫布上繪製,但它不工作?

 Resources res = getResources(); 
    Bitmap bitmapx = BitmapFactory.decodeResource(res, R.drawable.overlay_good_full); 
    Bitmap bitmapxx = BitmapFactory.decodeResource(res, R.drawable.overlay_bad_full); 
    if(text.equals("Good")) 
    { 


     cs.drawBitmap(bitmapx, 0, 0, new Paint()); 
     //cs.drawBitmap(bitmapx, 0, 0, tPaint); 

    } 
    else 
    { 
     // cs.drawBitmap(bitmapxx, 0, 0, tPaint); 

    } 

    cs.drawText(caption, (bitmap.getWidth()/2), bitmap.getHeight() 
      , captionPaint); 




// canvas.drawBitmap(image, 0, 0, null); 


// Log.i("Caption", caption); 
    return bitmap; 

} 
+1

您是不是指工作不正常?你有沒有得到任何異常?如果它不起作用,你需要檢查你的text.equals(「好」)條件是否有效。 – ridoy

+0

TRY cs.setBitMap(bitmapx);相反... –

+0

不,它沒有顯示可繪製的圖像。 –

回答

1

下面的代碼將從資源繪製一個Drawable到從位圖創建的畫布上(在這種情況下,從相機預覽位圖)。這已經過測試,並且與API 22+一起使用(尚未使用較早版本進行測試):

  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length, opt); 
      Canvas camImgCanvas = new Canvas(cameraBitmap); 
      Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.myDrawable); 
      //Centre the drawing 
      int bitMapWidthCenter = cameraBitmap.getWidth()/2; 
      int bitMapheightCenter = cameraBitmap.getHeight()/2; 
      d.setBounds(bitMapWidthCenter, bitMapheightCenter, bitMapWidthCenter+d.getIntrinsicWidth(), 
        bitMapheightCenter+d.getIntrinsicHeight()); 
      //And draw it... 
      d.draw(camImgCanvas);