自定义控件之单点触摸监听
来源:互联网 发布:It 门槛 编辑:程序博客网 时间:2024/06/16 13:46
配置XML
<!--styleable--><resources> <declare-styleable name="MyView"> <attr name="Colorouter" format="color|reference"> </attr> <attr name="Colowithin" format="color|reference"> </attr> </declare-styleable></resources>
//自定义属性
TypedArray type=context.obtainStyledAttributes(attrs,R.styleable.MyView);colorouter = type.getColor(R.styleable.MyView_Colorouter, Color.WHITE);colowithin = type.getColor(R.styleable.MyView_Colowithin, Color.WHITE);
@Overrideprotected void onDraw(Canvas canvas) {//重写绘制 super.onDraw(canvas); this.setBackgroundResource(R.color.colorluse); Paint pa=new Paint(); measuredWidth = getMeasuredWidth();//获取当前大小 measuredHeight = getMeasuredHeight(); pa.setColor(getResources().getColor(R.color.colorhuangse)); pa.setStrokeWidth(20); pa.setStyle(Paint.Style.FILL); width = (measuredWidth / 2); height = (measuredHeight / 2); huangse = width; canvas.drawCircle(width, height, huangse,pa); pa.setColor(getResources().getColor(R.color.colorbaise));//设置颜色 baiseewidth = width / 2; canvas.drawCircle(width, height, baiseewidth,pa); Rect rect = new Rect(); String str="圆环";//设置内容 pa.setTextSize(20); pa.setColor(Color.BLACK); pa.getTextBounds(str,0,str.length(),rect); canvas.drawText(str, width -rect.width()/2, height +rect.height()/2,pa);}@Overridepublic boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action){ case MotionEvent.ACTION_DOWN://触摸 float pointX = event.getX(); float pointY = event.getY(); double range = Math.sqrt(( width - pointX) * (width - pointX) + ( height- pointY) * (height - pointY)); text = ""; if (range <baiseewidth) { text = "在小圆内"; GetText.Get(text);//接口回调返回 } else if (range >baiseewidth && range <huangse ) { text = "在圆环内"; GetText.Get(text); } else if (range > huangse) { text = "在圆环外"; GetText.Get(text); } } return true;}
0 0
- 自定义控件之单点触摸监听
- 自定义控件之触摸事件的传递
- Android 自定义UI-单点触摸-多点触摸
- android开发之单点触摸
- cocos2d-js之单点触摸
- Android之自定义AlertDialog无法监听控件
- Android自定义控件之触摸事件分发机制
- 18.自定义UI(画布)-单点触摸-多点触摸
- 简单自定义View及触摸监听
- 单点触摸
- Android游戏开发之单点触摸与多点触摸
- Cocos2d触摸:单点触摸
- 自定义控件监听网络改变
- 寒風的Cocos2dx之旅之单点触摸
- Android-单点触摸-多点触摸
- Cocos2d-x游戏开发之处理单点触摸事件
- cocos2d-x学习之给CCLayer设置单点触摸
- cocos2d-x 3.x学习之TouchOneByOne单点触摸
- 杨辉三角形
- 与ViewPager合用的MyTitleLayout,可以设置滚动指示器
- Android实现一个简单的自定义适配器
- Linux查看文件以及磁盘空间大小管理
- [ASIFT 0] ASIFT算法深入讲解
- 自定义控件之单点触摸监听
- CPU卡程序设计实例(二十一)4字节随机数读取
- ===============个人博客导航栏======================
- 抽象与计算机课程
- 显卡驱动
- C++异常处理机制
- 5.定时器Timer
- hdu5691_Sitting in Line_状压dp
- [SSRS / RV](.rdlc报表)Reporting Services多印空白页的解決方式