微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在画布上以高质量绘制自定义画笔画

如何解决如何在画布上以高质量绘制自定义画笔画

我想使用下面的代码绘制图片,并为画布自定义画笔。我成功了,但是在保存时,我根据画布大小创建了位图,但图片质量下降了,我该如何防止?

在画布上绘图时; click to see the picture

将绘图转换为位图后; click to see the picture

public class ObjectPricingSetting {
  @JsonCreator
  public ObjectPricingSetting(
     @JsonProperty("pricing_type") final ObjectPricingType pricingType,@JsonProperty("rate") final BigDecimal ownRate) {
  ...

我使用以下代码将我的自定义画笔绘图转换为位图。

@Override
protected void onDraw(Canvas canvas) {
    for (BrushLinePath linePath : mDrawnPaths) {
        canvas.drawPath(linePath.getDrawPath(),linePath.getDrawPaint());
    }
    canvas.drawPath(mPath,mDrawPaint);
    /////
    final Bitmap scaledBitmap = getScaledBitmap();

    final float centerX = scaledBitmap.getWidth() / 2;
    final float centerY = scaledBitmap.getHeight() / 2;

    final PathMeasure pathMeasure = new PathMeasure(mPath,false);

    float distance = scaledBitmap.getWidth() / 2;

    float[] position = new float[2];
    float[] slope = new float[2];

    float slopeDegree;

    while (distance < pathMeasure.getLength())
    {
        pathMeasure.getPosTan(distance,position,slope);
        slopeDegree = (float)((Math.atan2(slope[1],slope[0]) * 180f) / Math.PI);
        canvas.save();
        canvas.translate(position[0] - centerX,position[1] - centerY);
        canvas.rotate(slopeDegree,centerX,centerY);
        canvas.drawBitmap(scaledBitmap,mDrawPaint);
        canvas.restore();
        distance += scaledBitmap.getWidth() + 10;
    }

}

private Bitmap getScaledBitmap()
{
    // width / height of the bitmap[
    float width = brushBitmap.getWidth();
    float height = brushBitmap.getHeight();

    // ratio of the bitmap
    float ratio = width / height;

    // set the height of the bitmap to the width of the path (from the paint object).
    float scaledHeight = mDrawPaint.getstrokeWidth();

    // to maintain aspect ratio of the bitmap,use the height * ratio for the width.
    float scaledWidth = scaledHeight * ratio;

    // return the generated bitmap,scaled to the correct size.
    return Bitmap.createScaledBitmap(brushBitmap,(int)scaledWidth,(int)scaledHeight,true);
}

我尝试通过创建位图在画布上绘制图片,但该方法给出了相同的结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。