android自定义View

1.说明

android开发时,很多时候我们会用到安卓原生的组件,如最常见的Button、ImageView、Menu、Spinner等等。但是在某些情况下,这些“简单的组件”已经不能满足我们的需求,楼主一般会自定义的组件有gallery、searchview等。下面将距离说明如何自定义组件。

2.最简单的做法:

最常见的做法就是继承view(或view的子类),并重写  onDraw(Canvas canvas)或onCreate(Bundle savedInstanceState)方法。所有的组件都是一个view或是view的子类,所以自定义组件的时候我们也不能逃脱。
public class CustomText extends View {
	private Paint paint;
	private final String SHOW_CONTENT = "今日头条";

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.BLUE);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2);
		paint.setShader(new Shader());
		RectF rF = new RectF(10, 10, 80, 60);
		canvas.drawRoundRect(rF, 85, 35, paint);
		canvas.drawText(SHOW_CONTENT, 35, 15, paint);
	}

	public CustomText(Context context) {
		super(context);
		Log.i("CustomText", "CustomText(Context context)");
	}

	public CustomText(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("CustomText", "CustomText(Context context, AttributeSet attrs)");
	}

	public CustomText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.i("CustomText",
				"CustomText(Context context, AttributeSet attrs, int defStyle)");
	}

}
效果图展示:
从上面的代码我们可以发现,这种写法耦合性太强,例如我们有用到Stirng类型的属性值SHOW_CONTENT。这种写法不推荐,下面介绍另外一种更值得推荐的方法。

3.定义属性 attrs

Android中可以自定义控件,有时候我们需要为这些自定义的空间加上一些属性,Java代码中可以定义属性变量没有问题,那么XML文件中怎么使用属性呢?那么就需要在XML文件中自定义控件属性(可以参照ADT\sdk\platforms\android-17\data\res\values\attrs.xml)。
假设我们现在需要自定义一个组件FlowIndicator
public class FlowIndicator extends View {
	private int count;
	private float space, radius;
	private int point_normal_color, point_seleted_color;
	private int seleted = 0;
	...
}
我们现在有count、space、radius等六个属性,其中selected已经有值0。如何才能为该组件的属性赋值呢,我们可以通过xml文件attrs.xml来实现。
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="FlowIndicator">
        <attr name="count" format="integer" />
        <attr name="space" format="dimension" />
        <attr name="point_size" format="dimension" />
        <attr name="point_seleted_color" format="color|reference" />
        <attr name="point_normal_color" format="color|reference" />
        <attr name="point_radius" format="dimension" />
    </declare-styleable>

</resources>
如上所示,format为属性的格式。
下面是如何获得自定义的属性值:
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				R.styleable.FlowIndicator);
		count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
		space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
		radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius,
				9);
		point_normal_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_normal_color, 0x000000);
		point_seleted_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
		int sum = attrs.getAttributeCount();
		typeArray.recycle();
使用FlowIndicator,并赋值:
        <com.widget.FlowIndicator
            android:id="@+id/myView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            app:count="0"
            app:point_normal_color="#45000000"
            app:point_radius="4dip"
            app:point_seleted_color="#32cd32"
            app:point_size="5dip"
            app:space="10dip" />
以上两种情况已经可以满足绝大部分需求,另外一种情况作为补充,即重写组件的回调方法。例如:
public class ClearEditText extends EditText implements  
        OnFocusChangeListener, TextWatcher{
...
}
效果图展示:
                                      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值