Android: Mirroring a View

前端 未结 2 1052
北恋
北恋 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;
    }
    

提交回复
热议问题