凌雪
2018-11-29
来源 :网络
阅读 1209
评论 0
摘要:本文将带你了解Android应用开发之Android自定义view的方式绘制虚线,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发之Android自定义view的方式绘制虚线,希望本文对大家学Android有所帮助。
Android自定义view绘制虚线
最近项目中有个需求,通过自定义view的方式绘制虚线
别的不多说先看一眼效果
这个需求在我们的开发中应该是一个很常见的需求了吧,有人会说有更简单的实现方式,对,但是你试过么,比如网上提到的这种方式
代码如下
这里写图片描述
看样子好像已经成功了,最后在把这个作为background加进去,
结果在真机上跑一圈,发现显示的竟然是一条实线,在好几台手机上试都是这样,都是一条实线,后来查资料发现需要加上一句话
android:layerType=”software” . android:shape="line"完美解决
画水平虚线
1.直接建一个shape,设置stroke属性就行了,再将这个属性直接作为background的drawable属性引入就行了
注意在4.0以上的真机加一句
<code><code> <!--?xml version="1.0" encoding="utf-8"?--> <shape android:shape="line" xmlns:android="https://schemas.android.com/apk/res/android"> <solid android:color="@color/red"></solid> <stroke android:color="@color/app_color" android:dashgap="10dp" android:dashwidth="10dp" android:width="4dp"> </stroke></shape></code></code>
2.布局xml文件中直接引用
<code><code><code> <view android:background="@drawable/dottde_line" android:id="@+id/content" android:layertype="software" android:layout_gravity="center_vertical" android:layout_height="1dp" android:layout_width="match_parent"> //4.0以上的加,不然真机中是实线</view></code></code></code>
我的手机是华为 p9 系统7.0 最终发现问题没有效果,后来调整了 android:layout_height="1dp" 才发现,这样绘制出来的横向直线确实是成功了,但是是包裹在view四周的,这种先天性的缺陷导致了他无法在高度特别低的控件中使用
添加这句话就可以解决 android:shape="line"
最终的效果:
其实我们可以通过自定义一个view的形式来实现这个效果
1.自定义一个java类继承view
<code><code><code><code><code><code> /** * Created by benchengzhou on 2018/3/16 15:47 . * 作者邮箱: mappstore@163.com * 功能描述: 一个横向的虚线view * 类 名: HorizontalPartLineView * 备 注: 网络参考链接: https://blog.csdn.net/cxmscb/article/details/51760528 * https://blog.csdn.net/xmxkf/article/details/51468648 * https://www.cnblogs.com/prophet-it/p/6651033.html */ public class HorizontalPartLineView extends View { private int DEFAULT_SOLID_WIDTH = 1; private int DEFAULT_HOLLOW_WIDTH = 1; private int DEFAULT_BG_COLOR = 0xffffffff; private int DEFAULT_SOLID_COLOR = 0xffdcdcdc; private int SolidW; private int hollowW; private int SolidColor; private int bgColor; private int offset=0; private Paint mPaint; private int DEFAULT_LINE_HEIGHT = 30; private int mWidth; private int mHeight; public HorizontalPartLineView(Context context) { this(context, null); } public HorizontalPartLineView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public HorizontalPartLineView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Horizontal_PartLine_View, defStyleAttr, 0); //边框线宽 SolidW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_solid_width, DEFAULT_SOLID_WIDTH); hollowW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_hollow_width, DEFAULT_HOLLOW_WIDTH); SolidColor = a.getColor(R.styleable.Horizontal_PartLine_View_solid_color, DEFAULT_SOLID_COLOR); bgColor = a.getColor(R.styleable.Horizontal_PartLine_View_bg_color, DEFAULT_BG_COLOR); offset = a.getColor(R.styleable.Horizontal_PartLine_View_offset, 0); if (SolidW % 2 != 0) { SolidW++; } if (hollowW % 2 != 0) { hollowW++; } if (SolidW == 0) { new Throwable(new IllegalArgumentException("the value SolidW san not be zone !")); } if (hollowW == 0) { new Throwable(new IllegalArgumentException("the value hollowW san not be zone !")); } a.recycle(); //使用完成之后记得回收 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); // int widthMode = MeasureSpec.getMode(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); // int heightMode = MeasureSpec.getMode(heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(bgColor); canvas.drawRect(0, 0, mWidth, mHeight, paint); mPaint = new Paint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(SolidColor); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(/*dip2px(ctx,2)*/mHeight); Path path = new Path(); path.moveTo(0, 0); path.lineTo(mWidth, 0); PathEffect effects = new DashPathEffect(new float[]{SolidW, hollowW, SolidW, hollowW}, offset); mPaint.setPathEffect(effects); canvas.drawPath(path, mPaint); } }</code></code></code></code></code></code>
2.创建自定义属性文件 attrs-line.xml
<code><code><code><code><code><code><code><!--?xml version="1.0" encoding="utf-8"?--><resources> <!--圆角图片imageview--> <declare-styleable name="Horizontal_PartLine_View"> <!--实线属性--> <!--虚线属性,注意虚线是透明没有颜色的--> <!--控件的底色--> </attr></attr></attr></attr></attr></declare-styleable> </resources></code></code></code></code></code></code></code>
3.布局文件中使用
<code><code><code><code><code><code><code><code> <com.ztsc.house.customview.partline.horizontalpartlineview android:layout_height="3px" android:layout_width="match_parent" app:bg_color="@color/color_bg_ffffff" app:hollow_width="10px" app:solid_color="@color/color_hint_dcdcdc" app:solid_width="10px"></com.ztsc.house.customview.partline.horizontalpartlineview></code></code></code></code></code></code></code></code>
4.定义引用控件
<code><code><code><code><code><code><code><code><code> xmlns:app="https://schemas.android.com/apk/res-auto"</code></code></code></code></code></code></code></code></code>
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号