android 绘制自定义控件

一:Android 滚动条常见问题

1:子元素不能多余一个

2:不显示底部或者顶部的阴影

android:fadingEdge="none"


二:后台设置颜色

Color.parseColor("#91d439")


三:dp,px之间的转化

[java]  view plain  copy
  1. public float Dp2Px(Context context, float dp) {   
  2.  final float scale = context.getResources().getDisplayMetrics().density;   
  3.  return (float) (dp * scale + 0.5f);   
  4. }  


四:弹出对话框

[java]  view plain  copy
  1. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
  2.                     //builder.setIcon(R.drawable.ic_launcher);  
  3.                     builder.setTitle("选择船舶");  
  4.                     //    指定下拉列表的显示数据  
  5.                     final String[] cities = {"广州""上海""北京""香港""澳门"};  
  6.                     //    设置一个下拉的列表选择项  
  7.                     builder.setItems(cities, new DialogInterface.OnClickListener()  
  8.                     {  
  9.                         @Override  
  10.                         public void onClick(DialogInterface dialog, int which)  
  11.                         {  
  12.                             tv.setText("选择船舶:"+cities[which]);  
  13.                             System.out.println("选择的城市为:" + cities[which]);  
  14.                         }  
  15.                     });  
  16.                     builder.show();  


五: android application与activity上边都可以设置Label

        可以在图标与不同页面显示Label



六:Activity动画切换

       http://my.oschina.NET/u/556624/blog/102552

      http://my.oschina.net/u/556624/blog/102552


七:Android实现再按一次退出程序

       

[java]  view plain  copy
  1. private long exitTime = 0;  
  2. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  3.     if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){     
  4.         if((System.currentTimeMillis()-exitTime) > 2000){    
  5.             Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                  
  6.             exitTime = System.currentTimeMillis();     
  7.         } else {  
  8.             finish();  
  9.             System.exit(0);  
  10.         }  
  11.         return true;     
  12.     }  
  13.     return super.onKeyDown(keyCode, event);  
  14. }  



八:Android实现跨越多个activity跳转

       例如在设置页面实现退出登录,如果直接finish()回退出到主界面,无法跳转到登录页
       

[java]  view plain  copy
  1. Intent intent = new Intent();    
  2.                 //跨越多个activity,删除回退栈的所有activity  
  3.                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  4.                   
  5.                 intent.setClass(SetingActivity.this, LoginActivity.class);  
  6.                 startActivity(intent);        



一:实现同心圆加小三角指向效果


同心圆

[java]  view plain  copy
  1. public class RingView extends View{  
  2.       
  3.     private Paint paint;  
  4.     private Context context;  
  5.     private int radius;//半径  
  6.     private int color;//颜色值  
  7.       
  8.     public RingView(Context context) {  
  9.           
  10.         // TODO Auto-generated constructor stub  
  11.         this(context, null);  
  12.     }  
  13.   
  14.     public RingView(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         // TODO Auto-generated constructor stub  
  17.         this.context = context;  
  18.         this.paint = new Paint();  
  19.         this.paint.setAntiAlias(true); //消除锯齿  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onDraw(Canvas canvas) {  
  24.         // TODO Auto-generated method stub  
  25.         int center = getWidth()/2;  
  26.           
  27.         //绘制内圆  
  28.         paint.setStyle(Paint.Style.FILL);  
  29.         paint.setColor(Color.parseColor("#91d439"));  
  30.         canvas.drawCircle(center,center, 22this.paint);  
  31.           
  32.         //绘制外圆(空心)  
  33.         paint.setStyle(Paint.Style.STROKE);  
  34.         paint.setColor(Color.parseColor("#91d439"));  
  35.         paint.setStrokeWidth(6);    
  36.         canvas.drawCircle(center,center, 30this.paint);  
  37.         super.onDraw(canvas);  
  38.     }  
  39.   
  40.     public int getRadius() {  
  41.         return radius;  
  42.     }  
  43.   
  44.     public void setRadius(int radius) {  
  45.         this.radius = radius;  
  46.     }  
  47.   
  48.     public int getColor() {  
  49.         return color;  
  50.     }  
  51.   
  52.     public void setColor(int color) {  
  53.         this.color = color;  
  54.     }     
  55. }  


