如何在自定义视图上使用ShapeDrawable和PathShape来绘制一条线?

6

我试图在自定义的View中绘制一条线。我创建了一个简单的Path,只有一个线段,从中创建了一个PathShape,最后将其放入ShapeDrawable中,以便在onDraw()内部使用它来绘制Canvas。然而,这并不起作用。请看我的示例,这里

package com.example.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.Log;
import android.view.View;

public class TestView extends View {

    private Path mPath = null;
    private Paint mPaint = null;
    private PathShape mPathShape = null;
    private ShapeDrawable mShapeDrawable = null;

    public TestView(Context context) {
        super(context);
    }

    private void init() {
        int width = this.getWidth() / 2;
        int height = this.getHeight() / 2;

        Log.d("init", String.format("width: %d; height: %d", width, height));

        this.mPath = new Path();
        this.mPath.moveTo(0, 0);
        this.mPath.lineTo(width, height);

        this.mPaint = new Paint();
        this.mPaint.setColor(Color.RED);

        this.mPathShape = new PathShape(this.mPath, 1, 1);

        this.mShapeDrawable = new ShapeDrawable(this.mPathShape);
        this.mShapeDrawable.getPaint().set(this.mPaint);
        this.mShapeDrawable.setBounds(0, 0, width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        // Doing this here because in the constructor we don't have the width and height of the view, yet
        this.init();
    }

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

        Log.d("onDraw", "Drawing");

        // This works, but won't let me do what I'm really trying to do
        canvas.drawLine(0.0f, 0.0f, this.getWidth() / 2.0f, this.getHeight() / 2.0f, this.mPaint);

        // This should work, but does not
        //this.mPathShape.draw(canvas, this.mPaint);

        // This should work, but does not
        //this.mShapeDrawable.draw(canvas);
    }

}

如您在onDraw()方法中的评论中所述,使用PathShapeShapeDrawable来绘制PathCanvas上实际上都不起作用。当我尝试时没有任何东西被绘制出来。有人知道为什么吗?
我正在测试这个设备上运行的Android 4.1.1。

只是好奇 - 你有没有尝试在onDraw中创建一个新的Paint对象。我正在这样做,并且使用位图取得了成功,但不确定是否必要。 - JavaCoderEx
我之前没有这样做,因为我不想在onDraw中创建对象(这实际上会导致Lint警告)。然而,我刚刚尝试了一下,结果还是一样。没有成功。 - Daniel
经过进一步调查和在这里缺乏回应后,我认为这只是出了问题,因此我已经提交了一个错误报告。http://code.google.com/p/android/issues/detail?id=35229 - Daniel
1个回答

13

这里有两个问题。

第一个问题是Paint样式。默认为Paint.Stroke.FILL,但是对于一条线来说没有什么可以填充的。我需要添加这个(感谢Romain Guy):

this.mPaint.setStyle(Paint.Style.STROKE);

第二个问题是PathShape中标准的高度和宽度不正确。我已经阅读了文档,但没有正确理解。一旦我解决了第一个问题,这就变得明显了。将其设置为我的自定义视图的高度和宽度(因为我正在整个视图上绘制)可以解决这个问题。我还必须更改ShapeDrawable的边界以匹配。

this.mPathShape = new PathShape(this.mPath, this.getWidth(), this.getHeight());

this.mShapeDrawable.setBounds(0, 0, this.getWidth(), this.getHeight());

希望这对未来的某些人有所帮助。


1
谢谢!我也遇到了完全相同的问题。 - Achal Dave
2
另一个可能导致它不出现的问题是,如果您路径的尺寸大于最大纵横比,则会在LogCat中产生警告。 - Steven Feldman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接