1.上代码。
package com.mobile.common_library.custom_view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import com.mobile.common_library.R;
import androidx.core.content.ContextCompat;
/**
* author : Urasaki
* e-mail : 1164688204@qq.com
* date : 2021/4/29 11:19
* desc : 虚线View
* version: 1.0
*/
public class DottedLineView extends View {
private static final String TAG = "DottedLineView";
private Paint mDashPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Rect mRect = new Rect();
public DottedLineView(Context context) {
super(context, null);
}
public DottedLineView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@SuppressLint({"InlinedApi", "NewApi"})
private void init() {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, metrics);
float dashGap = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, metrics);
float dashWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, metrics);
mDashPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_FF3258F7));
mDashPaint.setStyle(Paint.Style.STROKE);
mDashPaint.setStrokeWidth(width);
mDashPaint.setAntiAlias(true);
//DashPathEffect是Android提供的虚线样式API,具体的使用可以参考下面的介绍
mDashPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//取出线条的位置(位置的定义放在XML的layout中,具体如下xml文件所示)
mRect.left = left;
mRect.top = top;
mRect.right = right;
mRect.bottom = bottom;
}
@Override
protected void onDraw(Canvas canvas) {
final DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, metrics);
float dashGap = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, metrics);
float dashWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, metrics);
mDashPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_FF3258F7));
mDashPaint.setStyle(Paint.Style.STROKE);
mDashPaint.setStrokeWidth(width);
mDashPaint.setAntiAlias(true);
//DashPathEffect是Android提供的虚线样式API,具体的使用可以参考下面的介绍
mDashPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));
float x0 = (mRect.right - mRect.left) / 2f;
float y0 = 0;
float x1 = x0;
float y1 = y0 + mRect.bottom - mRect.top;
canvas.drawLine(x0, y0, x1, y1, mDashPaint);
}
}
//xml颜色
<color name="color_FF3258F7">#FF3258F7</color>
2.使用。
(1)在xml使用。
<com.mobile.main_module.custom_view.DottedLineView
android:layout_width="@dimen/dp_4"
android:layout_height="15dp"
android:layout_marginStart="@dimen/dp_56"
android:layout_marginTop="9dp" />
(2)动态使用。
FrameLayout.LayoutParams lineViewParams2 = new FrameLayout.LayoutParams(
ScreenManager.dipToPx(mContext, 4),
ScreenManager.dipToPx(mContext, 3));
lineViewParams2.setMarginStart(
ScreenManager.dipToPx(mContext, 56f));
dottedLineView = new DottedLineView(mContext);
//添加到布局
outerLayout.addView(dottedLineView, lineViewParams2);
/**
* 根据手机的分辨率从 dip 的单位 转成为 px(像素)
*/
public static int dipToPx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。