Android Basics Getting Started tutorial--2.2.4 framelayout (frame Layout)
Source: Internet
Author: User
<span id="Label3"></p>Android Basics Getting Started tutorial--2.2.4 framelayout (frame Layout)<p><p>Tags (space delimited): android basics Getting Started Tutorial</p></p>Introduction to this Section: <blockquote> <blockquote> <p>Framelayout (frame Layout) is one of the simplest layouts in six layouts, a layout that opens directly on the screen<br>A blank area, when we add controls to it, they are placed in the upper-left corner of the area by default, and this layout<br>There is no positioning method, so it does not apply many scenes; the size of the frame layout is determined by the largest child control in the control, if the control<br>size, you can only see the topmost component at the same time! the subsequent added controls will overwrite the previous one!<br>Although the control is placed in the Upper-left corner by default, We can also assign it to a different location through the layout_gravity property!<br>This section in addition to show you a simple example, but also to bring you two fun examples, interested can see!</p> </blockquote> </blockquote>1. Common Properties <blockquote> <blockquote> <p>The properties of Framelayout are rarely two, but before we say it, we introduce one thing:<br><strong>foreground image: always at the top of the frame layout, directly facing the User's image, is not overwritten by the picture</strong><br>Two properties:<br><strong>android:foreground:</strong> Setting the foreground image of a frame-changing layout container<br><strong>android:foregroundgravity:</strong> Setting the location of the foreground image display</p> </blockquote> </blockquote>2. Example Demo 1) The simplest example<p><p>Run:</p></p><p><p></p></p><p><p>The implementation code is as Follows:</p></p><pre class="prettyprint"><code class="language-XML hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">xmlns:tools </span> =<span class=" hljs-value ">" http://schemas.android.com/tools "</span> <span class="hljs-attribute">android:id </span> =<span class="hljs-value"> "@+id/framelayout1" </span> <span class=" Hljs-attribute ">android:layout_width </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:layout_height </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">tools:context </span> =<span class=" hljs-value ">". Mainactivity "</span> <span class=" hljs-attribute ">android:foreground </span> =<span class=" hljs-value ">" @drawable /logo "</span> <span class=" hljs-attribute ">android:foregroundgravity </span> =<span class=" hljs-value ">" right| Bottom "</span>; </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:layout_width</span>= "<span class="hljs-value">200dp"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" 200DP "</span><span class="hljs-attribute">android:background</span>=<span class="hljs-value">" #FF6143 "</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"150dp"</span><span class="hljs-attribute">android:layout_height</span>= <span class="hljs-value">"150dp"</span> <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"#7BFE00"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:layout_width</span>= "<span class="hljs-value">100dp"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" 100DP "</span><span class="hljs-attribute">android:background</span>=<span class="hljs-value">" #FFFF00 "</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">framelayout</span>></span></span> </code></pre><p><p>Code parsing:</p></p> <blockquote> <blockquote> <p>Very simple, three TextView set different size and background color, then overlay, then the bottom right corner is the foreground image, through<br>Android:foreground= "@drawable/logo" to set a picture of the foreground image<br>Android:foregroundgravity= "right|bottom" to set the position of the foreground image in the lower right corner</p> </blockquote> </blockquote>2) the sister who moves with the finger<p><p>As follows:</p></p><p><p></p></p><p><p>Implement Process Resolution:</p></p> <blockquote> <blockquote> <ul> <li>Step 1: Set the Main.xml layout to a blank framelayout and set a picture background for it</li> <li>Step 2: Create a new Meziview custom component class that inherits the view class, initializing the initial coordinates of the view in the constructor method</li> <li>Step 3: Override the OnDraw () method to instantiate an empty brush class paint</li> <li>Step 4: Call Bitmapfactory.decoderesource () to generate a bitmap object</li> <li>Step 5: Call Canvas.drawbitmap () to draw the Sister's bitmap object</li> <li>Step 6: Determine if the image is recycled, otherwise force the image to be recycled</li> <li>Step 7: Get the Frame layout object in the main Java code and instantiate a Meziview class</li> <li>Step 8: Add a touch event listener to the Mezi object that will be instantiated, override the Ontouch method, change the x, y coordinates of the mezi, call invalidate () redraw method</li> <li>Step 9: Add the Mezi object to the frame layout</li> </ul> </blockquote> </blockquote><p><p>The implementation code is as Follows:<br>Layout code:<strong>main_activity.xml</strong></p></p><pre class="prettyprint"><code class="language-XML hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/mylayout"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">tools:context</span>=<span class="hljs-value">". Mainactivity "</span><span class="hljs-attribute">android:background</span>=<span class="hljs-value">" @drawable/back "</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">framelayout</span>></span></span> </code></pre><p><p>Custom <strong>Meziview.java</strong></p></p><pre class="prettyprint"><code class="language-XML hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.jay.example.framelayoutdemo2;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.content.Context;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.graphics.Bitmap;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.graphics.BitmapFactory;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.graphics.Canvas;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.graphics.Paint;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">meziview</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span> {</span></span> <span class="hljs-comment"><span class="hljs-comment">//define related variables, followed by the x, y coordinates of the sister's display position</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>bitmapx;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>bitmapy;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Meziview</span></span>(context Context) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context);<span class="hljs-comment"><span class="hljs-comment">//set the sister's starting coordinates</span></span>BITMAPX =<span class="hljs-number"><span class="hljs-number">0</span></span>; Bitmapy =<span class="hljs-number"><span class="hljs-number"></span> $</span>; }<span class="hljs-comment"><span class="hljs-comment">//override The OnDraw () method of the view class</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDraw</span></span>(canvas Canvas) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDraw (canvas);<span class="hljs-comment"><span class="hljs-comment">//create, and instantiate the Paint object</span></span>Paint paint =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Paint ();<span class="hljs-comment"><span class="hljs-comment">//create a Bitmap object</span> from a picture</span>Bitmap Bitmap = Bitmapfactory.decoderesource (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. getresources (), r.drawable.s_jump);<span class="hljs-comment"><span class="hljs-comment">//draw Sister</span></span>Canvas.drawbitmap (bitmap, bitmapx, bitmapy,paint);<span class="hljs-comment"><span class="hljs-comment">//determine If the picture is recycled, and the wood is recycled to forcibly retract the picture</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(bitmap.isrecycled ()) {bitmap.recycle (); } } }</code></pre><p><p><strong>Mainactivity.java:</strong></p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.jay.example.framelayoutdemo2;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Bundle;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.MotionEvent;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View.OnTouchListener;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.widget.FrameLayout;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.app.Activity;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Framelayout frame = (framelayout) Findviewbyid (r.id.mylayout);<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Meziview Mezi =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Meziview (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>);<span class="hljs-comment"><span class="hljs-comment">//add A touch event listener to our cute sister</span></span>Mezi.setontouchlistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Ontouchlistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">OnTouch</span></span>(view view, motionevent Event) {<span class="hljs-comment"><span class="hljs-comment">//set the location shown by Sister</span></span>MEZI.BITMAPX = Event.getx ()-<span class="hljs-number"><span class="hljs-number"></span> max</span>; Mezi.bitmapy = Event.gety ()-<span class="hljs-number"><span class="hljs-number"></span> max</span>;<span class="hljs-comment"><span class="hljs-comment">//call Redraw Method</span></span>Mezi.invalidate ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; } }); Frame.addview (mezi); } }</code></pre><p><p><strong>Code explanation:</strong><br>See step, very simple, is to customize a view class, rewrite the redraw method, and then add a touch time to the activity<br>Override the Ontouch method in touch time to get the click focus, Plus 150, Otherwise the coordinates are custom view<br>Upper left corner, then call invalidate () redraw method, and finally added to the frame layout!<br>Code download: HTTP://PAN.BAIDU.COM/S/1PJJFKGZ</p></p>3) the running of the cute sister<p><p><strong>As follows:</strong></p></p><p><p></p></p><p><p><strong>Implementation Process:</strong></p></p> <blockquote> <blockquote> <ul> <li>Step 1: Define an empty framelayout layout that sets the position of the foreground image to a central location</li> <li>Step 2: Get the framelayout layout in activity, create a new handler object, override the Handlermessage () method, call image-update method</li> <li>Step 3: Customize a Move () method to dynamically set the bitmap displayed by the foreground picture via switch</li> <li>Step 4: Create a new timer object in the OnCreate () method timer, override the run method, and send empty information to handler every 170 milliseconds</li> </ul> </blockquote> </blockquote><p><p><strong>The implementation code is as Follows:</strong></p></p><p><p><strong>Layout File: Main_activity.xml:</strong></p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-XML hljs xml"><span class="hljs-tag"><<span class="hljs-title">FrameLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span> <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/myframe"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:foregroundGravity</span>=<span class="hljs-value">"center"</span>></span> <span class="hljs-tag"></<span class="hljs-title">FrameLayout</span>></span> </code></pre></pre><p><p><strong>Mainactivity.java:</strong></p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.jay.example.framelayoutdemo3;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.util.Timer;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.util.TimerTask;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Bundle;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Handler;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Message;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View.OnClickListener;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.widget.FrameLayout;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.app.Activity;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.graphics.drawable.Drawable;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-comment"><span class="hljs-comment">//initialize variable, Frame Layout</span></span>Framelayout frame =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-comment"><span class="hljs-comment">//customize A handler class object that is used to periodically update the UI interface</span></span>Handler Handler =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Handler () {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>i =<span class="hljs-number"><span class="hljs-number">0</span></span>;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Handlemessage</span></span>(Message Msg) {<span class="hljs-comment"><span class="hljs-comment">//determine If the information is issued for this application</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(msg.what = =<span class="hljs-number"><span class="hljs-number">0x123</span></span>) {i++; Move (i%<span class="hljs-number"><span class="hljs-number">8</span></span>); }<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Handlemessage (msg); } };<span class="hljs-comment"><span class="hljs-comment">//define How to switch pictures when walking</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span>Move<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>I) {drawable a = getresources (). getdrawable (r.drawable.s_1); drawable B = getresources (). getdrawable (r.drawable.s_2); drawable C = getresources (). getdrawable (r.drawable.s_3); drawable d = getresources (). getdrawable (r.drawable.s_4); drawable e = getresources (). getdrawable (r.drawable.s_5); drawable f = getresources (). getdrawable (r.drawable.s_6); drawable g = getresources (). getdrawable (r.drawable.s_7); drawable h = getresources (). getdrawable (r.drawable.s_8);<span class="hljs-comment"><span class="hljs-comment">//setforeground to set the foreground image</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(i) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">0</span></span>: Frame.setforeground (a);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">1</span></span>: Frame.setforeground (b);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">2</span></span>: Frame.setforeground (c);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">3</span></span>: Frame.setforeground (d);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">4</span></span>: Frame.setforeground (e);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">5</span></span>: Frame.setforeground (f);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">6</span></span>: Frame.setforeground (g);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">7</span></span>: Frame.setforeground (h);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>; } }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); frame = (framelayout) Findviewbyid (r.id.myframe);<span class="hljs-comment"><span class="hljs-comment">//define a Timer object and send information to handler periodically</span></span> <span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Timer (). Schedule (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>TimerTask () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Run</span></span>() {<span class="hljs-comment"><span class="hljs-comment">//send an empty message to notify the system to change the foreground picture</span></span>Handler.sendemptymessage (<span class="hljs-number"><span class="hljs-number">0x123</span></span>); } },<span class="hljs-number"><span class="hljs-number">0</span></span>,<span class="hljs-number"><span class="hljs-number"></span> the</span>); } }</code></pre><p><p><strong>Code parsing:</strong><br>The code is also simple, which is to define a handler object to refresh the foreground image of the frame layout, define a timer timer<br>Timing information is sent every 170 milliseconds, i++;move (i%8); This is because we use 8 images as animated footage!<br>Code download: Http://pan.baidu.com/s/1c0vL8PE</p></p>This section summarizes:<p><p>This section describes the next framelayout (frame layout), Mastering the use of foreground and foregroundgravity properties!<br>The frame layout is a little bit more than the previous table layout! I would like to try to write a little example.</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Basics Getting Started tutorial--2.2.4 framelayout (frame Layout)</p></p></span>
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