android 绘制自定义控件
2015-06-20 21:38
477 查看
一:画虚线
二:实现同心圆加小三角指向效果
同心圆
http://www.it165.net/pro/html/201409/22252.html
http://www.it165.net/pro/html/201406/15815.html
<com.lc.mycontrol.DashedLine android:id="@+id/dashedLine" android:layout_width="wrap_content" android:layout_marginLeft="50dp" android:layout_height="300dp" />
public class DashedLine extends View{ private final String namespace = "http://com.smartmap.driverbook"; private float startX; private float startY; private float endX; private float endY; private Rect mRect; Context contextgb; public DashedLine(Context context, AttributeSet attrs) { super(context, attrs); contextgb = context; } @Override protected void onDraw(Canvas canvas) { int[] location = new int[2] ; //this.getLocationInWindow(location); //获取在当前窗口内的绝对坐标 //System.out.println("InWindow x:"+location[0]+" y:"+location[1]); this.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标 System.out.println("OnScreen x:"+location[0]+" y:"+location[1]); // TODO Auto-generated method stub super.onDraw(canvas); Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.WHITE); Path path = new Path(); //location[0]获取到控件所在的x坐标,可以设置改控件位置 path.moveTo(5, Dp2Px(contextgb,10)); path.lineTo(5, Dp2Px(contextgb,56)); PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},2); paint.setPathEffect(effects); paint.setStrokeWidth((float) 1.5); canvas.drawPath(path, paint); } public float Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (float) (dp * scale + 0.5f); } }
二:实现同心圆加小三角指向效果
同心圆
public class RingView extends View{ private Paint paint; private Context context; private int radius;//半径 private int color;//颜色值 public RingView(Context context) { // TODO Auto-generated constructor stub this(context, null); } public RingView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); //消除锯齿 } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub int center = getWidth()/2; //绘制内圆 paint.setStyle(Paint.Style.FILL); paint.setColor(Color.parseColor("#91d439")); canvas.drawCircle(center,center, 22, this.paint); //绘制外圆(空心) paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.parseColor("#91d439")); paint.setStrokeWidth(6); canvas.drawCircle(center,center, 30, this.paint); super.onDraw(canvas); } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }小三角
public class mytriangle extends View{ private Paint paint; private Context context; private int radius;//半径 private int color;//颜色值 public mytriangle(Context context) { // TODO Auto-generated constructor stub this(context, null); } public mytriangle(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); //消除锯齿 } @Override protected void onDraw(Canvas canvas) { float x= getX();//得到控件的所在位置 float y= getX(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.parseColor("#b9dc8c")); // canvas.drawText("画三角形:", 10, 200, paint); // 绘制这个三角形,你可以绘制任意多边形 Path path = new Path(); // y=(float) (y+15.5); path.moveTo(Dp2Px(context,x+10),Dp2Px(context,y));// 此点为多边形的起点 path.lineTo(Dp2Px(context,x), Dp2Px(context,y+7)); path.lineTo(Dp2Px(context,x+10), Dp2Px(context,y+14)); path.close(); // 使这些点构成封闭的多边形 canvas.drawPath(path,paint); super.onDraw(canvas); } public float Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (float) (dp * scale + 0.5f); } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }
http://www.it165.net/pro/html/201409/22252.html
http://www.it165.net/pro/html/201406/15815.html
相关文章推荐
- Android View 无处不在的图片查看器~ 不服就看
- Android关于TextView 宽度过大导致Drawable无法居中问题
- android如何将一个字符串生成二维码
- Android两种常见错误(ANR和FC)解决办法
- android笔记--处理started service的多次启动请求
- 用Android手机spydroid-ipcamera搭载局域网监控环境
- Android异步消息处理机制——handle与Looper,AsyncTask
- Android学习路径(四)文件项目学习的名单,android显示单元经常使用的
- Android判断屏幕锁屏的步骤总结
- 03_Android项目中读写文本文件的代码
- Android应用实例-有道词典
- 03_Android项目中读写文本文件的代码
- Android数据库ORMlite框架翻译系列(第二章:part 3)
- Android数据库ORMlite框架翻译系列(第二章:part 2)
- Android数据库ORMlite框架翻译系列(第二章:part 1)
- Android数据库ORMlite框架翻译系列(第一章)
- 02_Android写xml文件和读xml文件
- 02_Android写xml文件和读xml文件
- Android 中的 Service 全面总结
- Android如何通过shareduserid获取系统权限