FrameLayout (frame layout)
Preface
As one of the simplest la s in the six la s of android, this layout directly opens up a blank area on the screen,
When we add components to it, all the components will be placed in this area.Upper left corner;
Frame layout sizeSub-controlThe largest sub-control determines,
If both components areAs bigAt the same time.You can only see the top component.Now!
Of course, we can alsoAdd the layout_gravity attributeToHow to Develop Components
Frame layout is widely used in Game Development. I will show you two interesting instances later.
Foreground image:
Always at the top of the frame layout, directly facing the user's image, is not overwritten Image
Common attributes:
Android: foreground:Set the foreground image of the frame layout container
Android: foregroundGravity:Set the foreground image display position
Application instance:
① Simplest example:
The attribute is very simple. Next we will demonstrate the basic usage of FrameLayout.
It's really easy. Most examples on the Internet use this =,
:
The Code is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> <frameLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/FrameLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "android: foreground =" @ drawable/logo "android: foregroundGravity =" right | bottom "> </FrameLayout>
Code explanation:
We have set three image blocks, which are only three textviews with different background colors.
TextView appears in the upper left corner. To avoid overwriting, the size decreases sequentially.
Set the foreground image in the outer FrameLayout, and set it to the lower right corner.
② Cute girl who follows her fingers
:
Implementation process:
Step 1:First, set the main. xml layout to a blank FrameLayout, and set an image background for it.
Step 2:Create a MeziView custom component class that inherits the View class and initialize the initial coordinates of the view in the constructor.
Step 3:Override the onDraw () method to instantiate an empty Paint class Paint
Step 4:Call BitmapFactory. decodeResource () to generate a bitmap object.
Step 5:Call canvas. drawBitmap () to draw the girl's bitmap object.
Step 6:Determines whether the image is recycled. Otherwise, the image is forcibly recycled.
Step 7:Get the frame layout object in the main Java code and instantiate a MeziView class
Step 8:Add a listener for the touch event to the instantiated mezi object, override the onTouch method, change the X and Y coordinates of mezi, and call the invalidate () redraw method.
Step 9:Add the mezi object to the frame Layout
Code details:
Layout code:
<frameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/back" ></frameLayout>
Custom MeziView class
Package com. jay. example. framelayoutdemo2; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. paint; import android. view. view; public class MeziView extends View {// defines the relevant variables, which are the X and Y coordinates of the sister's position in sequence: public float bitmapX; public float bitmapY; public MeziView (Context context) {super (context); // set the sister's starting coordinate bitmapX = 0; bitmapY = 200;} // rewrite the onDraw () method of the View class @ Overrideprotected void onDraw (Canvas canvas Canvas) {super. onDraw (canvas); // create and instantiate the Paint object. The Paint paint = new Paint (); // Bitmap object bitmap = BitmapFactory is generated based on the image. decodeResource (this. getResources (), R. drawable. s_jump); // draw the canvas of the cute girl. drawBitmap (bitmap, bitmapX, bitmapY, paint); // determines whether the image is recycled. if the wood is recycled, the image is forcibly withdrawn if (bitmap. isRecycled () {bitmap. recycle ();}}}
Main Activity class:
Package com. jay. example. framelayoutdemo2; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. frameLayout; import android. app. activity; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); FrameLayout frame = (FrameLayout) findViewById (R. id. mylayout); final MeziView mezi = new MeziView (MainActivity. this); // Add the touch event listener mezi for our cute girl. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View view, MotionEvent event) {// set the mezi position displayed by the sister. bitmapX = event. getX (); mezi. bitmapY = event. getY (); // call the re-painting method mezi. invalidate (); return true ;}}); frame. addView (mezi );}}
Code explanation:
The steps are detailed and the code is not difficult. Let's take a look.
It is to customize a View class, then rewrite the repainting method, and then add a touch event for it in the main Activity.
In the touch event, rewrite the onTouch method to obtain the click coordinate and call the invalidate () re-painting method;
Added to the frame layout.
Code link:
Http://pan.baidu.com/s/1pJJfKgz
③ Running cute girl
:
Implementation process:
Step 1:Defines an empty FrameLayout layout and sets the foreground image position to the central position.
Step 2:Get the FrameLayout layout in the Activity, create a new Handler object, override the handlerMessage () method, and call the image update method.
Step 3:Customizes a move () method and dynamically sets the bitmap of the foreground image through the switch.
Step 4:Create a Timer object Timer In the onCreate () method, override the run method, and send an empty message to handler every 170 milliseconds.
The Code is as follows:
Xml layout code:
<frameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/myframe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:foregroundGravity="center"></frameLayout>
MainActivity. java:
Package com. jay. example. framelayoutdemo3; import java. util. timer; import java. util. timerTask; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. frameLayout; import android. app. activity; import android. graphics. drawable. drawable; public class MainActivity extends Activity {// initialization variable, frame layout FrameLayout frame = null; // customize a handler Class Object Handler handler = new Handler () {int I = 0; @ Overridepublic void handleMessage (Message msg) used to regularly update the UI Interface) {// determine whether the information is the if (msg. what = 0x123) {I ++; move (I % 8);} super. handleMessage (msg) ;}}; // defines the way to switch the image when walking void move (int 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); // use setForeground to set the foreground image switch (I) {case 0: frame. setForeground (a); break; case 1: frame. setForeground (B); break; case 2: frame. setForeground (c); break; case 3: frame. setForeground (d); break; case 4: frame. setForeground (e); break; case 5: frame. setForeground (f); break; case 6: frame. setForeground (g); break; case 7: frame. setForeground (h); break ;}@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); frame = (FrameLayout) findViewById (R. id. myframe); // defines a Timer object and regularly sends information to handlernew Timer (). schedule (new TimerTask () {@ Overridepublic void run () {// send an empty message to notify the system to change the foreground image handler. sendEmptyMessage (0x123) ;}, 0,170 );}}
Code explanation:
The code is not complicated, that is, defining a handler object to refresh the foreground image of the frame layout.
Use Timer pair to send information regularly
I ++; move (I % 8); // This is because eight images are used as the animation material.
Code link:
Http://pan.baidu.com/s/1c0vL8PE
Summary:
The above is the explanation of FrameLayout. FrameLayout is quite interesting.
Frame Animation is a common example.
If any omission exists, you can point it out to me.
O (∩ _ ∩) O thank you!