Android 自定义控件与属性
来源:互联网 发布:听歌识曲软件下载 编辑:程序博客网 时间:2024/05/22 03:45
自定义控件
很多时候需要我们自己动手画出我们想要的控件,或者图形。有了他你会有更加开阔的UI设计.直接上代码
1、xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.mono.activity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <!-- com.mono.activity" 应用的包名 --> <!-- http://schemas.android.com/apk/res/ 是固定的 --> <com.mono.activity.CustomImageView android:id="@+id/btntwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/record_bg" app:roundHeight="10dp" app:roundWidth="10dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dp" android:src="@drawable/record_bg" /></LinearLayout>xmlns:app 的app是前缀
http://schemas.android.com/apk/res/ 固定
com.mono.activity 应用的包名
这样你就可以往下继续了
2、 编写控件
<com.mono.activity.CustomImageView android:id="@+id/btntwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/record_bg" app:roundHeight="10dp" app:roundWidth="10dp" />这是你自定义的控件,不用多说代码在CustomImageView.java里面
3、配置文件
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="RoundAngleImageView"> <attr name="roundWidth" format="dimension" /> <attr name="roundHeight" format="dimension" /> </declare-styleable> <!--属性referencestringcolordimensionbooleanintegerfloatfractionenumflagformat里面可以引用这些属性 --></resources>
4、关键代码
private void init(Context context, AttributeSet attrs) {if (attrs != null) {//AttributeSet是节点的属性集合//TypedArray 属性的容器//getDimension()是基于当前DisplayMetrics进行转换,获取指定资源id对应的尺寸。文档里并没说这里返回的就是像素,要注意这个函数的返回值是float,像素肯定是int。//getDimensionPixelSize()与getDimension()功能类似,不同的是将结果转换为int,并且小数部分四舍五入。//getDimensionPixelOffset()与getDimension()功能类似,不同的是将结果转换为int,并且偏移转换(offset conversion,函数命名中的offset是这个意思)是直接截断小数位,即取整(其实就是把float强制转化为int,注意不是四舍五入哦)。TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RoundAngleImageView);roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);//typedArray.recycle();//为了保持以后使用该属性一致性} else {float density = context.getResources().getDisplayMetrics().density;roundWidth = (int) (roundWidth * density);roundHeight = (int) (roundHeight * density);}paint = new Paint();paint.setColor(Color.BLUE);paint.setAntiAlias(true);//PorterDuffXfermode 这是一个非常强大的转换模式,使用它,可以使用图像合成的16条Porter-Duff规则的任意一条来控制Paint如何与已有的Canvas图像进行交互//http://blog.csdn.net/hiyohu/article/details/12509731paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));paint2 = new Paint();paint2.setXfermode(null);}
配置文件 TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RoundAngleImageView); 这里有引用
注释以及引用其他大手们的链接都在里面。感谢他们的帖子。 代码我已经加上注释
源码下载地址: http://download.csdn.net/detail/wang84100/7322331
希望大家多多指点
0 1
- Android 自定义控件与属性
- Android自定义控件与属性
- Android之Android 自定义控件与属性
- android 自定义控件属性
- Android自定义控件属性
- android 自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android控件:自定义属性
- Android自定义控件属性
- android 自定义控件属性
- Android 自定义控件属性
- Android 自定义控件属性
- 自定义控件与自定义属性
- Android自定义控件 自定义属性
- Android 自定义控件 自定义属性
- bind/function使用
- Centos 升级GLIBCXX
- EF二三事
- ClickableSpan造成Listview的OnItemClickListener失效的解决办法
- check_sudoku
- Android 自定义控件与属性
- 男子自制吸金球盗黄金数斤,狂赚200万
- Linux系统中 任务、进程和线程总结
- 《程序员的修炼——从优秀到卓越》
- wordpress建博客的步骤
- MySQL学习笔记之一 MySQL入门
- 【转Oracle补丁】老托的Oracle 数据库Patch概念性小常识
- css border 外边使用方法
- java获取运行时虚拟机内存情况