I have a view I need to flip vertically, or mirror. There\'s plenty of information out there about mirroring a single bitmap by scaling it by -1 and translating for the offset,
Transfer View into Bitmap, then flip it using method below:
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;
}
You could simply create a Canvas out of a Bitmap
and then invoke your root view's View.draw(Canvas) method. This will give you a snapshot of the view hierarchy in a Bitmap
. You then apply the aforementioned transformations to mirror the image.