Question

I set the ImageResource of an ImageButton programmatically, while itself is created in xml:

        <ImageButton
            android:id="@+id/id_of_image_button"
            android:layout_width="88dp"
            android:layout_height="88dp"
            android:layout_below="@+id/id_of_other_image_button"
            android:layout_centerHorizontal="true"
            android:background="@drawable/background_of_image_button"
            android:contentDescription="@string/description_of_image_button"
            android:onClick="onButtonClick"
            android:scaleType="fitCenter" />

in java I set the src (depends on other code...)

    ImageButton ib = (ImageButton) findViewById(R.id.id_of_image_button);
    ib.setImageResource(R.drawable.src_of_image_button);

How can I mirror the ImageResource (src only, NOT the background)? Is there any solution (in Java/XML) which doesn't blow up the simple code? ;)

Was it helpful?

Solution

Add this method to your code

private final static Bitmap makeImageMirror(final Bitmap bmp)
{
    final int width = bmp.getWidth();
    final int height = bmp.getHeight();

    // This will not scale but will flip on the X axis.
    final Matrix mtx = new Matrix();
    mtx.preScale(-1, 1);

    // Create a Bitmap with the flip matrix applied to it.
    final Bitmap reflection = Bitmap.createBitmap(bmp, 0, 0, width, height, mtx, false);

    // Create a new Canvas with the bitmap.
    final Canvas cnv = new Canvas(reflection);

    // Draw the reflection Image.
    cnv.drawBitmap(reflection, 0, 0, null);

    //
    final Paint pnt = new Paint(Paint.ANTI_ALIAS_FLAG);
    // Set the Transfer mode to be porter duff and destination in.
    pnt.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    // Draw a rectangle using the paint.
    cnv.drawRect(0, 0, width, height, pnt);

    return reflection;
}

Then, get your mirrored image like so:

    final ImageView imgMirror = (ImageView) findViewById(R.id.imgMirror);
    imgMirror.setImageBitmap
    (
        makeImageMirror
        (
            BitmapFactory.decodeResource(getResources(), R.drawable.head_prof)
        )
    );

Result:

enter image description here

[EDIT]

You can get the VERTICAL mirror by using this matrix: mtx.preScale(1, -1);
You can get the HORIZONTAL + VERTICAL mirror by using this matrix: mtx.preScale(-1, -1);

OTHER TIPS

If you want to flip it in the code you can do something like this:

BitmapDrawable flip(BitmapDrawable d)
{
    Matrix m = new Matrix();
    m.preScale(-1, 1);
    Bitmap src = d.getBitmap();
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return new BitmapDrawable(dst);
}

You can switch the preScale values if you want to mirror on the X axis.

Try this

Assuming you are going to mirror on Y axis

private void mirrorMatrix(){
       float[] mirrorY = {  -1, 0, 0, 
                       0, 1, 0,  
                       0, 0, 1    
                   };
       matrixMirrorY = new Matrix();
       matrixMirrorY.setValues(mirrorY);    
   }

then get the mirrored bitmap and set it to the next imageButton.

private void drawMatrix()
{

  Matrix matrix = new Matrix();
  matrix.postConcat(matrixMirrorY); 

  Bitmap mirrorBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
  ib.setImageBitmap(mirrorBitmap);
} 

Note: mirrorY with the Matrix.postConcat() generates a mirror image about Y axis.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top