Android绘制虚线View

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,此项目持续维护中。

项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值