自定义View(二) (attrs)

<?xml version="1.0" encoding="utf-8"?>
<resources >
<!--需要自己手写-->
			自己定义的名字	
    <declare-styleable name="myattrs">
	属性				表示资源的引用和资源下Color的引用(其实可以只写一个reference)
        <attr name="bgcolor" format="reference|color">
		枚举	这样在布局文件中用到attr的话就可以直接使用name
            <enum name="yellow" value="0xffffff00"/>
            <enum name="bule" value="0xff0000ff"/>    
        </attr>
        				布尔类型
        <attr name="interceptable" format="boolean"/>
				
        <attr name="drawable" format="reference"/>
					定义大小
        <attr name="drawablewidth" format="dimension"/>
        <attr name="drawableheight" format="dimension"/>
        
            
        
        
    </declare-styleable>
    
    
</resources>


mainavtivity_layout


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res/com.example.customviewgroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.example.customviewgroup.MyViewGroup
        android:layout_width="200dp"
        android:layout_height="200dp"
        my:bgcolor="yellow" 
        my:interceptable="true"
        >


        <com.example.customviewgroup.MyView
            android:layout_width="140dp"
             android:layout_height="140dp"
            my:drawable="@drawable/ic_launcher"
            my:drawableheight="100dp"
            my:drawablewidth="100dp"
            android:background="#cccccc"
            
            />
        
        
    </com.example.customviewgroup.MyViewGroup>


</RelativeLayout>



public class MyViewGroup extends ViewGroup {
	private int color;
	private boolean interceptable;
	public MyViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		//获得res自愿文件下面的attr
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.myattrs);
		//					获得索引						//没取到默认值,代码中“#”要用0xff
		color = a.getColor(R.styleable.myattrs_bgcolor, 0xffcc00cc);
		interceptable=a.getBoolean(R.styleable.myattrs_interceptable, false);
		setWillNotDraw(false);
	}

	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		return interceptable;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		//测量子view的大小,必须写
		measureChildren(widthMeasureSpec, heightMeasureSpec);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		//确定子view的位置
		View view =getChildAt(0);
		view.layout(30, 10, view.getMeasuredWidth()+30, view.getMeasuredHeight()+10);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(color);
	}

}





public class MyView extends ImageView {
	private Drawable drawable;
	private float drawableHeight;
	private float drawableWidth;
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.myattrs);
		drawable=a.getDrawable(R.styleable.myattrs_drawable);
		drawableWidth=a.getDimension(R.styleable.myattrs_drawablewidth, 1);
		drawableHeight=a.getDimension(R.styleable.myattrs_drawableheight, 1);
		
		
	}


	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		//imageview设置里面内容(Drawable)内容
		drawable.setBounds(0, 0, 100, 100);
		drawable.draw(canvas);
	}
	


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值