- 浏览: 326227 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
Android自定义View实现很简单
继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
实例:
[code="java"] package demo.view.my; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; /** * 这个是自定义的TextView. * 至少需要重载构造方法和onDraw方法 * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了 * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称 * 并根据需要设定默认值,放在在xml文件中没有定义。 * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas, * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my" * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包 * @author Administrator * */ public class MyView extends View { Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息 public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs){ super(context, attrs); mPaint = new Paint(); //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组 //在使用完成后,一定要调用recycle方法 //属性的名称是styleable中的名称+“_”+属性名称 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定 float textSize = array.getDimension(R.styleable.MyView_textSize, 36); mPaint.setColor(textColor); mPaint.setTextSize(textSize); array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响 } public void onDraw(Canvas canvas){ super.onDraw(canvas); //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形 //mPaint = new Paint(); //mPaint.setColor(Color.RED); mPaint.setStyle(Style.FILL); //设置填充 canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形 mPaint.setColor(Color.BLUE); canvas.drawText("我是被画出来的", 10, 120, mPaint); } }
相应的属性文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
在布局文件中的使用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res/demo.view.my" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <demo.view.my.MyView android:layout_width="fill_parent" android:layout_height="wrap_content" my:textColor="#FFFFFFFF" my:textSize="22dp" /> </LinearLayout>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chenjie19891104/archive/2011/04/11/6315668.aspx
发表评论
-
Android中AsyncTask的简单用法
2012-01-13 16:00 1130在开发Android移动客户端的时候往往要使用多线程来进行操 ... -
Android应用的自动升级、更新模块的实现 .
2011-11-16 14:01 650http://www.eoeandroid.com/threa ... -
一个APK反编译利器Apktool
2011-11-16 13:54 1561一个APK反编译利器Apktool APK 本地化 ... -
自定义Android标题栏TitleBar布局
2011-11-14 14:13 1223很多网友发现自己Android程序的标题栏TitleBar区域 ... -
Android GPS获取地理位置 .
2011-11-14 14:11 831import android.app.Activity; i ... -
android ListView详解
2011-11-14 13:48 1029在android开发中ListView是比较常用的组件,它以列 ... -
Android之Content provider 详解
2011-11-14 13:35 2442Android是如何实现应用程序之间数据共享的?一个应用程序可 ... -
Android源码地址
2011-11-12 19:14 1029http://blog.csdn.net/ilittleone ... -
android之File
2011-11-11 22:39 19761:Fileservice package cn.itcas ... -
Android知识补漏
2011-11-09 22:33 01:AndroidManifiest.xml < ... -
深入剖析Android消息机制
2011-11-09 14:13 940在Android中,线程内部或者线程之间进行信息交互时经常会使 ... -
Android之Handler详解(四)
2011-11-09 14:00 1268d、自己创建新的线程,然后在新线程中创建Looper,主线程调 ... -
Android之Handler详解(三)
2011-11-09 13:58 1355c、将消息队列绑定到子线程上,主线程只管通过Handl ... -
Android之Handler详解(二)
2011-11-09 13:54 1641二:sendMessage版本的Handl ... -
Android之Handler详解(一)
2011-11-09 13:22 2271一个Handler允许你发送和处理消息(Message)以及 ... -
关于StartActivityForResult方法的使用
2011-10-31 17:11 1061根据方法名可知 这个方法是要得到启动后的Activity返回的 ... -
Android 悬浮Activity并可拖动(访悬浮歌词)
2011-10-24 16:23 2066天天动听, 这款Android手机上的音乐播放器,相信不少朋友 ... -
Android GWES
2011-10-24 16:13 1165第八章 Android GWES 8.1 View Syst ... -
Android系统服务-WindowManager
2011-10-24 16:10 1423WindowManager是Android中一个重要的服务 ... -
http通信
2011-10-15 17:31 1074HTTP(HyperText Transfer Proto ...
相关推荐
1:水晶(水滴),波浪球实现步骤详解 2:自定义View 最佳实现实例 3:波浪线绘制原理
Android 自定义View (验证码)项目实例完整源码 Android 自定义View (验证码)项目实例完整源码 Android 自定义View (验证码)项目实例完整源码 Android 自定义View (验证码)项目实例完整源码
android 自定义view比较综合的例子,涉及到一些复杂的实现效果,有一些基础的人可以参考下。
Android自定义View基本绘制流程及实例
一个自定义的android view 用于模拟运动的背景
Android自定义View实现转盘旋转的效果
android自定义View之NotePad出鞘记,自定义NotePad,android自定义笔记本
用自定义View的方式来实现圆形的遥控器菜单。更多关于android自定义的知识可访问:http://blog.sina.com.cn/s/blog_9c4f0e790102vyt3.html
android自定义View实现SwitchView
android 自定义view及自定义属性
Android自定义View之高仿QQ健康 ,谷歌deMaterial Design 风格,自定义界面,比较好用的样式
Android自定义View之进度条Demo,Android自定义View之进度条Demo
仿真书籍翻页效果BookPageView简介:实现了仿真翻页效果,教程完整地描述了翻页原理分析到性能优化的过程教程博客:Android自定义View——从零开始实现书籍翻页效果(一) Android自定义View——从零开始实现书籍...
http://blog.csdn.net/niubitianping/article/details/51933523
android 自定义View界面大合集
本篇文章主要介绍了android自定义view之实现日历界面实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
Android自定义View之签名板,在控件上用触控写出名字,提取jpg格式文件并保存
android 自定义View 实例,不熟悉的朋友,可以看看这个demo,可以改写成任何你想要的样子。
Android自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View...
Android 自定义View之随机生成图片验证码