[Android自定义控件] Android自定义控件
public class MyView extends View {private String mtext;private int msrc;public MyView(Context context) {super(context);}public MyView(Context context, AttributeSet attrs) {super(context, attrs);int resourceId = 0;int textId = attrs.getAttributeResourceValue(null, "Text",0);int srcId = attrs.getAttributeResourceValue(null, "Src", 0);mtext = context.getResources().getText(textId).toString();msrc = srcId;}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();paint.setColor(Color.RED);InputStream is = getResources().openRawResource(msrc); Bitmap mBitmap = BitmapFactory.decodeStream(is);int bh = mBitmap.getHeight();int bw = mBitmap.getWidth();canvas.drawBitmap(mBitmap, 0,0, paint);//canvas.drawCircle(40, 90, 15, paint);canvas.drawText(mtext, bw/2, 30, paint);} }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.example.myimageview2.MyViewandroid:id="@+id/myView1"android:layout_width="wrap_content"android:layout_height="wrap_content" Text="@string/hello_world"Src="@drawable/xh"/></LinearLayout>
属性Text, Src在自定义View类的构造方法中读取。
public class MyImageView extends LinearLayout {public MyImageView(Context context) {super(context);}public MyImageView(Context context, AttributeSet attrs) {super(context, attrs);int resourceId = -1;TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyImageView);ImageView iv = new ImageView(context);TextView tv = new TextView(context);int N = typedArray.getIndexCount();for (int i = 0; i < N; i++) {int attr = typedArray.getIndex(i);switch (attr) {case R.styleable.MyImageView_Oriental:resourceId = typedArray.getInt(R.styleable.MyImageView_Oriental, 0);this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL: LinearLayout.VERTICAL);break;case R.styleable.MyImageView_Text:resourceId = typedArray.getResourceId(R.styleable.MyImageView_Text, 0);tv.setText(resourceId > 0 ? typedArray.getResources().getText(resourceId) : typedArray.getString(R.styleable.MyImageView_Text));break;case R.styleable.MyImageView_Src:resourceId = typedArray.getResourceId(R.styleable.MyImageView_Src, 0);iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);break; }}addView(iv);addView(tv);typedArray.recycle();} }
attrs.xml进行属性声明, 文件放在values目录下
<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="MyImageView"><attr name="Text" format="reference|string"></attr><attr name="Oriental" ><enum name="Horizontal" value="1"></enum><enum name="Vertical" value="0"></enum></attr><attr name="Src" format="reference|integer"></attr></declare-styleable></resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><com.example.myimageview2.MyImageViewandroid:id="@+id/myImageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"uview:Text="这是一个图片说明" uview:Src="@drawable/tw"uview:Oriental="Vertical"></com.example.myimageview2.MyImageView></LinearLayout>
六:自定义View的方法
[Android自定义控件] Android自定义控件相关推荐
- Android开发技巧——自定义控件之自定义属性
Android开发技巧--自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码. 上一篇讲了如何通过xml把几个控件组织起来,并继承某个 ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- Android开发之自定义控件——直尺
Android开发之自定义控件--直尺 本人的第一篇技术博客,希望能够在分享的路上走的更远. 先上效果图: 涉及到的知识点: - Paint类 - Cancas类 - Path类 * 由于功能简单,此 ...
- Android开发之自定义控件的基本介绍(附源码)
老套路先看效果图: 一个特别简单的字母排序列表如上图: 先看下有哪些属性: <com.xiayiye.honorfirst.custom.CustomNumViewandroid:id=&quo ...
- 【Android】Android开源项目分类汇总
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- android之android.intent.category.DEFAULT的用途和使用
1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...
- android:layout_with=,android – 难以理解layout_alignWithParentIfMissing
这仅适用于使用RelativeLayout时. 如果您将元素设置为一个其他元素,则表示该元素位于该元素的左侧. 但是如果这个元素会丢失,因为你删除它,例如它将与父对齐. 举个例子 android:la ...
- [Android Studio] Android Studio常用快捷键
[Android Studio] Android Studio常用快捷键 (会持续更新)这边讲的常用快捷键是指做完Keymap到Eclipse后的,不是纯Android Studio的,这边主要讲下比 ...
- Android利用android:indeterminateDrawable来实现ProgressBar三种方式
方式1:(效果为补间动画一样) [html] view plaincopyprint? <ProgressBar android:layout_width="wrap_content& ...
- Android之Android实现浮层的上下滑动(支持内部添加View)
前言 我K,今天居然是情人节,对于资深的单身狗来说,简直是个噩耗,今天注定是各种秀恩爱,心塞中.... 话题到此结束,管他什么情人节,今天给大家带来的是一个浮层的上下滑动,浮层滑动时分三种状态:全部显 ...
最新文章
- 针对OAuth2的CSRF攻击
- [Egret]长按截屏分享、分享截屏图片、本地存储
- How To Do @Async in Spring--转
- 2011年c语言二级计算机考试,2011年9月全国计算机等级考试二级C语言机试
- “埋点”到底要不要?
- erlang rebar 配置mysql_Erlang Rebar 使用指南之四:依赖管理
- Libevent初探
- Spring Boot笔记-WebSocket的使用
- 在linux上安装jenkins
- 如何启动php程序下载,php – 最好的方式来启动下载?
- 原生JavaScript如何解决父元素查找指定类名的子元素的问题
- windows server 2012 动态访问控制
- 网络摄像机 c++ 抓拍_IP摄像机和工业摄像机怎么区分 IP摄像机和工业摄像机差异...
- oracle asm文件查找,Oracle ASM 文件管理
- 微信小程序:最新掌上题库微信小程序源码下载,修复登录接口,支持在线考试,自定义导入考题
- Autodesk Maya入门到精通学习教程
- SPL工业智能:发现时序数据的异常
- StringBuffer(史上最详细)
- 学习笔记:使用endnotesX9插入参考文献
- Y Combinator Is Boot Camp for Startups
热门文章
- 如何以CustomValidator搭配jQuery AJAX进行Server端验证(转)
- 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解
- Box,( UVa, 1587 )
- 大话机器学习之数据预处理与数据筛选
- hdu 1520(树形dp)
- 深入Linux内核架构(中文版)pdf
- 来教你用什么泡脚好,泡脚的好处有那些?
- Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
- uoj#422. 【集训队作业2018】小Z的礼物(MIn-Max容斥+插头dp)
- 使用 profile 进行python代码性能分析