ALFA
DeV工作室原创文章如转载,请注明:转载自博客原文地址:http://blog.csdn.net/kongre/article/details/6746146
创建自定义的View能够从根本上塑造你的应用程序的外观,你可以通过创建自定义视图的方式去满足用户独特的需求。你可以继承View类或者是SurfaceView类。View类提供了一个Canvas对象,你可以去使用这个对象的很多画图的方法以及Paint对象去绘制你的自定义视图。当然你可以通过覆盖screen
touch, key press等的UI事件,对这些事件进行响应,与用户进行交互。SurfaceView类提供了一个Surface对象用来支持使用后台独立线程来对用户自定义视图进行绘制的这么一个核心的对象,并且可以使用openGL技术进行3D绘图。主要用于用户自定义视图更新频繁或者是显示一些复杂的3D效果的场景,在游戏开发当中经常使用。本篇博客我们介绍继承View类的自定义视图,在后面的博客当中,我们将为大家讲解继承SurfaceView的自定义View的构建方法。
步骤一、创建一个新的可视界面
View类默认展现100像素*100像素的区域,如果需要改变这个视图的大小,你需要去重写onMeasure()和onDraw()方法。onDraw()方法用来在Canvas上绘图,onMeasure()方法用来计算在给定的边界情况下,我们自定义的视图的高度和宽度各是多少。Skeleton Code:
public class MyView extends View {
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
//Must make this call to setMeasuredDimension
//or you will cause a runtime exception when the control is laid out
setMeasuredDimension(measuredWidth, measuredHeight);
}
private int measureWidth(int widthMeasureSpec) {
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
//calculate the width
return specSize;
}
private int measureHeight(int heightMeasureSpec) {
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);
//calculate the height
return specSize;
}
@Override
protected void onDraw(Canvas canvas) {
//draw your user interface
}
}
注意在我们重写的onMeasure()方法当中,必须调用setMeasuredDimension()方法。不然的话,有的自定义视图在使用的时候将会抛出运行时异常。
步骤二、绘制你的自定义视图
onDraw()方法是奇迹发生的地方。Android提供了一系列的工具帮助我们开发者使用种类繁多的Paint(画笔)对象在Canvas(画布)上绘制我们希望的视图。Canvas对象提供了一些辅助的方法用来绘制一些原生的2D图像,比如说:圆形,直线,矩形,文本以及图形图像等等。有了这些辅助的方法和辅助的工具,你绘制的视图的复杂性和详细情况仅仅取决于屏幕的大小以及处理器的渲染能力。
注意:在Android平台之上写出高效代码的最重要的技巧就是避免对象的重复创建和回收。对于任何在onDraw()方法当中创建的对象,只要屏幕一刷新,此对象就会被回收。所以把onDraw()方法中所用到的对象最好都定义为成员变量,在构造器当中可以对其初始化。
protected void onDraw(Canvas canvas) {
//get the size of the control based on the last call to onMeasure()
int height = getMeasuredHeight();
int width = getMeasuredWidth();
//Find the center
int px = width/2;
int py = height/2;
//create the new paint brushes
//note:For efficiency this should be done in the view's constructor
Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
//define the string
String displayText = "Hello World!";
//measure the width of the string
float textWidth = mTextPaint.measureText(displayText);
canvas.drawText(displayText, px - textWidth/2, py, mTextPaint);
super.onDraw(canvas);
}
步骤三、计算你的视图的高度和宽度onMeasure()方法在我们自定义视图的父级元素布局我们的自定义的时候,父级元素会提出一个问题:"How much space will you use?", 并且传递两个参数, widthMeasureSpec和heightMeasureSpec.这两个参数指明了对于我们自定义的视图而言,有多大的空间是可以用的以及可用空间的一些元数据信息。但是切记:我们需要在onMeasure()方法当中调用setMeasuredDimension()方法。
public class MyView extends View {
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
//Must make this call to setMeasuredDimension
//or you will cause a runtime exception when the control is laid out
setMeasuredDimension(measuredWidth, measuredHeight);
}
private int measureWidth(int widthMeasureSpec) {
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
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 measureHeight(int heightMeasureSpec) {
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);
int result = 500;
if(specMode == MeasureSpec.AT_MOST){
result = specSize;
}else if(specMode == MeasureSpec.EXACTLY){
result = specSize;
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
//get the size of the control based on the last call to onMeasure()
int height = getMeasuredHeight();
int width = getMeasuredWidth();
//Find the center
int px = width/2;
int py = height/2;
//create the new paint brushes
//note:For efficiency this should be done in the view's constructor
Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
//define the string
String displayText = "Hello World!";
//measure the width of the string
float textWidth = mTextPaint.measureText(displayText);
canvas.drawText(displayText, px - textWidth/2, py, mTextPaint);
super.onDraw(canvas);
}
}
步骤四、怎么使用我们的自定义视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.freedie.customerizedview.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
真机运行效果如下:
最后如果你还是觉得我写的不够详细
看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习ALFA DeV工作室希望可以和大家一起进步。
下载地址:http://download.csdn.net/source/3573096
分享到:
相关推荐
Android 创建自定义View源码~~~~~~~~~~~~~~
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。
Android自定义view组合控件、重写控件的例子源码,创建出对应的组合布局视图,将创建出的视图添加到根节点。Android使用view控件的例子,面向Android开发新手,高手绕行。
这是一个通过xml文件自定义view的一个Demo
这里有很多自定义view可以参考思路思路比较简单,整个view无非两样东西云雨滴这里又包含两部分动画,一部分是云的左右移动动画,一部分是雨滴移动动画 那我们这里可以自定义一些属性,如果对自定义属性还不太了解的...
例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放。 一个设计良好的自定义view和其他设计良好的类很像。封装了某个具有易用性接口的功能组合,这些功能能够有效地使用CPU和内存,...
本文实例为大家分享了Android自定义View实现跟随手指移动的小兔子,供大家参考,具体内容如下 自定义的View实现跟随手指的小兔子 按前面的例子新创建一个project,再在project中新创建一个module 将需要的背景图和...
本文实例讲述了Android编程实现在自定义对话框中获取EditText中数据的方法。分享给大家供大家参考,具体如下: 在项目中忽然遇到这样的问题,需要自定义对话框,对话框需要有一个输入框,以便修改所选中的价格,然后...
Android_Custom_Views 创建自定义视图类声明自定义视图与在 Java 中声明任何其他类非常相似。 在设计自定义类时,请记住以下几点它应该提供一些易于使用的界面并且应该有效地使用内存。 自定义视图应符合 Android ...
本文实例讲述了Android开发之自定义View(视图)用法。分享给大家供大家参考,具体如下: ...为了实现自定义View,需要创建一个新的类,然后重写onDraw方法,在此需要注意,新创建的类MyView要继承View基类,同时还
从TextView继承,创建一个我们自己的TextView,自定义属性,通过自己自定义的属性,修改我们自己TextView的值。
在具体实现代码之前,我们先来了解一下Android api对实现自定义Camera的介绍。 根据api的介绍,对于Camera应用可以简单总结以下几个步骤。 1.检查Camera是否存在,并在AndroidManifest.xml中赋予相关的权限; 2....
自定义view首先要自定义属性: 在values下面创建attrs.xml: <!--画图板--> <attr name=PaintColor> //画笔颜色 <attr name=PaintWidth> // 画笔宽度 <attr name=CanvasImg> //画板图片
主要介绍了Android App中自定义View视图的实例教程,详细讲解了如何在创建View中定义各种锁需要的样式属性,需要的朋友可以参考下
创建一个自定义LinearLayout 控件用来装载课程的信息和课程的周数,和节数大概的布局三这样的 根据上面的看来觉得总体布局我分了两个 上面的星期是一个 下面的节数和格子是一个 总体使用Vertical 而单独内部者使用...
《Android 群英传》读书笔记:自定义 View 之创建复合控件
效果图 实现思路 这个效果实现起来并不难,重要的是思路 此View满足了多种水波纹涟漪扩散效果,这要求它能满足很多的...首先为View创建自定义的xml属性 在工程的values目录下新建attrs.xml文件 <attr name=
本文实例为大家分享了Android自定义View倒计时圆的具体代码,供大家参考,具体内容如下 创建attr <?xml version=1.0 encoding=utf-8?> <!--颜色--> <!-- 进度文本的字体大小 --> ...
在毕设项目中多处用到自定义控件,一直打算总结一下自定义控件的实现方式,今天就来总结一下吧。在此之前学习了郭霖大神博客上面关于自定义View的几篇博文,感觉...1、新建一个Android项目,创建自定义标题栏的布局文件
主要针对Android自定义View过程进行解析,Android创建自定义的view,感兴趣的小伙伴们可以参考一下