Android 自定义控件3
来源:互联网 发布:hgo数据处理软件 编辑:程序博客网 时间:2024/06/16 04:03
创建新的控件:
作为一个有创意的开发者,你经常会遇到安卓原生控件无法满足你的需求。
为了优化你的界面和工作流程,安卓允许你去继承已经存在的控件或者实现你自己的控件。
那么最好的方式去创建一个新的控件是什么? 这主要取决你想要完成什么。
1.有些基本功能原生控件都能提供,所以这个时候你只需要继承并对控件进行扩展。通过重写它的事件,onDraw,但是始终都保持都父类方法的调用。
2.组合控件 就是通过合并几个控件的功能来生成一个控件。
3.完完整整创建一个新的控件。
1.修改存在的控件
例子:
public
class
MyTextView
extends
TextView {
public
MyTextView(Context context, AttributeSet ats,
int
defStyle) {
super
(context, ats, defStyle);
}
public
MyTextView(Context context) {
super
(context);
}
public
MyTextView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
@Override
public
void
onDraw(Canvas canvas) {
// 在画布上画文本之下的内容
// 保证默认的文本渲染
super
.onDraw(canvas);
// 在画布上画文本之上的内容
}
@Override
public
boolean
onKeyDown(
int
keyCode, KeyEvent keyEvent) {
// 写自己的控制
// 保持父类默认的控制
return
super
.onKeyDown(keyCode, keyEvent);
}
}
2.组合控件
1.最简单的方式,是定义了XML布局文件,然后用include实现重用。(。。。这也算啊。。。)
2.去合并一个控件 通常你自定义的控件需要继承一个ViewGroup(通常就是Layout),就像:
123456789public
class
MyCompoundView
extends
LinearLayout {
public
MyCompoundView(Context context) {
super
(context);
}
public
MyCompoundView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
}
就像activity,比较好的设计一个混合的控件UI布局是使用一个外部的layout资源。
这里我们模拟定义一个:
1234567891011121314151617<?
xml
version=”1.0” encoding=”utf-8”?>
<
LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<
EditText
android:id=”@+id/editText”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
/>
<
Button
android:id=”@+id/clearButton”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Clear”
/>
</
LinearLayout
>
然后在构造函数初始化的时候:
public
class
ClearableEditText
extends
LinearLayout {
EditText editText;
Button clearButton;
public
ClearableEditText(Context context) {
super
(context);
// Inflate the view from the layout resource.
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
/*这句很关键,解析反射资源文件,然后将布局附加到当前的控件,也就是this*/
li.inflate(R.layout.clearable_edit_text,
this
,
true
);
/* 因为反射成功后的布局已经附加上了,那么直接可以findViewById*/
editText = (EditText) findViewById(R.id.editText);
clearButton = (Button) findViewById(R.id.clearButton);
// 下面自定义的方法就是为控件注册监听,不解释了
hookupButton();
}
}
使用:在activity_main.xml
<com.example.customview.MyCompoundView android:layout_width="match_parent" android:layout_height="wrap_content" />
3. 完完全全自定义控件
通常是继承View或者SurfaceView ,View类提供一个Canvas(画布)和一系列的画的方法,还有Paint(画笔)。使用它们去创建一个自定义的UI。你可以重写事件,包括屏幕接触或者按键按下等等,用来提供与用户交互。
1.如果你不需要快速重画和3D图像的效果,那么让View作为父类提供一个轻量级的解决方案。
2.如若不然,就需要使用SurfaceView作为父类,这样你就可以提供一个后台线程去画和使用OPENGL去实现你的图像。这个就相对重量级了,如果你的视图需要经常更新,然后由需要显示比较复杂的图像信息(尤其是在游戏和3D可视化),SurfaceView将是更好的选择。
在这里我们讨论前者,后者后期再讨论。
一般你需要重写2个方法:
1.onMeasure
什么是onMeasure?
下面转载一段文章:
View在屏幕上显示出来要先经过measure(计算)和layout(布局).
1、什么时候调用onMeasure方法?
当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec.
这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.
widthMeasureSpec和heightMeasureSpec这2个参数都是整形是出于效率的考虑,所以经常要做的就是对其解码=>
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- 依据specMode的值,(MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST)
- 如果是AT_MOST,specSize 代表的是最大可获得的空间;
如果是EXACTLY,specSize 代表的是精确的尺寸;
如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
2、那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢?
经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。
而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式表示你没有指定大小。- View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。
有个观念需要纠正的是,fill_parent应该是子view会占据剩下容器的空间,而不会覆盖前面已布局好的其他view空间,当然后面布局子 view就没有空间给分配了,所以fill_parent属性对布局顺序很重要。以前所想的是把所有容器的空间都占满了,难怪google在2.2版本里 把fill_parent的名字改为match_parent.
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
12345678910111213141516171819202122232425262728293031@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
int
measuredHeight = measureHeight(heightMeasureSpec);
int
measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
// 记住这句可不能省。
}
private
int
measureHeight(
int
measureSpec) {
int
specMode = MeasureSpec.getMode(measureSpec);
int
specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int
result =
500
;
if
(specMode == MeasureSpec.AT_MOST) {
// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.
result = specSize;
}
else
if
(specMode == MeasureSpec.EXACTLY) {
// If your control can fit within these bounds return that value.
result = specSize;
}
return
result;
}
private
int
measureWidth(
int
measureSpec) {
// 代码基本类似measureHeight
}
总结:
通过
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);这2个值,然后计算自己想要占有的宽和高。
2.onDraw
这个不解释了。(后期会细说canvas和paint)
- Android 自定义控件3
- android 自定义控件3
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- Android自定义控件 -- 自定义View
- Android自定义控件 -- 自定义ViewGroup
- [LeetCode]044-Wildcard Matching
- jquery easyui datagrid 加载成功,选中某一行
- scala定时任务调度器
- 让IE6/IE7/IE8浏览器支持CSS3属性
- Junit断言的学习
- Android 自定义控件3
- 鼠标双击桌面上的快捷方式变成属性
- Launcher3 开机后应用数据的加载流程分析
- 设计模式系列3---搞转换的适配器模型
- js中的"=="和equals()以及is()三者的区别
- jquery中怎样把字符串解析成json【前端$.parseJSON(字符串)返回json对象详细看文章内容】
- hdu2037今年暑假不AC
- Akka并发编程——第二节:Actor模型(一)
- 属性动画实现卫星菜单