本文介绍了疑惑采用了android绘制自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画我的应用程序自定义视图基本上接受参数(XML)来显示文本,然后不断旋转无限它。

当我做这个控件我有,我想问几个疑点:

  • 我做了2我,我在 attrs.xml 文件已经宣布风格化的属性。这些属性是来设置宽度和在我的控制中使用的圆的宽度。这些价值观我将在OnDraw的方法和onmeasure方法使用。我跑我的程序通过声明只是这些,但有它要求我把 Android的一个错误:宽度和android:高度属性。我的问题是,为什么当我已经用我的自定义属性来定义的高度和我的观点的宽度,我需要那些

  • 我编码在Mac上。现在,当我正在我的 attrs.xml 文件,以获取自动完成列表中,我们通常会按ctrl +空格是没有显示出来。这是为什么。对于例如,我想知道什么样的价值观,我可以给我的,像我demostrating以下的自定义属性的格式属性:< attr指示NAME =somename格式=值我想查找通过自动完成> 。为什么不自动完成雨​​后春笋般冒出来?它弹出时,我正在a.java文件。

  • 在我的的OnDraw()方法,我想画一个圆。现在此我将要求坐标的圆的中心。因此,我要做的就是把0,0。但是,这是不正确的。我想圆是在父视图它是在市中心。所以,如果我的观点是在一个层次,我希望它尊重这一点。我想给坐标相对于父视图。什么是happeining现在的问题是,它被画在我的屏幕的左上角。这是我不想要的东西。我该怎么做才能实现这一目标?

解决方案

这是由于Android需要知道如何以及在哪里把你的观点的布局。你可以实现你的观点被覆盖的返回自己的尺寸参数。然后,你可以设置机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT在布局文件

自动完成和车博士实现Android的XML文件是不稳固充其量,所以它不是真正奇怪,这是行不通的。我不知道有修复和AFAIK它是一个IDE错误。

如果您实现onMeasure()正确的,相对于父坐标应采取的已经照顾。为了让您的视图中心使用下面的code:

 无效的OnDraw(帆布油画)
{
INT的centerX = this.getWidth()/ 2;
INT centerY = this.getHeight())/ 2;
//..draw code在这里
}
 

修改
这里有一个例子与源$ C ​​$ C,应该帮助:的

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:

  • 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 put android: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

  • 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.

  • 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?

解决方案

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.

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.

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/

这篇关于疑惑采用了android绘制自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:20
查看更多