Android 自定义虚线View
发布日期:2021-07-30 03:26:04 浏览次数:4 分类:技术文章

本文共 3540 字,大约阅读时间需要 11 分钟。

虚线的需求,总是觉得要是用图片没得那么看着舒心,适配性也不好。定义一个虚线DashedLineView继承自View。

效果图:
这里写图片描述

自定义第一步是永远都不变的自定义属性,在attrs.xml中:

然后就是在view的构造方法中来获取:

TypedArray typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.dashedline);        lineColor = typedArray.getColor(R.styleable.dashedline_lineColor, Color.BLACK);        lineLength =(int) typedArray.getDimension(R.styleable.dashedline_lineLength,10);        lineSpace =(int) typedArray.getDimension(R.styleable.dashedline_lineSpace,10);                typedArray.recycle();

接下来就是初始化画笔和路径了:

this.mPaint = new Paint();        this.mPath = new Path();        this.mPaint.setStyle(Paint.Style.STROKE);        this.mPaint.setColor(lineColor);        this.mPaint.setAntiAlias( true);//消除锯齿        this.mPaint.setStrokeWidth(4);//画笔的尺寸

然后使用DashPathEffect这个效果来做虚线。

这个东西有两个参数new DashPathEffect(float[] float, int offset),
假如float = { 1, 2, 3, 4},效果即是:绘制长度1的实线,再绘制长度2的空白,再绘制长度3的实线,再绘制长度4的空白,依次重复下去。

使mian_activity.xml:

MainActivity中:其实这部分完全可以直接写到自定义view中去。

public class MainActivity extends Activity {
private DashedLineView line_1,line_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // line_1 默认效果 line_2 = (DashedLineView) findViewById(R.id.line_2); line_2.setMyStyle(new DashPathEffect(new float[]{
5,2,10,5}, 1)); }}

最后贴一个DashedLineView的完整代码:

public class DashedLineView extends View {
private int lineColor; private int lineLength; private int lineSpace; private Paint mPaint = null; private Path mPath = null; private PathEffect pe = null; private float[] arrayOfFloat; public DashedLineView(Context paramContext) { this(paramContext, null); } public DashedLineView(Context context, AttributeSet attributeSet) { super(context, attributeSet); TypedArray typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.dashedline); lineColor = typedArray.getColor(R.styleable.dashedline_lineColor, Color.BLACK); lineLength =(int) typedArray.getDimension(R.styleable.dashedline_lineLength,10); lineSpace =(int) typedArray.getDimension(R.styleable.dashedline_lineSpace,10); typedArray.recycle(); this.mPaint = new Paint(); this.mPath = new Path(); this.mPaint.setStyle(Paint.Style.STROKE); this.mPaint.setColor(lineColor); this.mPaint.setAntiAlias( true);//消除锯齿 this.mPaint.setStrokeWidth(4);//画笔的尺寸 arrayOfFloat = new float[2]; //假如arrayOfFloat = { 1, 2, 3, 4};效果即是:绘制长度1的实线,再绘制长度2的空白,再绘制长度3的实线,再绘制长度4的空白,依次重复下去 arrayOfFloat[0] =px2sp(context,lineLength);//线长度 arrayOfFloat[1] =px2sp(context,lineSpace);//空隙长度 this.pe = new DashPathEffect(arrayOfFloat, 1);//参数1为偏移量 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this. mPath.moveTo(0.0F, 0.0F);//画笔移动到x=0;y=0位置, this. mPath.lineTo(getMeasuredWidth(), 0.0F);//从到(0,0)位置,画线到(getMeasuredWidth(),0); this. mPaint.setPathEffect(this.pe); canvas.drawPath(this.mPath,this.mPaint); } public void setMyStyle(PathEffect p){ this.pe = p; invalidate(); } public static int px2sp(Context context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); }}

demo下载地址:

转载地址:https://blog.csdn.net/u010886975/article/details/52840639 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android Dialog去掉标题Title
下一篇:Android 代码模拟ListView item的点击事件的两种方式

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月26日 07时31分14秒