自定义控件2.第一个自定义view
1. 继承 View, 重写 onDraw 方法
/**
* 自定义一个简单的 View
*
* @author GAOYUAN
*
*/
public class CustomView1 extends View {
private Paint mPaint = new Paint();
private Rect mRect = new Rect();
- // 一般要实现下面两个构造方法
public CustomView1(Context context) {
super(context);
}
- // 第二个参数后面会讲到
public CustomView1(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Style.FILL);
mPaint.setColor(Color.BLUE);
mRect.set(0, 0, 100, 100);
// Avoid object allocations during draw/layout operations (preallocate
// and reuse instead)
// 避免在 draw/layout 方法中进行对象分配, 提前分配, 或者重用.
// canvas.drawRect(new Rect(0, 0, 100, 100), mPaint);
canvas.drawRect(mRect, mPaint);
// mPaint.reset();
mPaint.setColor(Color.RED);
mPaint.setTextSize(30.0f);
// 注意, 画文字的时候, 是从左下角开始画的.
canvas.drawText("Hello!", 0, 100, mPaint);
}
}
2. 在布局文件中使用
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.gaoyuan4122.customui.views.CustomView1
android:id="@+id/cv_custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_hello" />
<TextView
android:id="@+id/tv_hello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_below="@id/cv_custom1" />
3. 关于控件的宽高
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.gaoyuan4122.customui.views.CustomView1
android:id="@+id/cv_custom1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/tv_hello" />
<TextView
android:id="@+id/tv_hello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_below="@id/cv_custom1" />
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
转载于:https://www.cnblogs.com/ywq-come/p/5927301.html
自定义控件2.第一个自定义view相关推荐
- android自定义控件几种,Android 自定义View一个控件搞定多种水波纹涟漪扩散效果 - CSDN博客...
效果图 实现思路 这个效果实现起来并不难,重要的是思路 此View满足了多种水波纹涟漪扩散效果,这要求它能满足很多的变化 根据上面的样式,可以看出此View需要满足以下变化 圆圈从中心可循环向外扩散 ...
- 自定义View学习之仿QQ运动步数进度效果
自定义View学习的小记录之第一篇 自定义View学习之QQ计步器 如何实现 1.分析想要得到什么效果 2.确定自定义属性,编写attrs.xml 3.在布局中使用 4.在自定义View中获取自定义属 ...
- android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...
转载:http://blog.csdn.net/xiabing082/article/details/48781489 1. 大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...
- [自定义控件]android自定义view实战之太极图
android自定义view实战之太极图 尊重原创,转载请注明出处: http://blog.csdn.net/qq137722697 自定义view是Android工程师进阶不可避免要接触的,我的学 ...
- android自定义控件中文乱码,Android笔记--自定义View之组合控件
Android-自定义View 分享是最好的记忆-- 如需转发请注明出处 [强调]:共同学习 共同进步 不喜勿喷 内容简介 前言 实现 总结 1. 前言 这次更新有2个目的 1. 复用控件,而不是每次 ...
- android自定义控件是一个 内部类 如何在xml中引用,android 自定义view属性
android 自定义view属性 一个完美的自定义控件也可以添加xml来配置属性和风格.要实现这一点,可按照下列步骤来做: 1) 添加自定义属性到xml文件中 2) 在xml的中,指定属性的值 3) ...
- Android自定义控件面试题,自定义View面试总结
本着针对面试,不负责任的态度,写下<面试总结>系列.本系列记录面试过程中各个知识点,而不是入门系列,如果有不懂的自行学习. 自定义View三种方式,组合现有控件,继承现有控件,继承View ...
- 教你搞定Android自定义View
Android App开发过程中,很多时候会遇到系统框架中提供的控件无法满足我们产品的设计需求,那么这时候我们可以选择先Google下有没有比较成熟的开源项目可以让我们用,当然现在Github上面的项 ...
- Andoid自定义View的OnMeasure详解和自定义属性
一,OnMeasure详解 Android开发中偶尔会用到自定义View,一般情况下,自定义View都需要继承View类的onMeasure方法,那么,为什么要继承onMeasure()函数呢?什么情 ...
- Android 自定义View,自定义属性--自定义圆形进度条(整理)
很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...
最新文章
- Linux使用netstat命令查看并发连接数
- 【AI竞赛】TinyMind汉字书法识别挑战赛开始报名啦!!
- taro 如何使用dom_taro 事件处理
- 布袋除尘器过滤风速多少_布袋除尘器处理风量、过滤风速、过滤面积及阻力的选型计算...
- C# Bitmap类型与Byte[]类型相互转化
- loadrunner中并发数与迭代的区别
- mockJs文档(一)
- 在 F5 LTM 上配置数据包过滤
- javascript详解函数原型对象prototype与constructor
- python脚本用类编写_跟老齐学Python之编写类之二方法
- 大数据处理需注意的问题
- 光伏并网pscad_100kW级组串式光伏逆变器的英飞凌模块方案介绍
- 用91卫图助手获取研究区(勾画研究矢量shp格式范围等)
- ocx注册以及检测(转)
- 在哪些场景下要使用CDN加速服务
- 【AUTOSAR-COM】-10.3-接收的IPDU Callout(Com_RxIpduCallout)的使用小结
- threejs导入简单地图模型
- 在JSP2.0中使用EL表达式语言
- rsync daemon模式介绍
- E3游戏展十大观察:游戏行业缺乏惊喜1A1M
热门文章
- 工作要求,写了一些没用的设计文档
- 编译问题解决:mkdir: 无法创建目录/usr/local/share/man/man1: 文件已存在
- python中RGB缓冲区红蓝颠倒的解决办法
- 一个简单的TCP客户/服务器的程序
- Writing Serializable Classes---定义可序列化的类
- bat 设置变量带中文
- mysql5.5函数大全_mysql 函数大全
- c语言代码大全和详细解释_最详细的Simulink代码生成(C语言)
- 批量修改mac系统文件的可读写权限
- 12.1-12.5 LNMP架构介绍 , MySQL安装 , PHP安装, Nginx介绍