你是如何应该使用一个ShapeDrawable用PathShape上绘制自定义视图行? [英] How are you supposed to use a ShapeDrawable with a PathShape to draw a line on a custom View?

查看:237
本文介绍了你是如何应该使用一个ShapeDrawable用PathShape上绘制自定义视图行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制自定义视图行。在这里,我创建了一个简单的路径只是一个单一的领域,创造了从一个PathShape,终于卡住了成ShapeDrawable使用,要在画布上画里面的onDraw的意图()。但是,这是行不通的。见我的例子,在这里。

I am attempting to draw a line in a custom View. Here I've created a simple Path with just a single segment, created a PathShape from that, and finally stuck that into a ShapeDrawable with the intention of using that to draw on the Canvas inside onDraw(). However, this does not work. See my example, here.

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看到的评论,既没有使用PathShape也不ShapeDrawable绘制路径到Canvas实际工作。当我尝试什么也不绘制。没有人有任何想法,为什么?

As you can see from my comments in the onDraw method, neither using the PathShape nor the ShapeDrawable to draw the Path onto the Canvas actually works. Nothing is drawn when I try. Does anyone have any idea why?

我在测试这个设备运行的是Android 4.1.1。

The device I'm testing this on is running Android 4.1.1.

推荐答案

有两个问题与此有关。

首先就是画图的风格。默认值是Paint.Stroke.FILL,但有一条线,没有什么来填补。我需要添加这个(感谢,罗曼盖伊):

The first is the Paint style. The default is Paint.Stroke.FILL, but with a line there's nothing to fill. I needed to add this (thanks, Romain Guy):

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

的第二个问题是,在PathShape标准高度和宽度是不正确的。我读<一个href=\"http://developer.android.com/reference/android/graphics/drawable/shapes/PathShape.html#PathShape%28android.graphics.Path,%20float,%20float%29\">the文档这一点,但没有正确地理解它。这显然​​有一次我固定的第一个问题。它设置的高度和我的自定义视图的宽度(因为我在整个观图)解决了这个问题。我也不得不改变ShapeDrawable的界限相匹配。

The second issue is that the standard height and width in the PathShape is not correct. I had read the documentation on this, but did not understand it correctly. This became apparent once I fixed the first issue. Setting it to the height and width of my custom view (since I am drawing across the whole view) fixed this. I also had to change the bounds of the ShapeDrawable to match.

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

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

希望这可以帮助别人,将来别人。

Hopefully this helps someone else in the future.

这篇关于你是如何应该使用一个ShapeDrawable用PathShape上绘制自定义视图行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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