Android自定义控件基本原理详解(一)

所属分类: 软件编程 / Android 阅读数: 989
收藏 0 赞 0 分享

前言:

在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理。

自定义控件要求: 

     1. 应当遵守Android标准的规范(命名,可配置,事件处理等)。
     2. 在XML布局中科配置控件的属性。
     3. 对交互应当有合适的反馈,比如按下,点击等。
     4. 具有兼容性, Android版本很多,应该具有广泛的适用性。

自定义控件学习步骤: 

  1 .View的工作原理
  2 .编写View类
  3.为View类增加属性
  4 .绘制屏幕
  5. 响应用户消息
  6 .自定义回调函数 

自定义控件两种方式:
1. 继承ViewGroup 
例如:ViewGroup、LinearLayout、FrameLayout、RelativeLayout等。
2. 继承View
例如:View、TextView、ImageView、Button等。 

自定义控件基本绘制原理:
 View的绘制基本上由measure()、layout()、draw()这个三个函数完成 
1.)测量-Measure过程是计算视图大小,View measure过程相关方法主要有三个: 
public final void measure(int widthMeasureSpec, int heightMeasureSpec) 
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
measure调用onMeasure,onMeasure测量宽度、高度然后调用setMeasureDimension保存测量结果,measure,setMeasureDimension是final类型,view的子类不需要重写,onMeasure在view的子类中重写。 

关于MeasureSpec: 
(1)UPSPECIFIED :父容器对于子容器没有任何限制,子容器想要多大就多大. 
(2) EXACTLY父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间. 
(3) AT_MOST子容器可以是声明大小内的任意大小.

 2.)布局-Layout过程用于设置视图在屏幕中显示的位置,View layout过程相关方法主要要三个: 
public void layout(int l, int t, int r, int b)
protected boolean setFrame(int left, int top, int right, int bottom)
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
layout通过调用setFrame(l,t,r,b),l,t,r,b即子视图在父视图中的具体位置,onLayout一般只会在自定义ViewGroup中才会使用 

3.)绘制-draw过程主要用于利用前两步得到的参数,将视图显示在屏幕上,到这里也就完成了整个的视图绘制工作。 
public void draw(Canvas canvas)
protected void onDraw(Canvas canvas)
通过调用draw函数进行视图绘制,在View类中onDraw函数是个空函数,最终的绘制需求需要在自定义的onDraw函数中进行实现,比如ImageView完成图片的绘制,如果自定义ViewGroup这个函数则不需要重载。 

自定义控件示例:
这里先介绍继承View的方式为例,其实ViewGroup最终的继承的也是View。这里 模拟一个需求场景,需要一个圆形显示百分比。 

public class PercentView extends View {
 private final static String TAG = PercentView.class.getSimpleName();
 private Paint mPaint;

 public PercentView(Context context) {
 super(context);
 init();
 }

 public PercentView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init();
 }

 public PercentView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init();
 }

 private void init(){
 mPaint = new Paint();
 mPaint.setAntiAlias(true);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 Log.e(TAG, "onMeasure--widthMode-->" + widthMode);
 switch (widthMode) {
 case MeasureSpec.EXACTLY:

 break;
 case MeasureSpec.AT_MOST:

 break;
 case MeasureSpec.UNSPECIFIED:

 break;
 }
 Log.e(TAG, "onMeasure--widthSize-->" + widthSize);
 Log.e(TAG, "onMeasure--heightMode-->" + heightMode);
 Log.e(TAG, "onMeasure--heightSize-->" + heightSize);
 }

 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
 super.onLayout(changed, left, top, right, bottom);
 Log.e(TAG, "onLayout");
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 mPaint.setColor(Color.GRAY);
 // FILL填充, STROKE描边,FILL_AND_STROKE填充和描边
 mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
 int with = getWidth();
 int height = getHeight();
 Log.e(TAG, "onDraw---->" + with + "*" + height);
 float radius = with / 4;
 canvas.drawCircle(with / 2, with / 2, radius, mPaint);
 mPaint.setColor(Color.BLUE);
 RectF oval = new RectF(with / 2 - radius, with / 2 - radius, with / 2
 + radius, with / 2 + radius); //用于定义的圆弧的形状和大小的界限
 canvas.drawArc(oval, 270, 120, true, mPaint); //根据进度画圆弧
 }
} 

在布局中如何使用 

<LinearLayout 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:orientation="vertical">

 <com.whoislcj.views.PercentView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="10dp" />

</LinearLayout> 

显示效果:

 

如果布局文件改成 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

 <com.whoislcj.views.PercentView
 android:layout_width="150dp"
 android:layout_height="150dp"
 android:layout_margin="10dp" />

</LinearLayout> 

显示效果变成

 

总结:
本篇主要介绍Android自定义控件的基本绘制原理,会在下一篇中介绍如何自定义属性,点击查看

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多