2017-06-19 4 views
0

カメラアクティビティオーバーレイで作業していましたが、矩形(不透明な黒色)の外側に不透明な色を作成しました。また、私は正常に大きな矩形の内側に小さな矩形を作成しました。下の写真をご覧ください。ご覧のように、小さな四角形の上に青色の背景があります。問題は、画像を覆わずに、画像の外側に青色の背景(ビットマップ)を作りたいということです。問題は、透明な塗りつぶしで画像を変更すると、青い背景がすべてを覆うことになります。青い背景は画像の外側だけを覆うようにするにはどうすればよいですか?私はすでにGoogleからすべての可能な答えを見つけることを試みたが、私のために運がない、多分私は間違ったアプローチで始まった、助言を必要とする。Android - キャンバスを使用してビットマップの外に背景色で画像オーバーレイを設定する

enter image description here

ここに私のコードがわかりまし

bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas osCanvas = new Canvas(bitmap); 

    RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight()); 

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setColor(getResources().getColor(R.color.opaque_black)); 
    paint.setAlpha(99); 
    osCanvas.drawRect(outerRectangle, paint); 


    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); 

    paint.setColor(Color.TRANSPARENT); 
    paint.setStyle(Paint.Style.FILL); 
    RectF r1 = new RectF(
      (float) (xStartingPoint), 
      (float) (yStartingPoint), 
      (float) (parentWidth), 
      (float) (parentHeight)); 

    osCanvas.drawRoundRect(r1, (float) (cornerRadiusRatio * parentHeight), 
      (float) (cornerRadiusRatio * parentHeight), paint); 

    paint.setStrokeWidth(strokeWidth); 
    paint.setColor(getResources().getColor(R.color.colorDeepSky)); 
    paint.setStyle(Paint.Style.STROKE); 

    RectF r2 = new RectF(
      (float) (xStartingPoint), 
      (float) (yStartingPoint), 
      (float) (parentWidth), 
      (float) (parentHeight)); 

    osCanvas.drawRoundRect(r2, (float) (cornerRadiusRatio * parentHeight), 
      (float) (cornerRadiusRatio * parentHeight), paint); 

    if (callbackMsg.equals(AppConstant.KTP_SELF)) { 
     paint.setColor(getResources().getColor(R.color.colorDeepSky)); 

     drawRect(osCanvas, paint, 
       (float) (xChildStartingPoint), 
       (float) (yChildStartingPoint), 
       (float) (childWidth + xChildStartingPoint + 0.3 * childWidth), 
       (float) (childHeight + 0.7 * yChildStartingPoint)); 
    } 

    RectF outerRectangle2 = new RectF(
    (float) xChildStartingPoint, 
    (float) yChildStartingPoint, 
    (float) (childWidth + xChildStartingPoint + 0.3 * childWidth), 
    (float) (childHeight + 0.7 * yChildStartingPoint)); 

    Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint2.setColor(getResources().getColor(R.color.blue)); 
    paint2.setAlpha(99); 
    osCanvas.drawRect(outerRectangle2, paint2); 

    Rect r3 = new Rect(
      (int) (xChildStartingPoint), 
      (int) (yChildStartingPoint), 
      (int) (childWidth + xChildStartingPoint + 0.3 * childWidth), 
      (int) (childHeight + 0.7 * yChildStartingPoint)); 

    paint2 = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); 
    paint2.setColor(Color.TRANSPARENT); 
    paint2.setStyle(Paint.Style.FILL); 
    Bitmap mbitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon_profile2); 
    osCanvas.drawBitmap(mbitmap, null, r3, paint2); 

おかげ

答えて

0

だ、私はDST_OUTモード

注意してPorterDuffを使用してい答え

が見つかりました:あなたは色ここ

でいっぱいの外側の領域を作りたい場合は、画像の背景を透明にし、色 使用DST_INでいっぱいの外側の領域にしたい場合は210を使用するDST_OUTは

var paint2 = Paint(Paint.ANTI_ALIAS_FLAG) 
     paint2.color = resources.getColor(R.color.opaque_black) 
     osCanvas.drawRect(outerRectangle2, paint2) 

     paint2 = Paint(Paint.ANTI_ALIAS_FLAG) 
     val r3 = Rect(
       xChildStartingPoint.toInt(), 
       yChildStartingPoint.toInt(), 
       (childWidth + xChildStartingPoint + 0.3 * childWidth).toInt(), 
       (childHeight + 0.7 * yChildStartingPoint).toInt()) 

     paint2.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN) 
     val mBitmap = BitmapFactory.decodeResource(resources, R.drawable.selfie_outline) 
     osCanvas.drawBitmap(mBitmap, null, r3, paint2) 
Kotlin

を使用してコード、イムです
関連する問題