Drawing custom view using Android

前端 未结 1 1667
时光取名叫无心
时光取名叫无心 2021-02-06 03:09

I am drawing a custom view in my application which basically takes arguments(XML) as the text to display and then keeps on rotating it infinitely.

While I was making thi

1条回答
  •  遥遥无期
    2021-02-06 03:57

    why would I need those if I am already using my custom attributes to define the height and the width of my view?

    This is because Android needs to know how and where to put your view in the layout. You can implement your view to make use of your custom height/width requirements by overriding View.onMeasure() to return your own size parameters. You can then just set android:layout_width="wrap_content" android:layout_height="wrap_content" in your layout file.

    Why is the autocomplete not popping up? It does pop up when I am making a.java file.

    Autocomplete and autodoc implementation for Android xml files is shaky at best, so it's not really surprising that it doesn't work. I'm not aware of a fix and AFAIK it's an IDE bug.

    In my ondraw() method I wanted to draw a circle. Now for this I would require the co-ordinates of the center of the circle. So what I do is put 0, 0. But this is not correct. I want the circle to be at the center of the parent view it is in. So if my view is in a hierarchy I want it to respect that. I want to give the co-ordinates relative to the parent view. What is happeining right now is that it is being drawn at the top left corner of my screen. This is what I don't want. What do I do to achieve this?

    If you implemented onMeasure() correctly, the coordinates relative to parent should be taken care of already. To get the center of your view use the following code:

    void onDraw(Canvas canvas)
    {
    int centerX = this.getWidth() / 2;
    int centerY = this.getHeight()) / 2;
    //..draw code here
    }
    

    Edit
    Here's an example with source code that should help: http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/

    0 讨论(0)
提交回复
热议问题