继承view的自定义控件示例
来源:互联网 发布:淘宝账户怎么贷款额度 编辑:程序博客网 时间:2024/06/10 11:50
首先说一下自定义控件可以分为继承view的用canvas画出来的控件,还有继承Android原生控件来重写出来的控件,还有就是混合控件。今天我来做的是第一种情况,即继承view用canvas画出来的控件,下面我们来实现下图的一个需求:
实现思路:先画一条弧线,在画2个圆形,组成一个圆环,其次给圆环添加点击事件。
先初始化画笔paint的设置,代码如下:
rectF = new RectF(10, 200, screenWidth - 10, screenWidth - 10);paint_Arc = new Paint(Paint.ANTI_ALIAS_FLAG);// 设置抗锯齿paint_Arc.setAntiAlias(true);// 设置画笔宽度paint_Arc.setStrokeWidth(3);// 设置画笔颜色paint_Arc.setColor(Color.BLUE);// 设置空心paint_Arc.setStyle(Paint.Style.STROKE);// 设置模式paint_Arc.setXfermode(new PorterDuffXfermode(Mode.DARKEN));其中这里有一个知识点就是那个设置模式。大家可以戳这里
这里就是图层模式相应的效果图。
再来在onDraw弧线和圆环:代码如下:
canvas.drawArc(rectF, 180, 180, false, paint_Arc);paint_Arc.setStyle(Paint.Style.FILL);canvas.drawCircle(screenWidth / 2, 200, 100, paint_Arc);canvas.drawCircle(screenWidth / 6, 300, 100, paint_Arc);canvas.drawCircle(screenWidth * 5 / 6, 300, 100, paint_Arc);paint_Arc.setColor(Color.WHITE);canvas.drawCircle(screenWidth / 2, 200, 90, paint_Arc);canvas.drawCircle(screenWidth / 6, 300, 90, paint_Arc);canvas.drawCircle(screenWidth * 5 / 6, 300, 90, paint_Arc);
最后就是计算点击的坐标是否在圆环中,代码如下:
@SuppressLint("ClickableViewAccessibility")@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:int x = (int) event.getX();int y = (int) event.getY();if (circleListener != null) {if (Math.abs(x - screenWidth / 6) <= 100&& Math.abs(y - 300) <= 100&& (screenWidth / 6 - x) * (screenWidth / 6 - x)+ (300 - y) * (300 - y) <= 100 * 100) {circleListener.onCircleListener(1);return true;}if (Math.abs(x - screenWidth / 2) <= 100&& Math.abs(y - 200) <= 100&& (screenWidth / 2 - x) * (screenWidth / 2 - x)+ (200 - y) * (200 - y) <= 100 * 100) {circleListener.onCircleListener(2);return true;}if (Math.abs(x - screenWidth * 5 / 6) <= 100&& Math.abs(y - 300) <= 100&& (screenWidth * 5 / 6 - x)* (screenWidth * 5 / 6 - x) + (300 - y)* (300 - y) <= 100 * 100) {circleListener.onCircleListener(3);return true;}}break;default:break;}return super.onTouchEvent(event);}public void setOnCircleListener(CircleListener circleListener){this.circleListener = circleListener;}public interface CircleListener{public abstract void onCircleListener(int circlePos);}
0 0
- 继承view的自定义控件示例
- 继承View自定义控件
- 自定义控件-继承View
- 自定义View <0> 继承现有的控件
- 自定义控件之 继承 View
- 继承View自定义开关控件
- 自定义view之继承控件
- 自定义控件继承View------Music
- android继承View实现复杂的自定义控件(1)
- android自定义控件(二) 入门,继承View
- android自定义控件(二) 入门,继承View
- Android-自定义控件之继承View
- android自定义控件(二) 入门,继承View
- android自定义控件(二) 入门,继承View
- Android 如何自定义控件(继承View)
- Android 自定义控件之继承view
- 继承LinearLayout的自定义view
- Android自定义控件View(二)继承控件
- 工具学习——webpack 项目打包
- 使用MySQL如何有效地创建基于 INNODB 引擎的表
- Android Studio五分钟带你从菜鸟到高级调试
- IDEA Intellij小技巧和插件
- 深入浅出JMS(一)--JMS基本概念
- 继承view的自定义控件示例
- EditText基础属性
- Spark SQL and DataFrames
- 如何使用iOS 开发证书 和 Profile 文件
- 质数因子
- Xamarin.ios引用第三方SDK
- 常用sql注入语句
- ButterKnife在Android 中的使用
- log4j2 详细配置