自定义View笔记(一)

来源:互联网 发布:开机自动运行软件 编辑:程序博客网 时间:2024/06/05 02:56

Android系统为我们提供了丰富的组件来创建丰富的UI效果,有些时候系统原生的一些组件不能够满足我们的需要,这时就需要我们去自定义自己的View了。然后这并不是件容易的事,刚开始接触的时候,所有的东西可能要从网上去获取,可是大多数时候也有可能并不是适合自己的需求,就需要自己去写了。然后自己想破脑袋也不知道该怎么下手,这是我刚开始接触的时候遇到的最大的问题,就只能先从基础学起,慢慢的学会自定义的View。


在自定义View的时候,我们通常回去重写onDraw()方法来绘制View的显示内容。如果该View还需要使用wrap_content属性,那么还必须重写onMeasure()方法。也可以自己自定义属性来配置值。


在View中通常有一下一些比较重要的回调方法。


1.onFinishInflate(): 从XML加载组件后回调

2.onSizeChanged():组件大小改变时回调

3.onMeasure():回调该方法来进行测量

4.onLayout():回调该方法来确定显示的位置

5.onTouchEvent():监听到触摸事件时回调

这些方法不需要全部重写,只需要根据自己的需求重写一些方法。


package com.example.circleview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.util.AttributeSet;import android.view.View;import com.example.circleview.R.styleable;public class CircleView extends View{private int mColor;private int mColor2;private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);  // 实例化画笔并打开抗锯齿  private Paint mPaint2=new Paint(Paint.ANTI_ALIAS_FLAG);  // 实例化画笔并打开抗锯齿  public CircleView(Context context) {super(context);init();// TODO Auto-generated constructor stub}public CircleView(Context context,AttributeSet attrs){this(context,attrs,0);init();}public CircleView(Context context,AttributeSet attrs,int defStyleAttr){super(context,attrs,defStyleAttr);TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.CircleView);mColor=a.getColor(styleable.CircleView_circle_color, Color.BLACK);mColor2=a.getColor(styleable.CircleView_circle_color, Color.BLUE);a.recycle();init();}private void init(){mPaint.setColor(mColor);mPaint.setStyle(Paint.Style.FILL); }@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST){setMeasuredDimension(200, 200);}else if(widthSpecMode==MeasureSpec.AT_MOST){setMeasuredDimension(200, heightSpecSize);}else{setMeasuredDimension(widthSpecSize, 200);}}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);canvas.save();final int paddingLeft=getPaddingLeft();final int paddingRight=getPaddingRight();final int paddingTop=getPaddingLeft();final  int paddingBottom=getPaddingBottom();int width=getWidth()-paddingLeft-paddingRight;int height=getHeight()-paddingTop-paddingBottom;int radius=Math.min(width, height)/2;canvas.drawCircle(paddingLeft+width/2, paddingTop+height/2, radius, mPaint);mPaint.setColor(mColor2);canvas.drawCircle(paddingLeft+width/2, paddingTop+height/2, radius/2, mPaint);}}


3 0
原创粉丝点击