自定义View

来源:互联网 发布:cad制图软件下载 编辑:程序博客网 时间:2024/05/02 01:59

第一步,写一个类继承于View或者已有的view控件,

继承后后报错,提示添加构造方法,我们用两个参数的构造方法


第二步:重载onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法

在这个方法里面测量view的大小,(如果继承已有的view,就不需要自己去测量了)


注意:onLayout(boolean changed, int left, int top, int right,
int bottom) 


这个方法对于自定义View来讲,用不到,没什么用

/**
* 决定view的位置,这个没什么用,因为view本身没有决定权,决定权在ViewGroup那边
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
}



第三步:重载onDraw(Canvas canvas)方法,开始画控件



好了,一个控件就这样完成了,很简单吧,

控件画好只好还可以对控件进行设置监听和触摸事件,以处理一些逻辑



接下来,讲讲自定义属性

第一步:想要有哪些属性,就必须先去res下的values文件夹下创建一个xml文件夹,用来声明属性的,这个格式如果不会写,可以去抄源码,看看人家怎么写的

<?xml version="1.0" encoding="utf-8"?><resources>     <declare-styleable name="View">                <attr name="status" format="boolean" />        </declare-styleable></resources>

第二步;属性在xml文件里声明好了之后,我们就开始拿来用,用的时候,在布局文件里发现还是报 了一个错,这就是第二步要做的事情了,添加一个命名空间,抄一个安卓自带的那个命名空间修改一些就可以了,最后的改成自己的报名,头开成自己爽的,可以随意写,然后写属性的时候就以这个为头

xmlns:android="http://schemas.android.com/apk/res/android"    //安卓自带的    xmlns:huang="http://schemas.android.com/apk/res/com.example.mybutton"  //我的


第三步:重点来了,在自定义view的类里面,找构造方法的AttributeSet 属性

通过这个属性的一个getAttributeBooleanValue可以获得写在布局文件里的属性值

第一个参数是写你自己添加上去的命名空间,第二个参数是属性的名声,第三个是默认值

status=attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.example.mybutton", "status", false);

好了,得到属性值之后,我们就可以根据属性值对控件进行一个设置了



开始上代码了,

public class mButton extends View {private float Slidebnt=0 ;//开关按钮的位置private float Slide;  //手指拉动的距离private float LeftMax;//开关按钮距离View左边的最大距离private float LeftMin;//开关按钮距离View左边的最小距离private boolean isSlid;private boolean status; //开关的状态private Bitmap backgroundBitmap; //开关的背景图片private Bitmap SlideBitmap;//开关按钮的图片float first;float last;public mButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubstatus=attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.example.mybutton", "status", false);//背景backgroundBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);//拖动的图片SlideBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);if(status){Slidebnt=backgroundBitmap.getWidth()-SlideBitmap.getWidth();}else{Slidebnt=0;}//设置点击事件setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(isSlid){return;}Log.i("huang","hhh");if(status){status=!status;Slidebnt=0;}else{status=!status;Slidebnt=backgroundBitmap.getWidth()-SlideBitmap.getWidth();}invalidate();}});}/** * 第一步:测量view的大小, */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());}/** * 决定view的位置,这个没什么用,因为view本身没有决定权,决定权在ViewGroup那边 */@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {// TODO Auto-generated method stubsuper.onLayout(changed, left, top, right, bottom);}/** * 第三部:开始画控件 */@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint=new Paint();canvas.drawBitmap(backgroundBitmap, 0, 0, paint);canvas.drawBitmap(SlideBitmap, Slidebnt, 0, paint);}//触摸事件@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubsuper.onTouchEvent(event);LeftMax=backgroundBitmap.getWidth()-SlideBitmap.getWidth();LeftMin=0;switch(event.getAction()){case MotionEvent.ACTION_DOWN:first=event.getX();isSlid=false;break;case MotionEvent.ACTION_MOVE:last=event.getX();Slide=first-last;first=last;if(Math.abs(Slide)>1.5){isSlid=true;}if(status){Slidebnt=Slidebnt-Slide>LeftMin?(Slidebnt=Slidebnt-Slide<LeftMax?Slidebnt-Slide:LeftMax):LeftMin;}else if(!status){Slidebnt=Slidebnt-Slide>LeftMax?LeftMax:(Slidebnt=Slidebnt-Slide>0?Slidebnt-Slide:LeftMin);}invalidate();break;case MotionEvent.ACTION_UP:if(Slidebnt==0||Slidebnt==LeftMax){}else if(status){Slidebnt=Slidebnt<=LeftMax/2?0:LeftMax;}else if(!status){Slidebnt=Slidebnt>=LeftMax/2?LeftMax:0;}if(Slidebnt==0){status=false;}else{status=true;}invalidate();break;}return true;}}


0 0
原创粉丝点击