Android: mirroring

I have a view that I need to flip vertically or a mirror. There is a lot of information about mirroring a single bitmap image by scaling it by -1 and translating it to an offset, as described here , but there seems to be no information on how to draw all the contents of the View - in particular, all its sub-items - upside down.

I have several subviews in this container - text, images - and I was hoping for a way that would allow me to simply add them in one view and draw this View upside down / sideways, instead of all of them executing custom drawing code so that drag them upside down and move them into the container accordingly. Any ideas?

0
source share
2 answers

You can simply create Canvasfrom Bitmapand then call your root view View.draw(Canvas). This will give you a snapshot of the views hierarchy Bitmap. Then you apply the above transforms to mirror the image.

+2
source

Drag the view into a bitmap, then flip it using the following method:

private static Bitmap getBitmapFromView(View view,int width,int height) {
    int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    view.layout(0, 0, width, height);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}


private static Bitmap flipBitmap(Bitmap src)
{
    Matrix matrix = new Matrix();
    matrix.preScale(-1, 1);
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return dst;
}
+1
source

Source: https://habr.com/ru/post/1648059/


All Articles