1.无意看到了一个指南针的UI,在这里简单的模仿了一下。其实就是第画布的一些变化而已。

别人的效果图是:

  

3.简单说一下思路:

  1)首先是画一个黑色圆盘

  2) 然后画圆盘上的刻度(就是对Canvas一些变换)

  3) 文字添加

4.直接上代码:

  

 public class CompassView extends View {
private Paint circlePaint, tickPaint;
private TextPaint textPaint;
// 指定控件宽和高,用于自适应
private float vWidth, vHeight;
// 圆盘的半径
private float compassRadiu;
// 刻度线段的长度
private float tickHeight;
// 字体高度和宽度
private float textHeight, textWidth;; public CompassView(Context context) {
super(context);
initPaint(context);
} public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint(context);
} private void initPaint(Context context) {
// 对画圆盘画初始化
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
circlePaint.setColor(Color.BLACK);
circlePaint.setStyle(Paint.Style.FILL); // 对刻度画笔进行初始化
tickPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tickPaint.setColor(Color.RED);
tickPaint.setStrokeWidth(3); // 对字的画笔进行初始化
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(20); } // 自适应在这里做的
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 获取控件的宽和高
vWidth = w;
vHeight = h;
compassRadiu = Math.min(w, h) / 2;
tickHeight = (1 / 12F) * compassRadiu;
textHeight = textPaint.descent() - textPaint.ascent(); } @Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
// 黑色圆盘
canvas.drawCircle(compassRadiu, compassRadiu, compassRadiu, circlePaint);
// 画红色的刻度
int degress;
float textWidth; for (int i = 0; i < 24; i++) {
canvas.save();
canvas.translate(compassRadiu, compassRadiu);
// 当前canvas旋转角度
degress = i * 15;
canvas.rotate(15 * i); canvas.drawLine(0, -compassRadiu, 0, -compassRadiu + tickHeight,
tickPaint);
switch (degress) {
case 0:
textWidth = textPaint.measureText("45");
drawText(canvas, "45", textWidth);
break; case 45:
textWidth = textPaint.measureText("东");
drawText(canvas, "东", textWidth);
break;
case 90:
textWidth = textPaint.measureText("135");
drawText(canvas, "135", textWidth);
break;
case 135:
textWidth = textPaint.measureText("南");
drawText(canvas, "南", textWidth);
break;
case 180:
textWidth = textPaint.measureText("225");
drawText(canvas, "225", textWidth);
break;
case 225:
textWidth = textPaint.measureText("西");
drawText(canvas, "西", textWidth);
break;
case 270:
textWidth = textPaint.measureText("315");
drawText(canvas, "315", textWidth);
break;
case 315:
textWidth = textPaint.measureText("北");
drawText(canvas, "北", textWidth);
canvas.drawLine(0,
-compassRadiu + tickHeight + textHeight + 10,
-textWidth / 3, -compassRadiu + tickHeight + textHeight
+ 30, tickPaint);
canvas.drawLine(0,
-compassRadiu + tickHeight + textHeight + 10,
textWidth / 3, -compassRadiu + tickHeight + textHeight
+ 30, tickPaint); break;
default:
break;
}
canvas.restore();
} } private void drawText(Canvas canvas, String text, float textWidth) { canvas.drawText(text, -(textWidth / 2), -compassRadiu + tickHeight
+ textHeight, textPaint); }
}

运行后的效果图是:

  

源码下载

最新文章

  1. ios基础之 view的frame 与 bounds 的区别 (转)
  2. WIN10 新建ORACLE实例
  3. C++用PostMessage模拟按钮点击
  4. 基于nodejs实现js后端化处理
  5. Java算法-符号&amp;
  6. PHP生成条形码
  7. 浏览器打开应用指定的界面-b
  8. [D3] 2. Basics of SVG
  9. python使用pyapns进行ios推送消息
  10. 开启cocos2dx 3.0的Console功能
  11. 中国澳门sinox很多平台CAD制图、PCB电路板、IC我知道了、HDL硬件描述语言叙述、电路仿真和设计软件,元素分析表
  12. plus调用android原生页面
  13. C#数组--(Array类的属性和方法)
  14. java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒)
  15. 谈一款MOBA类游戏《码神联盟》的服务端架构设计与实现(更新优化思路)
  16. 洛谷P2084 进制转换
  17. Kotlin(一)
  18. sharding-jdbc读写分离原理解读
  19. 【java多线程】线程状态分析
  20. 【Unity】1.2 HelloWorld--测试桌面和Android游戏能否正常运行

热门文章

  1. 指向“**js/shop.js”的 &lt;script&gt; 加载失败
  2. python中的socket模块
  3. python拷贝目录下的文件
  4. deep learning and machine learning
  5. avalon1.3的新特性预览
  6. bootstrap 的页码显示问题-------------德州
  7. sqlserver,oracle,mysql等的driver驱动,url怎么写
  8. 基于NodeJS的14款Web框架
  9. 使用火狐浏览器访问双向认证的k8s api
  10. json和jsonp的区别(转)