在硬件加速下缩放画布时,偏移路径模糊 [英] Blurry offset paths when canvas is scaled under hardware acceleration

查看:84
本文介绍了在硬件加速下缩放画布时,偏移路径模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用可缩放的画布,以便可以以米而不是像素为单位指定路径点.当我缩放画布时,然后在启用硬件加速的情况下使用path.lineTo()画一条线,这条线是模糊且偏移的.关闭硬件加速或canvas.drawLine()不会发生这种情况.

My application uses a canvas that I scale so that I can specify path points in meters instead of pixels. When I scale the canvas, then draw a line using path.lineTo(), with hardware acceleration on, the line is blurry and offset. This does not happen with hardware acceleration off or with canvas.drawLine().

以下是重现该问题的代码:

Here is the code to reproduce the problem:

package com.example.canvasproblem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new MyView(this));
    }

    class MyView extends View {
        Paint pen = new Paint();
        public MyView(Context context) {
            super(context);
            pen.setStyle(Paint.Style.STROKE);
            pen.setStrokeWidth(1f); // 1 meters wide
            //this.setLayerType(LAYER_TYPE_SOFTWARE, null);
        }
        protected void onDraw(Canvas canvas) {
            float width_meter = 10.0f; // width of canvas in meters
            float width_pxl = canvas.getWidth(); // width of canvas in pixels
            float height_pxl = canvas.getHeight(); // height of canvas in pixels

            canvas.save();
            canvas.translate(width_pxl/2, height_pxl/2); // make center of canvas (0,0)
            canvas.scale(width_pxl/width_meter, width_pxl/width_meter); // convert to meters

            // path
            Path path = new Path();
            path.moveTo(0, 0);
            path.lineTo(0, 4);
            canvas.drawPath(path, pen);

            // line
            canvas.drawLine(0, 0, 0, 4, pen);

            canvas.restore();
        }
    }
}

这是问题输出的屏幕截图(正确的drawLine()显示在lineTo()顶部):

Here is a screenshot of the problem output (the correct drawLine() is shown on top of lineTo()):

Screenshot.png

硬件是运行Android 4.1.1的1024x768平板电脑.处理器是Rockchip RK30.

The hardware is a 1024x768 tablet, running android 4.1.1. The processor is a Rockchip RK30.

我的首选是将Path与硬件加速配合使用,以实现点与速度之间的舍入连接.如果我做错了什么导致此问题,请告诉我.谢谢

My preference is to use Path's with hardware acceleration, for rounded joins between points and speed. Please let me know if I am doing something wrong to create this problem. Thank you

请保持温柔,这是我的第一篇帖子.

Be gentle, this is my first post.

推荐答案

这是硬件加速渲染器的限制.在变换之前,将路径以其原始大小进行栅格化.在您的情况下,路径将转换为1x4纹理.然后在绘制时缩放该纹理.要解决此问题,请直接使用Path.transform(Matrix)缩放您的路径.您也可以在构建路径时使用缩放坐标.

This is a limitation of the hardware accelerated renderer. Paths are rasterized at their native size before transform. In your case, the Path is transformed into a 1x4 texture. That texture is then scaled at draw time. To work around this, scale your Path directly by using Path.transform(Matrix). You can also use scaled coordinates when building the path.

这篇关于在硬件加速下缩放画布时,偏移路径模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