【问题标题】:How to bring canvas on top of other views如何将画布置于其他视图之上
【发布时间】:2016-06-30 13:04:10
【问题描述】:

我正在为我的应用程序使用放大镜视图,这是我从这里获得的一个库:

https://github.com/nomanr/android-image-magnifier

我已经修改了这个类来扩展 FrameLayout(之前是 ImageView)来处理我的 FrameLayout。

除了绘制的画布视图保留在该自定义视图中添加的所有视图之外,它运行良好。

如何将画布放在这些添加的视图前面?

我正在使用的自定义视图类:

public class ImageMagnifier extends FrameLayout {

private PointF zoomPos;
private boolean zooming = false;
private Matrix matrix;
private Paint paint;
private Bitmap bitmap;
private BitmapShader shader;
private int sizeOfMagnifier = 300;
int cheight, cwidth;

Context con;
C c = C.getInstance();
public ImageMagnifier(Context context) {
    super(context);
    init();
    con=context;
}

public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    con=context;
}

public ImageMagnifier(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    con=context;
}

private void init() {
    zoomPos = new PointF(0, 0);
    matrix = new Matrix();
    paint = new Paint();

    cwidth = c.Width * 109 / 1280;
    cheight = cwidth * 134 / 109;
}

public void otherTouch(int x, int y) {
    if (x > c.getwidth1(28) && x < c.getwidth1(921) && y > c.getheight1(135) && y < c.getheight1(560)) {
        zoomPos.x = x - 10;
        zoomPos.y = y - 75;
        zooming = true;
        this.invalidate();

    } else {
        RemoveMagnifire();
    }
}

public void RemoveMagnifire() {
    zooming = false;
    this.invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (!zooming) {
        buildDrawingCache();
    } else {

        bitmap = getDrawingCache();
        shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);        
        paint.setShader(shader);
        matrix.reset();
        matrix.postScale(2f, 2f, zoomPos.x-10, zoomPos.y+60);

        paint.getShader().setLocalMatrix(matrix);

        int width = c.Width;
        int height = c.Height;

        float leftX = zoomPos.x - ((width * 100) / 1280);
        float topY = zoomPos.y - ((height * 250) / 720);
        float rightX = zoomPos.x +  ((width * 100) / 1280);
        float bottomY = zoomPos.y - ((height * 100) / 720);

        canvas.drawRect(leftX , topY, rightX, bottomY, paint);


    }
}

}

【问题讨论】:

  • 请发布您修改后的课程,以便我们确定您的意思。
  • 这里是 :: pastebin.com/AzcVEWG7
  • 好的,修复很简单,但是请在问题本身中发布课程。请不要链接到场外代码。未来的用户将需要能够查看它,并且在该链接失效后将无法查看。谢谢。
  • 好的,谢谢你的建议。我添加了有问题的代码。
  • @MikeM。如何将该视图置于其他视图之上?

标签: android canvas magnification magnify


【解决方案1】:

ViewGroupdispatchDraw() 方法中绘制其子Views。之后我们只需要将放大镜绘图移动到该位置即可。

修复很简单。将onDraw() 中的super 调用之后的所有内容移动到dispatchDraw() 中的super 调用之后。

...

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // Removed
}

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (!zooming) {
        buildDrawingCache();
    }
    else {
        bitmap = getDrawingCache();
        shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        matrix.reset();
        matrix.postScale(2f, 2f, zoomPos.x - 10, zoomPos.y + 60);

        paint.getShader().setLocalMatrix(matrix);

        int width = c.Width;
        int height = c.Height;

        float leftX = zoomPos.x - ((width * 100) / 1280);
        float topY = zoomPos.y - ((height * 250) / 720);
        float rightX = zoomPos.x +  ((width * 100) / 1280);
        float bottomY = zoomPos.y - ((height * 100) / 720);

        canvas.drawRect(leftX , topY, rightX, bottomY, paint);
    }
}

...

如果您不再需要 onDraw() 覆盖,则可以删除它。

【讨论】:

  • 好的,感谢您的解决方案。我们如何删除画布透明度? drawRect() 完美地绘制矩形,但它具有一定的透明度。
  • 是的,如果您没有为自定义View 设置背景,它会这样做,因为默认情况下它将具有透明背景。您可以在另一个 Rect 之前绘制另一个 Rect,具有相同的尺寸和位置,但使用不同的 Paint 对象,其颜色设置为您想要的任何颜色,使用 Paint#setColor() 方法。
  • 嗯,一个可能的解决方法是覆盖addView(View, int, ViewGroup.LayoutParams) 方法,并在那里调用destroyDrawingCache()。不过,不确定您是如何获得StackOverflowError 的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
相关资源
最近更新 更多