Android自定义控件实现及其布局

Android自定义控件实现及其布局

Android自定义控件一般要继承View类,因此控件的实现及其相应的布局需要完成:

1.     继承View类,并实现参数为(Context context,AttributeSet attrs)的构造函数

2.     在布局文件xml中设置属性的时候,应以(<包名.类名  />)的格式进行。

3.     声明一个自定义控件的变量,用findViewById将其与布局文件关联起来。

举例:以下是自定义的MyView控件,用于显示2张图片;

源代码:http://download.csdn.net/detail/nuptboyzhb/4509472

 

MyView.java代码

[java]  view plain copy
  1. package com.example.njupt.zhb.myselfview;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.RectF;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9. public class MyView extends View {      
  10.     public Bitmap bitmap1=null;  
  11.     public Bitmap bitmap2=null;  
  12.     public int myInterval=10;  
  13.     /*一定要重写这个构造方法*/  
  14.     public MyView(Context context,AttributeSet attrs) {  
  15.         super(context,attrs);  
  16.     }  
  17.     /*重写onDraw()*/     
  18.     @Override     
  19.     protected void onDraw(Canvas canvas)      
  20.     {         
  21.          super.onDraw(canvas);  
  22.          int myViewWidth=getWidth()-myInterval*2;  
  23.          int myViewHeight=getHeight()-myInterval;  
  24.          int lessLen=myViewWidth/2<myViewHeight?myViewWidth/2:myViewHeight;  
  25.          /*画位图1*/  
  26.          if (bitmap1!=null) {  
  27.              RectF dst1=new RectF(0,myInterval,lessLen,lessLen+myInterval);  
  28.              canvas.drawBitmap(bitmap1, null, dst1, null);  
  29.         }  
  30.          /*画位图2*/  
  31.          if(bitmap2!=null){  
  32.              RectF dst2=new RectF(lessLen+myInterval*2,myInterval,lessLen*2+myInterval*2,lessLen+myInterval);  
  33.              canvas.drawBitmap(bitmap2, null, dst2, null);  
  34.          }  
  35.     }    
  36.       
  37. }   

MainActivity.java代码

[java]  view plain copy
  1. package com.example.njupt.zhb.myselfview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.view.Menu;  
  8.   
  9. public class MainActivity extends Activity {  
  10.     MyView myView;  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         setTitle("自定义MyView");  
  16.         Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.lenna);  
  17.         myView=(MyView)findViewById(R.id.myview);  
  18.         myView.bitmap1=bitmap;  
  19.         myView.bitmap2=bitmap;  
  20.     }  
  21. }  


 

activity_main.xml代码

<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代码

[html]  view plain copy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.njupt.zhb.myselfview"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="4"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name=".MainActivity"  
  16.             android:screenOrientation="landscape"  
  17.             android:label="@string/title_activity_main" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25.   
  26. </manifest>  


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值