Words don't say more first:
Similar to this effect, very simple. This is a listview,item is a custom view, with two features:
- The length of the histogram is changed dynamically by passing the length;
- Change the color value according to the length, the longer the red, the more yellow.
Use of the knowledge point is nothing more than paint on canvas painting, here do not repeat, want to know but do not know the children's shoes can read my previous blog.
Portal :http://blog.csdn.net/zhaoyingkun/article/details/44340365
One of the most important places to mention here is the use of the Android namespace.
How did it come to mind that I found out when I wanted to pass a value to a custom view if you use the Setxxx () method only in the usual way, this assumes that the view object has been created and that you need to invoke the Setxxx method proactively. How can you create this view from a given value at the initial time, like a constructor method? Because view is used in XML, the constructor method cannot be called. So what do we do when we need to use namespaces? That is, like android:layout_height= "20DP" in XML to assign values to the view, this is the level two structure of the front of Android is the namespace, but this is Android itself. All we need to do is define a namespace and we can use it.
First, create a attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Barchartview" > <attr name= "text" format= "string"/> <!--bar_height represents the height of the defined Barchartview and <attr name= "Bar_height" format= "integer"/> <!--bar_width for defined barchartview width-- <attr name= "Bar_width" format= "integer"/> </declare-styleable></resources>
Like This,declare-styleable, as the name implies, declares a styleable type, and in our Java code below, we also need to extract the attributes from this side.
Here we define two int values: Bar_height,bar_width, a string value of text. We'll be able to use them right here.
Second, the layout file
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" <span style= "color: #ff0000;" >xmlns:myxmlns= "Http://schemas.android.com/apk/res/com.example.simple" </span> android:layout_ Width= "Wrap_content" android:layout_height= "match_parent" > <com.example.simple.barchartview Android:id= "@+id/cv_chart" android:layout_width= "wrap_content" android:layout_height= "20DP " Android:layout_centervertical= "true" <span style= "color: #ff0000;" >myxmlns:bar_height= "</span> > </com.example.simple.barchartview></relativelayout" >
This is the view in the ListView as item, note the red part, xmlns:myxmlns= "Http://schemas.android.com/apk/res/com.example.simple". The rule used is to first define the namespace xmlns:namespace-prefix= "NamespaceURI". The use of XML in Android is: xmlns: prefix =http://schemas.android.com/apk/res/application package path, then use by format: Namespace-prefix (prefix): property
Myxmlns can write casually, but be consistent with the following. Here we can use the attributes defined in the above attrs.xml, including Bar_height,bar_width,text and so on above your custom properties.
Third, use our pre-set properties in the code
Public Barchartview (context context, AttributeSet Attrs) { Super (context, attrs); TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.barchartview); Chartheight = A.getint (r.styleable.barchartview_bar_height); Chartwidth = A.getresourceid (R.styleable.barchartview_bar_width, ten); A.recycle (); Chartheight = displayutils.dp2px (context, chartheight); }
We can use the above method to get our preset values in our custom view construction method, using the attributes we need in the styleable we declared in the first step.
So our goal is achieved, using the default attributes in XML, no longer using the Setxxx method to pass parameters in the view.
Code on GitHub, children's shoes need to be downloaded by themselves:
Portal: Https://github.com/yocn/chartView.git
Https://github.com/yocn/chartView
Customize a horizontal histogram with the Android namespace