在LinearLayout中有2个图像。我想修复一个并缩放另一个图像

时间:2018-04-13 04:58:07

标签: java android image android-image masking

我有一个应用程序,我将一个图像屏蔽在另一个图像上。现在我想要的是底部图像应该被修复,我应该能够缩放或调整我想要掩盖的上面的图像。任何想法/帮助都会很棒。

以下是XML代码: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.example.jervi.mytestapp2.EditCase">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/zoom"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_gravity="center"
        android:src="@drawable/iphonex" />

    <Button
        android:id="@+id/load_gallery"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Gallery" />

</LinearLayout>

这是Java代码: -

public class EditCase extends AppCompatActivity实现了View.OnClickListener {

ImageView zoom;
private static final int PICK_IMAGE = 100;
Button gallery;
Uri imageUri;
Bitmap new_bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_case);

    gallery = findViewById(R.id.load_gallery);
    gallery.setOnClickListener(this);

    zoom = findViewById(R.id.zoom);
    PhotoViewAttacher photoView = new PhotoViewAttacher(zoom);
    photoView.update();
}

//On click event for gallery
@Override
public void onClick(View v) {
    if (v == gallery) {
        openGallery();
    }
}

//Opening the gallery
private void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

//On it's result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        zoom.setImageURI(imageUri);
        try {
            new_bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
        }
        makecustom_shapeImageScaleFit(zoom, new_bitmap);
    }
}

//This is the masking method
public void makecustom_shapeImageScaleFit(ImageView mImageView, Bitmap bitmap) {
    // Get custom_shape bitmap
    //String getDevice = device_name;
    Bitmap custom_shape = BitmapFactory.decodeResource(getResources(), R.drawable.iphonex);

    //Scale imageView and it's bitmap to the size of the custom_shape
    mImageView.getLayoutParams().width = custom_shape.getWidth();
    mImageView.getLayoutParams().height = custom_shape.getHeight();

    // Get bitmap from ImageView and store into 'original'
    mImageView.setDrawingCacheEnabled(true);
    mImageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    mImageView.layout(0, 0, mImageView.getMeasuredWidth(), mImageView.getMeasuredHeight());
    mImageView.buildDrawingCache(true);
    //Bitmap original = BitmapFactory.decodeResource(getResources(), mContent);

    // Scale that bitmap
    Bitmap original_scaled = Bitmap.createScaledBitmap(bitmap, custom_shape.getWidth(), custom_shape.getHeight(), false);
    mImageView.setImageBitmap(original_scaled);
    mImageView.setDrawingCacheEnabled(false);

    // Create result bitmap
    Bitmap result = Bitmap.createBitmap(custom_shape.getWidth(), custom_shape.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform custom_shaping
    Canvas mCanvas = new Canvas(result);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    mCanvas.drawBitmap(original_scaled, 0, 0, null);
    mCanvas.drawBitmap(custom_shape, 0, 0, paint);
    paint.setXfermode(null);

    // Set imageView to 'result' bitmap
    mImageView.setImageBitmap(result);
    mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

    // Make background transparent
    mImageView.setBackgroundResource(android.R.color.transparent);
}

}

Click here to view the mock image.

0 个答案:

没有答案