Android 自定义控件与属性

自定义控件

很多时候需要我们自己动手画出我们想要的控件,或者图形。有了他你会有更加开阔的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>
    <!--
属性
reference
string
color
dimension
boolean
integer
float
fraction
enum
flag
format里面可以引用这些属性
    -->
</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/12509731
		paint.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
希望大家多多指点


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值