Android:镜像视图

时间:2012-09-13 01:38:23

标签: android android-view

我有一个视图我需要垂直翻转或镜像。有很多关于镜像单个位图的信息,通过将其缩放-1并转换为偏移量,如here所述,但似乎没有关于如何绘制所有内容的任何信息。查看 - 特别是所有的子视图 - 颠倒了。

我在这个容器中有多个子视图 - 文本,图像 - 我希望有一种方法可以让我将它们添加到单个视图中并将该视图上下颠倒/侧向绘制,而不是让它们全部执行自定义绘图代码将它们颠倒绘制并让容器重新定位它们。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您只需在Bitmap中创建Canvas,然后调用根视图的View.draw(Canvas)方法即可。这将为您提供Bitmap中视图层次结构的快照。然后,您应用上述转换来镜像图像。

答案 1 :(得分:1)

将视图传输到位图,然后使用以下方法翻转:

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;
}