自定义控件和自定义属性

自定义控件

我们需要定义一个类MyView继承View,重写构造方法(三个)和onDraw()方法

在activity布局中,我们需要这样引用

 <com.ccc.myview.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

自定义控件需要用到这两个类 paint 画笔        canvas 画布

public class MyView extends View {
	
	private Paint paint;

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		paint = new Paint();
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
	}

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		paint = new Paint();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		//设置画笔的颜色
		paint.setColor(Color.YELLOW);
		
		//设置文字的大小
		paint.setTextSize(30);
		
		//设置画笔抗锯齿,文字看起来就圆滑了
		paint.setAntiAlias(true);
		
		//画文字
		canvas.drawText("我是自定义控件", 0, 100, paint);
		
		//画长方形
		canvas.drawRect(0, 0, 150, 50, paint);
	}

}

有时候我们需要自定义属性,这样是项目更维护更灵活,开发中常用到

自定义属性

这里自定义color和textsize属性

1、在res\values下新建attrs.xml文件

<resources>
    <declare-styleable name="myview">
        <attr name="tv_size" format="dimension"/>
        <attr name="tv_color" format="color" />
    </declare-styleable>
</resources>


2、同时在res\values下新建colors.xml和dimens.xml文件

<resources>

    <color name="red">#f00</color>
    
</resources>


 

<resources>

    <dimen name="textsize">30sp</dimen>
    
    
</resources>

3、在布局里引用属性

注意要引用自己这个包xmlns:myview=http://schemas.android.com/apk/res/com.ccc.custom ,编写自己的命名空间

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myview="http://schemas.android.com/apk/res/com.ccc.custom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.ccc.custom.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myview:tv_size="@dimen/textsize" 
        myview:tv_color="@color/red"/>

</LinearLayout>

 

4、在构造方法(两个参数的)里面处理自己的属性


 

public class MyView extends View {
	
	private Paint paint;

	public MyView(Context context) {
		super(context);
		paint = new Paint();
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		
		//加载自定义属性
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.myview);
		
		//获取属性的值 后面为默认值
		float tv_size = typedArray.getDimension(R.styleable.myview_tv_size, 20);
		int tv_color = typedArray.getColor(R.styleable.myview_tv_color, Color.WHITE);
		
		paint.setTextSize(tv_size);
		paint.setColor(tv_color);
		
		//释放资源
		typedArray.recycle();
	}

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		paint = new Paint();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		
		//设置画笔抗锯齿
		paint.setAntiAlias(true);
		
		//画文字
		canvas.drawText("我是自定义控件", 0, 100, paint);
		
	}

}



 

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值