Android Custom View Properties
Last Update:2016-04-12
Source: Internet
Author: User
<span id="Label3"></p><p><p>First Kind</p></p><p><p>/mainactivity/res/values/attrs.xml</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td><p><?xml version= "1.0" encoding= "utf-8"?><br><resources><br><declare-styleable name= "mycircle" ><br><attr name= "radius" format= "integer"/><br><attr name= "mycolor" format= "reference|color"/><br></declare-styleable><br></resources></p></td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table><p><p>/mainactivity/src/com/ch/myviews/mycircle.java</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td><p><p>Package com.ch.myviews;</p></p><p><p>Import com.ch.myviewattr.r;</p></p><p><p>Import android.content.Context;<br>Import android.content.res.TypedArray;<br>Import android.graphics.Canvas;<br>Import android.graphics.Color;<br>Import android.graphics.Paint;<br>Import android.util.AttributeSet;<br>Import android.view.View;</p></p><p><p>public class Mycircle extends View {<br>Private Paint paint;<br>private int radius;<br><br>Public mycircle (context context, attributeset attrs) {<br>Super (context, attrs);<br>TODO auto-generated Constructor stub<br>Get custom properties, that is, get a custom radius<br>TypedArray ta = context.obtainstyledattributes (attrs, r.styleable.mycircle);<br>Radius = Ta.getint (r.styleable.mycircle_radius, 0);<br><br><br>}<br>Method of Drawing<br>@Override<br>protected void OnDraw (canvas Canvas) {<br>TODO auto-generated Method Stub<br>Paint = new Paint ();<br>Paint.setcolor (color.red);<br><br>Canvas.drawcolor (color.black);<br>Canvas.drawcircle (getmeasuredwidth ()/2, getmeasuredheight ()/2, radius, paint);<br>Super.ondraw (canvas);<br>}<br><br></p></p><p><p>}</p></p></td> <td></td> </tr> </tbody> </table><p><p>/mainactivity/res/layout/activity_main.xml</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td><p><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>xmlns:tools= "http://schemas.android.com/tools"<br>Xmlns:myattr= "http://schemas.android.com/apk/res/com.ch.myviewattr"<br>Android:layout_width= "match_parent"<br>android:layout_height= "match_parent"<br>tools:context= ". Mainactivity "></p><p><com.ch.myviews.mycircle<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>Android:layout_centerhorizontal= "true"<br>Android:layout_centervertical= "true"<br>myattr:radius= "200"<br>/></p><p></RelativeLayout></p></td> <td></td> </tr> </tbody> </table><p><p>The second Kind</p></p><p><p>/mainactivity/res/values/attrs.xml</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td><p><?xml version= "1.0" encoding= "utf-8"?><br><resources><br><declare-styleable name= "mycircle" ><br><attr name= "radius" format= "integer"/><br><attr name= "mycolor" format= "reference|color"/><br></declare-styleable><br></resources></p></td> </tr> </tbody> </table><p><p>/mainactivity/src/com/ch/myviews/mycircle.java</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td><p><p>Package com.ch.myviews;</p></p><p><p>Import com.ch.myviewattr.r;</p></p><p><p>Import android.content.Context;<br>Import android.content.res.TypedArray;<br>Import android.graphics.Canvas;<br>Import android.graphics.Color;<br>Import android.graphics.Paint;<br>Import android.util.AttributeSet;<br>Import android.view.View;</p></p><p><p>public class Mycircle extends View {<br>Private Paint paint;<br>private int radius;<br>Private final static String NAMESPACE = "aaa1407a";<br>Public mycircle (context context, attributeset attrs) {<br>Super (context, attrs);<br>TODO auto-generated Constructor stub<br>Get custom properties, that is, get a custom radius<br><br>Radius = attrs.getattributeintvalue (NAMESPACE, "radius", 0);<br>}<br>Method of Drawing<br>@Override<br>protected void OnDraw (canvas Canvas) {<br>TODO auto-generated Method Stub<br>Paint = new Paint ();<br>Paint.setcolor (color.red);<br><br>Canvas.drawcolor (color.black);<br>Canvas.drawcircle (getmeasuredwidth ()/2, getmeasuredheight ()/2, radius, paint);<br>Super.ondraw (canvas);<br>}<br><br></p></p><p><p>}</p></p></td> </tr> </tbody> </table><p><p>/mainactivity/res/layout/activity_main.xml</p></p> <table style="background-color: #ffe4b5;" border="0"> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td><p><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>xmlns:tools= "http://schemas.android.com/tools"<br>Xmlns:myattr= "aaa1407a"<br>Android:layout_width= "match_parent"<br>android:layout_height= "match_parent"<br>tools:context= ". Mainactivity "></p><p><com.ch.myviews.mycircle<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>Android:layout_centerhorizontal= "true"<br>Android:layout_centervertical= "true"<br>myattr:radius= "200"<br>/></p><p></RelativeLayout></p></td> <td></td> </tr> </tbody> </table><p><p>Android Custom View Properties</p></p></span>