这里的getWidth可以根据控件设置的宽度来得到


小三角

[java]  view plain  copy
  1. public class mytriangle extends View{  
  2.       
  3.     private Paint paint;  
  4.     private Context context;  
  5.     private int radius;//半径  
  6.     private int color;//颜色值  
  7.       
  8.     public mytriangle(Context context) {  
  9.           
  10.         // TODO Auto-generated constructor stub  
  11.         this(context, null);  
  12.     }  
  13.   
  14.     public mytriangle(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         // TODO Auto-generated constructor stub  
  17.         this.context = context;  
  18.         this.paint = new Paint();  
  19.         this.paint.setAntiAlias(true); //消除锯齿  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onDraw(Canvas canvas) {       
  24.               
  25.         float x= getX();//得到控件的所在位置  
  26.         float y= getX();  
  27.           
  28.         paint.setStyle(Paint.Style.FILL);  
  29.         paint.setColor(Color.parseColor("#b9dc8c"));  
  30.        // canvas.drawText("画三角形:", 10, 200, paint);    
  31.         // 绘制这个三角形,你可以绘制任意多边形    
  32.         Path path = new Path();    
  33.        // y=(float) (y+15.5);  
  34.         path.moveTo(Dp2Px(context,x+10),Dp2Px(context,y));// 此点为多边形的起点    
  35.         path.lineTo(Dp2Px(context,x), Dp2Px(context,y+7));    
  36.         path.lineTo(Dp2Px(context,x+10), Dp2Px(context,y+14));    
  37.         path.close(); // 使这些点构成封闭的多边形    
  38.         canvas.drawPath(path,paint);         
  39.         super.onDraw(canvas);  
  40.     }  
  41.       
  42.     public float Dp2Px(Context context, float dp) {   
  43.         final float scale = context.getResources().getDisplayMetrics().density;   
  44.         return (float) (dp * scale + 0.5f);   
  45.     }  
  46.   
  47.     public int getRadius() {  
  48.         return radius;  
  49.     }  
  50.   
  51.     public void setRadius(int radius) {  
  52.         this.radius = radius;  
  53.     }  
  54.   
  55.     public int getColor() {  
  56.         return color;  
  57.     }  
  58.   
  59.     public void setColor(int color) {  
  60.         this.color = color;  
  61.     }     
  62. }  

布局文件中使用用全类名访问就可以了,还可以设置自动义属性,

getx(),gety得到控件的所在的坐标位置

 int[] location = new  int[2] ;
        getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标包括通知栏  
        System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);  


注意:不要以为获取到了这个位置设置下就可以让控件在具体的位置了,而是所在布局文件类型以,0,0开始,所有

不要去设置位置默认就在那个位置,设置了反而让控件不在本身正确的位置了


[html]  view plain  copy
  1. <com.lc.mycontrol.RingView   
  2.              android:layout_width="100dp"  
  3.              android:layout_height="100dp"  
  4.             />  




http://www.it165.net/pro/html/201409/22252.html

http://www.it165.net/pro/html/201406/15815.html


二:画虚线



[html]  view plain  copy
  1. <com.lc.mycontrol.DashedLine   
  2.      android:id="@+id/dashedLine"  
  3.      android:layout_width="wrap_content"   
  4.      android:layout_marginLeft="50dp"          
  5.      android:layout_height="300dp"                                     
  6.       />  


