PorterDuff.Mode. . Porter-Duff PorterDuff.Mode.DST_IN .
, , . -, :

, , Porter-Duff PorterDuff.Mode.DST_IN. Porter-Duff ( ), , , .

, , , . ( , , . , 1.0.) , , . .

:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView topImage = findViewById(R.id.bottomImage);
topImage.setImageBitmap(cropAndOverlay(topImage, R.drawable.smile,
R.drawable.smile_transparent));
}
private Bitmap cropAndOverlay(@NonNull ImageView imageView,
@DrawableRes int cropId, @DrawableRes int overlayId) {
Bitmap dst = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Bitmap src = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), Bitmap.Config.ARGB_8888);
Canvas resultCanvas = new Canvas(src);
Drawable drawable = ResourcesCompat.getDrawable(getResources(), cropId, null);
drawable.setBounds(0, 0, resultCanvas.getWidth(), resultCanvas.getHeight());
drawable.draw(resultCanvas);
Bitmap resultBitmap = getPorterDuffBitmap(dst, src, PorterDuff.Mode.DST_IN);
resultCanvas.setBitmap(resultBitmap);
drawable = ResourcesCompat.getDrawable(getResources(), overlayId, null);
drawable.setBounds(0, 0, resultBitmap.getWidth(), resultBitmap.getHeight());
drawable.draw(resultCanvas);
dst.recycle();
return resultBitmap;
}
private Bitmap getPorterDuffBitmap(Bitmap dst, Bitmap src, PorterDuff.Mode mode) {
Bitmap bitmap =
Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(dst, 0, 0, null);
Paint maskPaint = new Paint();
maskPaint.setXfermode(new PorterDuffXfermode(mode));
canvas.drawBitmap(src, 0, 0, maskPaint);
return bitmap;
}
}
XML:
activity_main.xml
<LinearLayout
android:id="@+id/mainLayout"
android:background="#b1a8a8"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/bottomImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/photo" />
</LinearLayout>
, , , .