Android学习笔记-自定义view
来源:互联网 发布:python exe 编辑:程序博客网 时间:2024/05/16 04:00
在android开发中有时会找不到想要的控件样式,这时就需要自定义view了。
首先看一下自定义view的进本步骤:
- 继承view,重写onMeasure(),onDraw()方法
- 建立atts.xml文件
自定义view
public class ToggleButton extends View { Bitmap background; Bitmap toggle; Paint bgOpenPaint; Paint bgClosePaint; Paint tgPaint; int width; int height; float current; private boolean status=true; boolean isMove=false; public ToggleButton(Context context) { super(context); } public ToggleButton(Context context, AttributeSet attrs) { super(context, attrs); // ToggleButton(context,attrs,0); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleButton); int bgOpenColor=a.getColor(R.styleable.ToggleButton_bgOpenColor,0x0000ff); int bgCloseColor=a.getColor(R.styleable.ToggleButton_bgCloseColor,0xcccccc); int tgColor=a.getColor(R.styleable.ToggleButton_tgColor,0xffffff); bgOpenPaint=new Paint(); bgOpenPaint.setColor(bgOpenColor); bgClosePaint=new Paint(); bgClosePaint.setColor(bgCloseColor); tgPaint=new Paint(); tgPaint.setColor(tgColor); } public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { if (status) { canvas.drawCircle(height / 2, height / 2, height / 2, bgOpenPaint); canvas.drawRect(height / 2, 0, width - height, height, bgOpenPaint); canvas.drawCircle(width - height, height / 2, height / 2, bgOpenPaint); } else { canvas.drawCircle(height / 2, height / 2, height / 2, bgClosePaint); canvas.drawRect(height / 2, 0, width - height, height, bgClosePaint); canvas.drawCircle(width - height, height / 2, height / 2, bgClosePaint); } if(isMove){ float x; if(current<height/2){ x=height/2; } else if(current>width-height){ x=width-height; } else{ x=current; } canvas.drawCircle(x,height/2,height/2,tgPaint); } else{ if(status){ canvas.drawCircle(width - height, height / 2, height / 2, tgPaint); } else{ canvas.drawCircle(height/2, height / 2, height / 2, tgPaint); } } // canvas.drawCircle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); width=MeasureSpec.getSize(widthMeasureSpec); height=MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width,height); } /** * 根据手势重绘view * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { current=event.getX(); if(current>width/2) status=true; else status=false; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: isMove=true; break; case MotionEvent.ACTION_UP: isMove=false; break; case MotionEvent.ACTION_MOVE: break; } invalidate(); return true; }}
attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ToggleButton"> <attr name="bgOpenColor" format="color"/> <attr name="bgCloseColor" format="color"/> <attr name="tgColor" format="color"/> </declare-styleable></resources>
在上面的代码中只实现了基本的功能,更多功能可以根据需求自己添加
0 0
- 【Android学习笔记】自定义View
- Android自定义view学习笔记
- Android学习笔记-自定义view
- android 学习笔记(1) ExpandableListActivity 自定义view
- android学习笔记3:自定义view
- Android自定义view学习笔记01
- Android自定义view学习笔记02
- Android自定义View学习笔记03
- Android自定义View学习笔记04
- Android自定义view学习笔记01
- Android自定义view学习笔记02
- Android自定义View学习笔记03
- Android自定义View学习笔记04
- android学习笔记-自定义View的属性
- Android学习笔记-自定义view保存状态
- android自定义view学习笔记1
- 自定义view学习笔记
- android自定义view笔记
- 深入学习java并发编程:内存模型(三)顺序一致性
- Java - 计蒜客 - 矩阵翻转
- 【SQL Sever】 函数的创建
- 被砍项目之总结
- @property里assgin 、copy、strong的区别
- Android学习笔记-自定义view
- C++对象内存分布(包括字节对齐和虚函数表)
- 设计模式08_适配器模式
- 关于fflush(stdin)的使用与scanf输入缓冲区的问题
- Win8.1获取 开发者许可证
- Java - 计蒜客 - 泥塑课
- 设计模式之六大原则——单一职责原则(SRP)
- 出来混,迟早是要还的
- C#的方法