自定义view之继承View重写onDraw方法
来源:互联网 发布:申请网络试听许可证 编辑:程序博客网 时间:2024/05/10 09:08
首先,这种继承方法主要用于实现一些不规则的效果,一般需要重写onDraw方法。
实现view的构造方法
public CircleView(Context context){
super(context);
init();
};
public CircleView(Context context,AttributeSet attrs){
super(context,attrs);
init();
};在values下新建一个attrs.xml文件
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CircleView"> <attr name="circle_color" format="color" /> </declare-styleable></resources>
public CircleView(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
//自定义属性,先加载然后解析最后释放
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.CircleView);
mColor=a.getColor(R.styleable.CircleView_circle_color,Color.RED);
a.recycle();
init();
};接下来获得画笔对象(Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);去锯齿
int mColor=Color.RED;给画笔设置个颜色)
重写onDraw方法
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//我们要处理padding,不然会是无效的
int paddingLeft=getPaddingLeft();
int paddingRight=getPaddingRight();
int paddingTop=getPaddingTop();
int paddingBottom=getPaddingBottom();
int width=getWidth();
int height=getHeight();
int radius=Math.min(width,height)/2;
canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,radius.mPaint);
}
处理wrap_content
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);// widthMeasureSpec = MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);// heightMeasureSpec = MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);// setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); // 在wrap_content的情况下默认长度为200dp int minSize = 200; // wrap_content的specMode是AT_MOST模式,这种情况下宽/高等同于specSize // 查表得这种情况下specSize等同于parentSize,也就是父容器当前剩余的大小 // 在wrap_content的情况下如果特殊处理,效果等同martch_parent if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(minSize, minSize); } else if (widthSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(minSize, heightSpecSize); } else if (heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthSpecSize, minSize); } }}
- 自定义view之继承View重写onDraw方法
- 纯粹自定义view即继承view(重写onMeasure() onDraw())
- 自定义View-4-重写onDraw
- 自定义View之onDraw
- 自定义View之onDraw方法详解
- 自定义View之draw()与onDraw()方法
- Android 自定义View需要重写ondraw()等方法
- 自定义绘图View之onDraw
- 自定义View之onDraw()
- 自定义view之继承view
- 自定义View之 继承View
- 自定义View onDraw方法不被执行
- 自定义的VIEW没有执行onDraw方法
- 自定义View(三) 之 onMeasure() onLayout() onDraw()
- 自定义View之onMeasure(),onLayout(),onDraw()
- Android自定义View基础之onDraw详解
- 自定义view(二):onDraw 之 Paint
- 自定义view(三):onDraw 之 Canvas
- Remove Duplicate Letters
- 李开复:算法的力量
- Node.js模板引擎: Jade入门
- 修复跨站脚本攻击:研发人员指南(Java 版本)
- 间谍卫星的基础?YOLT——利用卷积神经网络对卫星影像进行多尺度目标检测(Part I)
- 自定义view之继承View重写onDraw方法
- fieldset会有一个外白色边框 如何去掉
- Light oj-1259 - Goldbach`s Conjecture
- Redis数据结构和对象(六)
- 【名物】鳴門鯛焼本舗
- centos7在虚拟机中不能联网解决办法
- 计算机的组成及内存的初步理解
- 研究微信即时通讯的服务端、朋友圈、红包、推送等方案
- Tomcat通过文件路径URL下载自动重命名文件