2014-04-15 111 views
0

如何使用Canvas在另一幅圖像中繪製圖像; 這樣的,看圖片:enter image description hereAndroid在另一幅圖像中繪製圖像

把它放在一個地圖應用程序V2這樣

marker = gmap.addMarker(new MarkerOptions().title("test") 
         .position(new LatLng(0, 0)) 
         .snippet("snipet test") 
         .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) 

我已經畫一幅畫在一個矩形這樣

  InputStream inputStream = connection.getInputStream(); 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap 
      Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
        bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
      Canvas canvas = new Canvas(output); 

      final int color = 0xff424242; 
      final Paint paint = new Paint(); 
      final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
      final RectF rectF = new RectF(rect); 


      paint.setAntiAlias(true); 
      canvas.drawARGB(0, 0, 0, 0); 
      paint.setColor(color); 
      canvas.drawOval(rectF, paint); 

      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
      canvas.drawBitmap(bitmap, rect, rect, paint); 

如何做這個 請幫我

回答

0

我發現這個問題的解決方案: 你把圖像中的然後用畫布圈起來,然後用確切尺寸調整結果大小,並將其放入標記圖像中。

public class IconMarker { 
public IconMarker(){} 

public Bitmap DrawMarker(String userid,int typeid,Resources rcs){ 

    // image from database: ...InputStream inputStream = connection.getInputStream(); 
    //Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
    Bitmap img1=new UserInfoBd().getPhotoProfil(userid); 
    if(img1==null) img1=BitmapFactory.decodeResource(rcs,R.drawable.espace_photo); 
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
    Bitmap bmp = BitmapFactory.decodeResource(rcs, 
      typeid); 
    Bitmap output = Bitmap.createBitmap(bmp.getWidth(), 
      bmp.getHeight(), Bitmap.Config.ARGB_8888); 

    Canvas canvas1 = new Canvas(output); 
    canvas1.drawBitmap(bmp, 0,0, null); 
    Bitmap output1 = Bitmap.createBitmap(img1.getWidth(), 
      img1.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output1); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, img1.getWidth(), img1.getHeight()); 
    final RectF rectF = new RectF(rect); 


    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawOval(rectF, paint); 

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(img1, rect, rect, paint); 
    Bitmap img=getResizedBitmap(output1,bmp.getHeight()*3/4-4,bmp.getWidth()*3/4); 

    canvas1.drawBitmap(img,(float)4.7,(float)3.5,null); 

    return output; 

} 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

    int width = bm.getWidth(); 

    int height = bm.getHeight(); 

    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

//創建矩陣用於操作

Matrix matrix = new Matrix(); 

//調整BIT MAP

matrix.postScale(scaleWidth, scaleHeight); 

//重新創建新的位圖

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 

} 

}

0

用你想要的框架的形狀爲你的畫布設置一個剪切路徑:

Path frame = new Path(); 
frame.addCircle(centerX, centerY, radius, Direction.CW); 
canvas.getClipBounds(oldClipBounds); //see below 
canvas.clipPath(frame); 

如果以後畫在畫布上的所有內容在圓圈外面都不可見,

如果需要額外的繪圖,它應該只發生超出這個框,你可以在以後通過保護它:

canvas.clipRect(oldClipBounds, Region.Op.REVERSE_DIFFERENCE); 
相關問題