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

问题描述

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

在画布上绘图时; 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 (将#修改为@)