Android虽然自带了很多控件,但有时仍然不能满足需求,这时就需要我们自己定义控件。本文自己定义了一个控件,单击该控件,可以使控件绘制不同的几何图形。

首先,新建一个Android工程,并新建一个类,继承自View。

package com.hzhi.customview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.view.View;public class CustomView extends View{// 图形类型int ss=0;// 构造函数public CustomView(Context context, AttributeSet attrs) {super(context, attrs);}// onDraw函数public void onDraw(Canvas c){super.onDraw(c);Paint p = new Paint();p.setColor(Color.GREEN);p.setStrokeWidth(10);switch (ss){// 画圆形case 0:c.drawCircle(200, 200, 100, p);break;// 画矩形case 1:c.drawRect(60, 90, 360, 300, p);break;// 画三角形case 2:Path path = new Path();path.moveTo(80, 100);path.lineTo(420, 250);path.lineTo(80, 350);path.close();c.drawPath(path, p);break;default:break;}}public void changeStyle(){ss++;if (ss>2){ss=0;}}}

该类就是我们自己定义的控件类,继承自View,单击时,将依次绘制出圆形、矩形和三角形。

然后,将该类加入主窗体的布局文件中,方法和加入Android自带控件的方法是一样的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >        <com.hzhi.customview.CustomView        android:id="@+id/cusView"android:layout_width="wrap_content"android:layout_height="wrap_content"    >    </com.hzhi.customview.CustomView></RelativeLayout>

最后,是主窗体的Java文件。

package com.hzhi.customview;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;public class MainActivity extends Activity {CustomView cv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);cv = (CustomView) findViewById(R.id.cusView);        cv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Message message = new Message();message.what = 1;myHandler.sendMessage(message);}});}    HandlermyHandler= new Handler() {//接收到消息后处理public void handleMessage(Message msg){switch (msg.what){case 1:cv.changeStyle();cv.invalidate();break;}super.handleMessage(msg);}};}

通过findViewById()函数获得自定义控件,定义控件的单击事件,和Android自带控件的使用方法是一致的。

运行结果:

更多相关文章

  1. android设置背景平铺
  2. Android(安卓)自定义dialog出入场动画
  3. android 焦点问题
  4. EditText
  5. Android(安卓)ListView的item点击无响应的解决方法
  6. Android(安卓)Layout XML属性
  7. [Android]后台Service 弹出自定义dialog
  8. android 自定义ButtonTab , ActivityGroup 动态加载 activity
  9. Android.GridView事件监听

随机推荐

  1. Android7.0中文文档(API)-- SimpleExpandab
  2. 对view设置透明度
  3. [置顶] Android Studio编译
  4. android listview的高度
  5. Android Fundamentals: Working With Con
  6. android 更新列表
  7. android:descendantFocusability 焦点 父
  8. Android中VISIBLE、INVISIBLE、GONE的区
  9. android widget ViewFlipper
  10. Android隐藏状态栏和标题栏