如何将画布置于其他视图之上

时间:2016-06-30 13:04:10

标签: android canvas magnification magnify

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

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


    }
}

}

1 个答案:

答案 0 :(得分:1)

ViewGroupView方法中绘制其子dispatchDraw()。我们只需要将放大镜绘图移动到那之后。

修复很简单。在super之后的onDraw()来电之后移至super dispatchDraw() ... @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()

如果您不再需要其他任何内容,则可以删除SELECT u.userid, COUNT(m.id) FROM message m INNER JOIN message_thread t ON m.thread_id = t.id CROSS JOIN (SELECT 12645 as userid UNION ALL SELECT 985 UNION ALL SELECT 8956 UNION ALL SELECT 37856 ) u WHERE m.created_by_id <> u.userId AND t.id IN (SELECT t2.id FROM message_thread t2 INNER JOIN message_participants p ON p.thread_id = t2.id WHERE p.user_id = u.userId ) GROUP BY u.userId; 覆盖。