问题描述
我正在我的应用程序中绘制一个自定义视图,该视图基本上将arguments(XML)作为要显示的文本,然后继续无限旋转它.
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 this control I had a few doubts which I want to ask:
-
我已经制作了两个我在
attrs.xml
文件中声明的可样式化属性.这些属性用于设置控件中使用的圆的宽度和宽度.我将在ondraw方法和onmeasure方法中使用这些值.我通过声明这些来运行我的程序,但是有一个错误要求我放置android:width和android:height
属性.我的问题是,如果我已经在使用自定义属性定义视图的高度和宽度,为什么我需要那些
I have made 2 of my stylable attributes which I have declared in the
attrs.xml
file. These attributes are to set the width and the width of the circle used in my control. These values I would use in the ondraw method and the onmeasure method. I ran my program by declaring just these but there was an error which asked me to putandroid:width and android:height
attributes. My question is why would I need those if I am already using my custom attributes to define the height and the width of my view
我正在Mac上进行编码.现在,当我制作我的 attrs.xml
文件时,要显示通常由ctrl + space来获取的自动完成列表并没有显示出来.这是为什么.例如,我想知道我可以给自定义属性的format属性提供哪些值,就像我在下面进行降级处理一样:< attr name ="somename" format ="value我想通过自动填充功能查找的内容">
.为什么自动弹出功能没有弹出?当我制作一个.java文件时,它确实会弹出.
I am coding on a mac. Now when I was making my attrs.xml
file, to get the autocomplete list which we usually get by ctrl+space was not showing up. Why is that. For e.g., i wanted to know what values can I give the format attribute of my custom attribute that like I am demostrating in the following:<attr name ="somename" format="value I wanted to find out through autocomplete">
. Why is the autocomplete not popping up? It does pop up when I am making a.java file.
在我的 ondraw()
方法中,我想绘制一个圆.现在,为此,我需要圆心的坐标.所以我要做的是0,0.但这是不正确的.我希望圆位于它所在的父视图的中心.因此,如果我的视图处于层次结构中,我希望它尊重它.我想给出相对于父视图的坐标.现在发生的现象是它绘制在屏幕的左上角.这就是我所不想要的.我要怎么做才能做到这一点?
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?
推荐答案
这是因为Android需要知道如何以及在布局中放置视图的位置.您可以通过覆盖 View.onMeasure()以返回您自己的尺寸参数.然后,您可以在布局文件中设置 android:layout_width ="wrap_content" android:layout_height ="wrap_content"
.
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.
Android xml文件的自动完成和自动文档实现充其量是不稳定的,因此它不起作用并不奇怪.我不知道有修复程序,而AFAIK是一个IDE错误.
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.
如果正确实现了onMeasure(),则相对于父级的坐标应已得到照顾.要获得视图的中心,请使用以下代码:
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
}
修改
这是一个源代码示例,应该可以帮助您: http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/
这篇关于使用Android绘制自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!