[html]  view plain  copy
  1. public class DashedLine extends View{  
  2.     private final String namespace = "http://com.smartmap.driverbook";  
  3.     private float startX;  
  4.     private float startY;  
  5.     private float endX;  
  6.     private float endY;  
  7.     private Rect mRect;  
  8.       
  9.     Context contextgb;  
  10.     public DashedLine(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);   
  12.         contextgb = context;  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onDraw(Canvas canvas) {  
  17.           
  18.         int[] location = new  int[2] ;  
  19.         //this.getLocationInWindow(location); //获取在当前窗口内的绝对坐标          
  20.         //System.out.println("InWindow x:"+location[0]+" y:"+location[1]);    
  21.         this.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标  
  22.         System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);   
  23.           
  24.         // TODO Auto-generated method stub  
  25.         super.onDraw(canvas);          
  26.         Paint paint = new Paint();  
  27.         paint.setStyle(Paint.Style.STROKE);  
  28.         paint.setColor(Color.WHITE);  
  29.         Path path = new Path();          
  30.         //location[0]获取到控件所在的x坐标,可以设置改控件位置  
  31.         path.moveTo(5, Dp2Px(contextgb,10));  
  32.         path.lineTo(5, Dp2Px(contextgb,56));        
  33.         PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},2);  
  34.         paint.setPathEffect(effects);  
  35.         paint.setStrokeWidth((float) 1.5);  
  36.         canvas.drawPath(path, paint);  
  37.     }  
  38.       
  39.     public float Dp2Px(Context context, float dp) {   
  40.         final float scale = context.getResources().getDisplayMetrics().density;   
  41.         return (float) (dp * scale + 0.5f);   
  42.     }  
  43. }  

一:实现同心圆加小三角指向效果


同心圆

[java]  view plain  copy
  1. public class RingView extends View{  
  2.       
  3.     private Paint paint;  
  4.     private Context context;  
  5.     private int radius;//半径  
  6.     private int color;//颜色值  
  7.       
  8.     public RingView(Context context) {  
  9.           
  10.         // TODO Auto-generated constructor stub  
  11.         this(context, null);  
  12.     }  
  13.   
  14.     public RingView(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         // TODO Auto-generated constructor stub  
  17.         this.context = context;  
  18.         this.paint = new Paint();  
  19.         this.paint.setAntiAlias(true); //消除锯齿  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onDraw(Canvas canvas) {  
  24.         // TODO Auto-generated method stub  
  25.         int center = getWidth()/2;  
  26.           
  27.         //绘制内圆  
  28.         paint.setStyle(Paint.Style.FILL);  
  29.         paint.setColor(Color.parseColor("#91d439"));  
  30.         canvas.drawCircle(center,center, 22this.paint);  
  31.           
  32.         //绘制外圆(空心)  
  33.         paint.setStyle(Paint.Style.STROKE);  
  34.         paint.setColor(Color.parseColor("#91d439"));  
  35.         paint.setStrokeWidth(6);    
  36.         canvas.drawCircle(center,center, 30this.paint);  
  37.         super.onDraw(canvas);  
  38.     }  
  39.   
  40.     public int getRadius() {  
  41.         return radius;  
  42.     }  
  43.   
  44.     public void setRadius(int radius) {  
  45.         this.radius = radius;  
  46.     }  
  47.   
  48.     public int getColor() {  
  49.         return color;  
  50.     }  
  51.   
  52.     public void setColor(int color) {  
  53.         this.color = color;  
  54.     }     
  55. }  


这里的getWidth可以根据控件设置的宽度来得到


小三角

