Android self-defined component Family "1"-define View and ViewGroup

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;">The view class is the parent of viewgroup, and ViewGroup has all the properties of the VIEW. ViewGroup is primarily used as a container for VIEW. Take the view as your own child and manage it. Of course the child can also be a viewgroup type.</span></strong></p></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;">The view class is typically used for paint operations, overriding its OnDraw method, but it cannot include other components, without the AddView (view view) method.</span></strong></p></p><p><p><span style="font-weight: bold;"><span style="font-family:FangSong_GB2312;font-size:14px;">ViewGroup is a component container that can include no matter what component, but must override OnLayout<span style="text-indent: 28px;">(boolean changed,int l,int t,int r,int b) and <span style="text-indent: 28px;">onmesure (int Widthmesurespec,int HEIGHTMESURESPEC) Method. Otherwise</span> , adding components in ViewGroup is not displayed. </span></span></span></p></p><p><p></p></p><p><p> <strong> <span style="font-family:fangsong_gb2312;font-size:14px;"><span style="text-indent:28px;" "> </span> </span>" /strong> </strong></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_1_3051849" name="code" class="java"><strong>package <strong><span style="font-family:FangSong_GB2312;font-size:14px;">Com.example.testrefreshview;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.view.viewgroup;import Android.widget.button;import android.widget.TextView; public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (new Myviewgroup (this));} public class Myviewgroup extends ViewGroup {public myviewgroup (context Context) {super (context); Button button1 = New button (context); Button1.settext ("button1"); Button button2 = New button (context); Button2.settext ("button2"); TextView TextView = new TextView (context); textview.settext ("TextView"); addview (button1); addview (button2); AddView ( textView);} @Overrideprotected void onlayout (boolean arg0, int arg1, int arg2, int arg3,int arg4) {}</span></strong> }}</strong></pre><p><p></p></p><p><p><span style="text-indent: 28px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">The layout of view (int left,int Top,int Right,int Bottom) method is responsible for placing the view in the specified position, so if we define the viewgroup::o in our own Nlayout iterates through each child view and assigns its position with View.layout (). Each child view also calls onlayout, which makes up a recursive call process<br></span></strong></span></p></p><p><p><span style="text-indent: 28px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">If you override the OnDraw method in Viewgroup. Need to call This.setwillnodraw (flase) in the constructor method; The overridden OnDraw (Canvas Cancas) method is not called at this Time. otherwise, the OnDraw (canvas Canvas) method is not Called.</span></strong></span></p></p><p><p><span style="text-indent: 28px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Change the code above to show it.</span></strong></span></p></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><span style="text-indent: 28px;"></span></span></strong></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_2_1907580" name="code" class="java"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Package Com.example.testrefreshview;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.view.view;import Android.view.viewgroup;import Android.widget.Button;import Android.widget.textview;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); setcontentview (new myviewgroup (this));} public class Myviewgroup extends ViewGroup {public myviewgroup (context Context) {super (context); Button button1 = New button (context); Button1.settext ("button1"); Button button2 = New button (context); Button2.settext ("button2"); TextView TextView = new TextView (context); textview.settext ("TextView"); addview (button1); addview (button2); AddView ( textView);} @Overrideprotected void onlayout (boolean arg0, int arg1, int arg2, int arg3,int arg4) {int childCount = Getchildcount (); in T left = 0;int top = 10;for (int i = 0; i < childCount; i++) {View child = Getchildat (i); child.layout (left, top,Left + +, Top + +); top + + 70;}}} </span></strong></pre><p><p></p></p><p><p><span><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Look at the code again:</span></strong></span></p></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><span></span></span></strong></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_3_2398731" name="code" class="java"><pre code_snippet_id="299151" snippet_file_name="blog_20140418_3_2398731" name="code" class="java"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) { int childCount = Getchildcount (); Set the size of the viewgroup int specsize_width = measurespec.getsize (widthmeasurespec); int specsize_height = measurespec.getsize (heightmeasurespec); Setmeasureddimension (specsize_width, specsize_height); for (int i = 0; i < childCount; i++) { View childview = Getchildat (i); Childview.measure (a);} } </span></strong></pre></pre><p><p></p></p><strong><strong><span style="font-family:FangSong_GB2312;font-size:14px;">By overriding the Onmeasure method, you can specify a size for viewgroup, and you can specify the size for each child view by Traversing. Adding the above code to your own definition ViewGroup assigns the displayed width height to each child view in Viewgroup.<br style="line-height: 25.200000762939453px;"></span></strong></strong><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Let's make the sub-view move up. Add code such as the Following:</span></strong></p></p><p><p></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_4_1777067" name="code" class="java"><pre code_snippet_id="299151" snippet_file_name="blog_20140418_4_1777067" name="code" class="java"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">public boolean ontouchevent (motionevent ev) {final float y = ev.gety (), switch (ev.getaction ()) {case motionevent.action_ Down:mlastmotiony = y;break; Case MotionEvent.ACTION_MOVE:int Detay = (int) (mlastmotiony-y); mlastmotiony = Y;scrollby (0, detay); Case MotionEvent.ACTION_UP:break;} Return true;}</span></strong></pre></pre><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;">In the above using a Scrollby method, open the official API can see the view class has such as the following two methods:</span></strong></p></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Both of these functions seem to be mobile views. So what's the difference between them? With this question we look down</span></strong></p></p><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">first, we have to make it clear that there <span style="color:#ff0000;">is no boundary on the Android view view, and that canvas has no boundaries</span> , just that we do some work on the canvas object by drawing a specific view. For example: translate (pan), clipRect (cut), etc., in order to achieve our requirements for the canvas object Drawing. We can refer to such borderless views as " <span style="color:#ff0000;">view coordinates</span> "-----which are not restricted by physical screens. Usually we understand that a layout file is only the display area of the view, beyond which the display area will not be displayed in the parent view of the area, accordingly, we can call such a bounded view of the <span style="color:#ff0000;">layout coordinates</span> ------the parent view to the child view assigned layout ( Layout) Size. also, The starting coordinate of a view on the screen is at the beginning of the view Coordinates. For example, as seen in The.</span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Because layout coordinates can only display a specific piece of content. So we only have the coordinate origin of the moving layout coordinates to show the coordinates of the view wherever they are located.</span></strong></p></p><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_5_7808115" name="code" class="html"><pre code_snippet_id="299151" snippet_file_name="blog_20140418_5_7808115" name="code" class="html"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical " android:background=" # 888888 "><textview android:id=" @+id/txt " android:layout_width=" 300dip " android:layout_height= "120dip" android:background= "#cccccc" android:text= "textview"/><button android:id= "@+id/ BTN " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" Button "/></linearlayout></span></strong></pre></pre><strong><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></strong><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">When I click the button to trigger an event such as the Following:</span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-weight: bold;"><span style="font-family:FangSong_GB2312;font-size:14px;">After triggering the Click event in the Code above, Textview.scrollto ( -200, -100) is run, and the meaning of the two parameters in ScrollTo is not the coordinate Position. Instead of the offset from the position of the <span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;">view coordinate</span></span> , if we want to move to the position of (200,100), the offset is (0,0)-(200,100) = (-200. -100).</span></span></p></p><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">If we replace the above code with scrollby, we will find that the TextView will move out of the visible interface after clicking multiple buttons, because Scrollby is offset from <span style="color:#ff0000;">our current coordinates</span> .</span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Let's look at how these two methods are implemented in the source code:</span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"></span></strong></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_6_3499993" name="code" class="java"><pre code_snippet_id="299151" snippet_file_name="blog_20140418_6_3499993" name="code" class="java"> /** * Set The scrolled position of your VIEW. This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * Invalidated. * @param x position to scroll to * @param y the y position to scroll to */public void ScrollTo (int x, i NT Y) { If (mscrollx! = x | | mscrolly! = y) { int oldx = mscrollx; int OldY = mscrolly; MSCROLLX = x; mscrolly = y; Invalidateparentcaches (); onscrollchanged (mscrollx, mscrolly, oldx, oldY); If (!awakenscrollbars ()) { invalidate (true);}}} </pre></pre>can see MSCROLLX = x; mscrolly = y;<p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"></span></strong></p></p><pre code_snippet_id="299151" snippet_file_name="blog_20140418_7_4306788" name="code" class="java"><pre code_snippet_id="299151" snippet_file_name="blog_20140418_7_4306788" name="code" class="java"> /** * Move The scrolled position of your VIEW. This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * Invalidated. * @param x The amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */public void Scrollby (int x, int y) { scrollTo (mscrollx + x, mscrolly + y); }</pre></pre>able to see Mscrollx + x, mscrolly + y;<p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;">Mscrollx and mscrolly are the current offsets relative to the view Coordinates.<br><br><br></span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><strong><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></strong><p><p><strong><span style="font-family:FangSong_GB2312;font-size:14px;"><br></span></strong></p></p><br> <p><p>Android self-defined component Family "1"-define View and ViewGroup</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.