19、android 自定义虚线
来源:互联网 发布:java一定要去培训班吗 编辑:程序博客网 时间:2024/05/17 02:48
一、介绍
android 中经常使用到虚线,横着的虚线直接使用 shape实现。但是要想使用shape实现竖着的虚线却不是那么方便。于是想到使用View自定义一个虚线 。
横着的虚线很好实现,直接上代码 shape_h_dottedline.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:dashGap="6px" android:dashWidth="6px" android:color="#C7B299" /></shape>
照例使用shape 写了竖虚线 shape_votec_dottedline.xml
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="90" android:toDegrees="90"> <shape android:shape="line"> <stroke android:width="1dp" android:color="#cf152b" android:dashGap="10px" android:dashWidth="10px" /> </shape></rotate>
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:gravity="center_horizontal" android:layout_height="match_parent"> <RelativeLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="100dp"> <com.cloud.wd.custom.widget.JDottedLine android:layout_marginTop="10dp" android:layout_width="20dp" android:layout_height="160dp" /> </RelativeLayout> <ImageView android:layerType="software" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/shape_votec_dottedline" android:layout_width="20dp" android:layout_height="160dp"/> <ImageView android:layerType="software" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/shape_h_dottedline" android:layout_width="100dp" android:layout_height="160dp"/></LinearLayout>
可是呢?
竖着的线却是不能显示完整的
无奈搜索了好久,最终直接未果!于是想到了 自己使用path 画一个虚线;
实现代码很简单:
/** * Created by fmm on 2017/7/11. */public class JDottedLine extends View { private Paint mDotPaint; public JDottedLine(Context context) { super(context); initView(); } public JDottedLine(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(); } public JDottedLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public JDottedLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initView(); } private void initView() { mDotPaint = new Paint(); mDotPaint.setColor(Color.GRAY); //画笔颜色 mDotPaint.setStrokeWidth(1); //画笔宽度 // 1、STROKE 描边 // 2、FILL_AND_STROKE 填充内部和描边 // 3、FILL:填充内部 mDotPaint.setStyle(Paint.Style.STROKE); //1、Cap.BUTT 这条路径结束,而不是超越它。 //2、Cap.ROUND 结束是个半圆 //3、Cap.SQUARE 结束是个方形 mDotPaint.setStrokeCap(Paint.Cap.ROUND);// //设置抗锯齿 mDotPaint.setAntiAlias(true); //设置是否抖动 mDotPaint.setDither(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float startY = getHeight(); float startX = getWidth() / 2; DashPathEffect dashPathEffect = new DashPathEffect(new float[]{8, 10, 8, 10}, 0); mDotPaint.setPathEffect(dashPathEffect); Path path = new Path(); path.moveTo(startX,0); path.lineTo(startX,startY); canvas.drawPath(path,mDotPaint); }}
使用方式 和布局文件中的一样。
无意中发现一个良心的博主:
http://www.gcssloop.com/customview/CustomViewIndex/
阅读全文
0 0
- 19、android 自定义虚线
- android中自定义虚线
- Android 自定义虚线View
- Android实现自定义虚线
- android虚线控件---自定义控件
- android自定义水平虚线View
- 【Android自定义控件】Android自定义虚线<三>
- Android自定义画虚线的控件
- android自定义View---生成虚线的View
- android自定义View---生成虚线的View
- Android 自定义虚线段长和点宽
- Android自定义控件之虚线的用法
- 自定义虚线
- android虚线
- Android虚线
- android虚线
- Android 虚线
- Android 绘制虚线<XML><自定义view中onDraw绘制>
- @Version
- MongoDB常用命令汇总之语句块操作。
- Python 中文排序
- centos 安装VSCODE 的经历
- 设计模式(二十二)command
- 19、android 自定义虚线
- Python学习整理之输入输出input(),print()
- react前端+node后台+mongoose+express框架搭建
- Win10下安装卸载Ubuntu16.04双系统的方法
- cnn补充材料
- fgm实例练习笔记-2.11鼠标移过改变图片路径
- 初识Git(一)
- Android 屏幕适配:最全面的解决方案
- 欢迎使用CSDN-markdown编辑器