[java]  view plain  copy
  1. public class mytriangle extends View{  
  2.       
  3.     private Paint paint;  
  4.     private Context context;  
  5.     private int radius;//半径  
  6.     private int color;//颜色值  
  7.       
  8.     public mytriangle(Context context) {  
  9.           
  10.         // TODO Auto-generated constructor stub  
  11.         this(context, null);  
  12.     }  
  13.   
  14.     public mytriangle(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         // TODO Auto-generated constructor stub  
  17.         this.context = context;  
  18.         this.paint = new Paint();  
  19.         this.paint.setAntiAlias(true); //消除锯齿  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onDraw(Canvas canvas) {       
  24.               
  25.         float x= getX();//得到控件的所在位置  
  26.         float y= getX();  
  27.           
  28.         paint.setStyle(Paint.Style.FILL);  
  29.         paint.setColor(Color.parseColor("#b9dc8c"));  
  30.        // canvas.drawText("画三角形:", 10, 200, paint);    
  31.         // 绘制这个三角形,你可以绘制任意多边形    
  32.         Path path = new Path();    
  33.        // y=(float) (y+15.5);  
  34.         path.moveTo(Dp2Px(context,x+10),Dp2Px(context,y));// 此点为多边形的起点    
  35.         path.lineTo(Dp2Px(context,x), Dp2Px(context,y+7));    
  36.         path.lineTo(Dp2Px(context,x+10), Dp2Px(context,y+14));    
  37.         path.close(); // 使这些点构成封闭的多边形    
  38.         canvas.drawPath(path,paint);         
  39.         super.onDraw(canvas);  
  40.     }  
  41.       
  42.     public float Dp2Px(Context context, float dp) {   
  43.         final float scale = context.getResources().getDisplayMetrics().density;   
  44.         return (float) (dp * scale + 0.5f);   
  45.     }  
  46.   
  47.     public int getRadius() {  
  48.         return radius;  
  49.     }  
  50.   
  51.     public void setRadius(int radius) {  
  52.         this.radius = radius;  
  53.     }  
  54.   
  55.     public int getColor() {  
  56.         return color;  
  57.     }  
  58.   
  59.     public void setColor(int color) {  
  60.         this.color = color;  
  61.     }     
  62. }  

布局文件中使用用全类名访问就可以了,还可以设置自动义属性,

getx(),gety得到控件的所在的坐标位置

 int[] location = new  int[2] ;
        getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标包括通知栏  
        System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);  


注意:不要以为获取到了这个位置设置下就可以让控件在具体的位置了,而是所在布局文件类型以,0,0开始,所有

不要去设置位置默认就在那个位置,设置了反而让控件不在本身正确的位置了


[html]  view plain  copy
  1. <com.lc.mycontrol.RingView   
  2.              android:layout_width="100dp"  
  3.              android:layout_height="100dp"  
  4.             />  




http://www.it165.net/pro/html/201409/22252.html

http://www.it165.net/pro/html/201406/15815.html


二:画虚线



[html]  view plain  copy
  1. <com.lc.mycontrol.DashedLine   
  2.      android:id="@+id/dashedLine"  
  3.      android:layout_width="wrap_content"   
  4.      android:layout_marginLeft="50dp"          
  5.      android:layout_height="300dp"                                     
  6.       />  


[html]  view plain  copy
  1. public class DashedLine extends View{  
  2.     private final String namespace = "http://com.smartmap.driverbook";  
  3.     private float startX;  
  4.     private float startY;  
  5.     private float endX;  
  6.     private float endY;  
  7.     private Rect mRect;  
  8.       
  9.     Context contextgb;  
  10.     public DashedLine(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);   
  12.         contextgb = context;  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onDraw(Canvas canvas) {  
  17.           
  18.         int[] location = new  int[2] ;  
  19.         //this.getLocationInWindow(location); //获取在当前窗口内的绝对坐标          
  20.         //System.out.println("InWindow x:"+location[0]+" y:"+location[1]);    
  21.         this.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标  
  22.         System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);   
  23.           
  24.         // TODO Auto-generated method stub  
  25.         super.onDraw(canvas);          
  26.         Paint paint = new Paint();  
  27.         paint.setStyle(Paint.Style.STROKE);  
  28.         paint.setColor(Color.WHITE);  
  29.         Path path = new Path();          
  30.         //location[0]获取到控件所在的x坐标,可以设置改控件位置  
  31.         path.moveTo(5, Dp2Px(contextgb,10));  
  32.         path.lineTo(5, Dp2Px(contextgb,56));        
  33.         PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},2);  
  34.         paint.setPathEffect(effects);  
  35.         paint.setStrokeWidth((float) 1.5);  
  36.         canvas.drawPath(path, paint);  
  37.     }  
  38.       
  39.     public float Dp2Px(Context context, float dp) {   
  40.         final float scale = context.getResources().getDisplayMetrics().density;   
  41.         return (float) (dp * scale + 0.5f);   
  42.     }  
  43. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值