Android: Mirroring a View

前端 未结 2 1051
北恋
北恋 2021-01-28 13:02

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,

相关标签:
2条回答
  • 2021-01-28 13:12

    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;
    }
    
    0 讨论(0)
  • 2021-01-28 13:28

    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.

    0 讨论(0)
提交回复
热议问题