Android custom control implementation and Layout

Source: Internet
Author: User

Android custom control implementation and Layout

Android custom controls generally inherit the View class. Therefore, the implementation and corresponding layout of controls must be completed:

1. inherit the View class and implement the constructor whose parameters are (Context context, AttributeSet attrs ).

2. When setting attributes in the xml layout file, they should be in the format of (<package name. Class Name/>.

3. Declare the variables of a custom control and associate it with the layout file using findViewById.

For example, the custom MyView control is used to display two images;

MyView. java code

[Java]
Package com. example. njupt. zhb. myselfview;
 
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. RectF;
Import android. util. AttributeSet;
Import android. view. View;
Public class MyView extends View {
Public Bitmap bitmap1 = null;
Public Bitmap bitmap2 = null;
Public int myInterval = 10;
/* Be sure to override this constructor */
Public MyView (Context context, AttributeSet attrs ){
Super (context, attrs );
}
/* Override onDraw ()*/
@ Override
Protected void onDraw (Canvas canvas)
{
Super. onDraw (canvas );
Int myViewWidth = getWidth ()-myInterval * 2;
Int myViewHeight = getHeight ()-myInterval;
Int lessLen = myViewWidth/2 <myViewHeight? MyViewWidth/2: myViewHeight;
/* Bitmap 1 */
If (bitmap1! = Null ){
RectF dst1 = new RectF (0, myInterval, lessLen, lessLen + myInterval );
Canvas. drawBitmap (bitmap1, null, dst1, null );
}
/* Bitmap 2 */
If (bitmap2! = Null ){
RectF dst2 = new RectF (lessLen + myInterval * 2, myInterval, lessLen * 2 + myInterval * 2, lessLen + myInterval );
Canvas. drawBitmap (bitmap2, null, dst2, null );
}
}

}
MainActivity. java code

[Java]
Package com. example. njupt. zhb. myselfview;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. view. Menu;
 
Public class MainActivity extends Activity {
MyView myView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
SetTitle ("Custom MyView ");
Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. lenna );
MyView = (MyView) findViewById (R. id. myview );
MyView. bitmap1 = bitmap;
MyView. bitmap2 = bitmap;
}
}

 

Activity_main.xml code

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Com. example. njupt. zhb. myselfview. MyView
Android: id = "@ + id/myview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</RelativeLayout>

AndroidManifest. xml Code

[Html]
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. njupt. zhb. myselfview"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk
Android: minSdkVersion = "4"
Android: targetSdkVersion = "15"/>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = ". MainActivity"
Android: screenOrientation = "landscape"
Android: label = "@ string/title_activity_main">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>

 

Related Article

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.