Я добавил пример макета в вопрос. Поэтому, пожалуйста, любая помощь будет оценена по достоинству.
Изображение макета устройства в формате PNG.
Код: Выделить всё
public class ClippedImageView extends androidx.appcompat.widget.AppCompatImageView {
private Bitmap mOriginalBitmap;
private Bitmap mMaskBitmap;
public ClippedImageView(Context context) {
super(context);
}
public ClippedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClippedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mOriginalBitmap != null && mMaskBitmap != null) {
// Step 1: Prepare a paint object with Xfermode for clipping
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// Step 2: Draw the original image onto the canvas
canvas.drawBitmap(mOriginalBitmap, 0, 0, null);
// Step 3: Apply the mask (draw the mask on top of the image)
canvas.drawBitmap(mMaskBitmap, 0, 0, paint); // This will clip the original image
}
}
// Method to set the original image (the content to be shown inside the phone screen)
public void setOriginalBitmap(Bitmap originalBitmap) {
this.mOriginalBitmap = originalBitmap;
invalidate(); // Redraw the view
}
// Method to set the mobile mask image (the frame with transparent screen)
public void setMaskBitmap(Bitmap maskBitmap) {
this.mMaskBitmap = maskBitmap;
invalidate(); // Redraw the view
}
}
Код: Выделить всё
Код: Выделить всё
Bitmap maskBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mobile_temp); ((ClippedImageView) screen_base).setOriginalBitmap(bitmap); ((ClippedImageView) screen_base).setMaskBitmap(maskBitmap);
Но результат, который я получаю, совершенно неправильный. .
Это результат, который я получаю из приведенного выше кода
Подробнее здесь: https://stackoverflow.com/questions/793 ... ile-mockup
Мобильная версия