为什么我的android视图的宽度为0?

时间:2015-10-04 00:40:29

标签: java android canvas random drawing

我创建了一个名为CanvasView的自定义视图。我可以使用此视图在onDraw方法之外绘制内容。这意味着我可以在代码中的任何位置添加线条,圆圈,我想要的任何内容。

我想在onCreate方法的画布视图上绘制一条随机线。我希望起点位于视图的左半部分,而结束点位于右半部分。但是,当我使用getWidth测量视图的宽度时,它返回0!然后我将这个数字传递给Random.nextInt并进行更多计算。并导致负数。 IllegalArgumentException按预期发生。

我只是想问为什么我的视图宽度为0?我怎样才能得到正确的宽度?

以下是CanvasView类:(我认为addShapeonDraw以外的方法无关紧要)

public class CanvasView extends View {
    /**
     * Stores all the shapes that the view will draw in its {@code onDraw()}
     * method
     */
    private ArrayList<Shape> shapes;

    /**
     * Represents a standard {@code Paint} object that should be used when
     * drawing on this {@code CanvasView}.
     */
    private Paint paint;

    public CanvasView(Context c) {
        super (c);
        init ();
    }

    public CanvasView(Context context, AttributeSet attrs) {
        super (context, attrs);
        init ();
    }

    public CanvasView(Context context, AttributeSet attrs, int defStyleAttr) {
        super (context, attrs, defStyleAttr);
        init ();
    }

    /**
     * Initializes the view, called by the constructor.
     */
    private void init() {
        shapes = new ArrayList<> ();
        paint = new Paint ();
        paint.setStrokeWidth (5);
        paint.setColor (Color.BLACK);
    }

    @Override
    public void setOnTouchListener(final OnTouchListener listener) {
        final OnTouchListener baseListener = new OnTouchListener () {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction () == MotionEvent.ACTION_DOWN) {

                    float x = event.getX ();
                    float y = event.getY ();

                    Log.d ("My App", "X: " + x);
                    Log.d ("My App", "Y: " + y);

                    //check whether the point touched is in bounds
                    if (x < 18 || x > getWidth () - 18 || y < 18 ||
                            y > getHeight () - 18)
                        return false;
                    else
                        return true;
                }
                return false;
            }
        };

        super.setOnTouchListener (new OnTouchListener () {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (baseListener.onTouch (v, event)) {
                    if (listener != null) {
                        return listener.onTouch (v, event);
                    } else {
                        return true;
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw (c);
        for (Shape s : shapes) {
            s.draw (c);
        }
        //draw the border.
        c.drawLine (3, 3, getWidth () - 3, 3, paint);
        c.drawLine (3, getHeight () - 3, getWidth () - 3, getHeight () - 3, paint);
        c.drawLine (3, 3, 3, getHeight () - 3, paint);
        c.drawLine (getWidth () - 3, 3, getWidth () - 3, getHeight () - 3, paint);

        //draw the inner border
        c.drawLine (18, 18, getWidth () - 18, 18, paint);
        c.drawLine (18, getHeight () - 18, getWidth () - 18, getHeight () - 18, paint);
        c.drawLine (18, 18, 18, getHeight () - 18, paint);
        c.drawLine (getWidth () - 18, 18, getWidth () - 18, getHeight () - 18, paint);
    }

    /**
     * Adds a shape to the {@code CanvasView} so that it can be drawn
     * in its {@code onDraw()} method.
     *
     * @param s
     */
    public void addShape(Shape s) {
        shapes.add (s);
        invalidate ();
    }

    /**
     * Clears all the shapes on the {@code CanvasView}.
     */
    public void clear() {
        shapes.clear ();
    }
}

我创建了一个Shape接口,它只有这个方法:

void draw (Canvas c);

以下是我绘制随机行的方法,这个snippt位于onCreate中调用的方法中:

Random r = new Random ();
final int width = canvas.getWidth () - 30;
final int height = canvas.getHeight () - 30;
final Paint p = paint;

final int startX = r.nextInt ((width - 30) / 2) + 30;
final int startY = r.nextInt (height - 30) + 30;
final int stopX = r.nextInt ((width - 30) / 2) + width / 2 + 30;
final int stopY = r.nextInt (height - 30) + 30;

float adjSide = Math.abs (stopX - startX);
float oppSide = Math.abs (stopY - startY);
lineAngle = (float)Math.atan (oppSide / adjSide);

canvas.addShape (new Shape () {
    @Override
    public void draw(Canvas c) {
        c.drawLine (startX, startY, stopX, stopY, p);
    }
});

修改

我根据评论建议改变了我的代码。

我首先夸大父布局,如下所示:

getLayoutInflater ().inflate (R.layout.activity_parallel_lines_level,
    parent);

变量parent是您可能已经猜到的CanvasView的父布局。然后我测量CanvasView的父级的尺寸:

parent.measure (View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

现在出现了一些非常奇怪的事情,它成功地解决了宽度,但不是高度!我不知道这是否与LinearLayout是垂直的from selenium import webdriver fp = webdriver.FirefoxProfile('') driver = webdriver.Firefox(firefox_profile=fp) driver.set_window_size(1400, 1000) driver.get('') elem1 = driver.find_element_by_xpath("//img[@title='img1']") elem = driver.find_element_by_xpath("//img[@title='img2']") if elem.is_displayed(): elem.click() print "true" elif elem1.is_displayed(): elem1.click() print "1true" else: print "false" 有关。我该怎么办?

0 个答案:

没有答案