In Android, all UI interfaces are composed of the view class and the ViewGroup class and its subclasses. Where the view class is the base class for all UI components, and the ViewGroup class is the container that holds the UI components.
It is also a subclass of the view class.
In actual development, the view class is not enough to meet all the requirements of the program. At this point, you can develop your own components by inheriting the view class.
Steps to develop a custom component:
1. Create a view class that inherits the Android.view.View class, and override the constructor method.
2. Rewrite the appropriate method as needed.
3. Create and instantiate a custom view class and add it to the layout manager.
Now learn to use custom classes
1. First create a Java file to draw the picture
1 Package MyView;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import Android.content.Context;6 import Android.graphics.Bitmap;7 import android.graphics.BitmapFactory;8 import Android.graphics.Canvas;9 import Android.graphics.Paint;Ten import Android.view.View; One A Public classMyView extends view{ - - Public floatbitmipx; the Public floatbitmipy; - - PublicMyView (Context context) { - super (context); +bitmipx=550; -bitmipy=550; + //TODO auto-generated Constructor stub A } at - @Override - protected voidOnDraw (canvas canvas) { - //TODO auto-generated Method Stub - - Super.ondraw (canvas); in //Create and instantiate a Paint object -Paint paint =NewPaint (); to //generate bitmap objects from a picture +Bitmap Bitmap = Bitmapfactory.decoderesource ( This. Getresources (), R.drawable.image_equip_baibao_baicaodan); -Canvas.drawbitmap (bitmap, bitmipx,bitmipy, paint);//Drawing Pictures the if(Bitmap.isrecycled ())//determine if a picture can be recycled * { $Bitmap.recycle ();//Force Recycle PicturesPanax Notoginseng } - the } + A}
2. Instantiate the picture in the main activity and add the touch event
1 Package MyView;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import android.app.Activity;6 import Android.os.Bundle;7 import android.provider.ContactsContract.CommonDataKinds.Event;8 import android.view.MotionEvent;9 import Android.view.View;Ten import Android.view.View.OnTouchListener; One import android.widget.FrameLayout; A - Public classShowmyview extends activity{ - @Override the protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); - Setcontentview (r.layout.showmyview); +Framelayout framelayout =(framelayout) Findviewbyid (R.id.showview); -Final MyView MyView =NewMyView (Showmyview. This);//Create and instantiate the MyView class + A //Adding Touch Events atMyview.setontouchlistener (NewOntouchlistener () { - - @Override - PublicBoolean OnTouch (View V, motioneventEvent) { - //TODO auto-generated Method Stub -myview.bitmipx=Event. GetX (); inmyview.bitmipy=Event. GetY (); -Myview.invalidate ();//Redraw a custom component to return true; + } - }); the Framelayout.addview (myview); * $ }Panax Notoginseng}
The corresponding layout file
1<?xml version="1.0"encoding="Utf-8"?>2<framelayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5Android:id="@+id/showview"//Don't forget this line6>7</FrameLayout>
:
Android Development _ Talking about custom components