android canvas_Android自定义View之绘制虚线
生活随笔
收集整理的這篇文章主要介紹了
android canvas_Android自定义View之绘制虚线
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
開發中遇到需要畫虛線,我們首先就會想到ShapeDrawable,在布局中加一個View,并給它添加一個虛線背景,是挺簡單的。
<?xml version="1.0" encoding="utf-8"?> android:shape="line"> android:dashGap="4dp" android:dashWidth="8dp" android:width="1dp" android:color="#ffeaebf0" /> android:layout_width="match_parent" android:layout_height="2dp" android:layout_gravity="center_vertical" android:layerType="software" android:background="@drawable/shape_dash_line" />添加虛線背景,好理解,可是android:layerType="software"是干什么的?這是因為我們現在的手機默認都是開啟了硬件加速的,而dashGap不支持硬件加速,我們需要把硬件加速關了就好了。使用ShapeDrawable實現虛線的方式雖然簡單,但是簡單就意味著不靈活。比如說要求虛線是根據用戶操作來判斷要不要添加的,要實現動態改變虛線的粗細和顏色等樣式呢,不可能一個顏色寫一個ShapeDrawable吧,這種情況下就不如使用Canvas來實現方便了。我們都知道Canvas只有drawLine方法,沒有畫虛線的方法,但是我們可以用畫筆Paint的setPathEffect(PathEffect effect)方法,PathEffect一共有五個子類:ComposePathEffect, CornerPathEffect,DashPathEffect, DiscretePathEffect,?PathDashPathEffect, SumPathEffect,?其中的DashPathEffect就是可以用來實現虛線效果的,但是通過查閱資料發現它有一個弊端:不支持硬件加速!好吧,那我們用drawPath來繪制路徑吧。public class DashLineView extends View { private Paint mPaint; private Path mPath; //虛線顏色 private int backgroundColor; //虛線粗細 private int strokeWidth; //虛線寬度 private int dashWidth; //虛線隔斷寬度 private int dashSpaceWidth; public DashLineView(Context context) { super(context); initView(); } public DashLineView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); obtainAttributes(context, attrs); initView(); } public DashLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); obtainAttributes(context, attrs); initView(); } private void obtainAttributes(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DashLineView); backgroundColor = ta.getColor(R.styleable.DashLineView_pt_backgroundColor, getResources().getColor(R.color.line)); strokeWidth = ta.getInt(R.styleable.DashLineView_pt_strokeWidth, ScreenUtil.dip2px(context, 1)); dashWidth = ta.getInt(R.styleable.DashLineView_pt_dashWidth, ScreenUtil.dip2px(context, 4)); dashSpaceWidth = ta.getInt(R.styleable.DashLineView_pt_dashSpaceWidth, ScreenUtil.dip2px(context, 3)); ta.recycle(); } private void initView(){ //使用isInEditMode解決可視化編輯器無法識別自定義控件的問題 if (isInEditMode()) { return; } mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // 需要加上這句,否則畫不出東西 mPaint.setStyle(Paint.Style.STROKE); mPath = new Path(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //自定義的View能夠使用wrap_content或者是match_parent的屬性 setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()); } @Override protected void onDraw(Canvas canvas) { mPaint.setColor(backgroundColor); mPaint.setStrokeWidth(strokeWidth); mPaint.setPathEffect(new DashPathEffect(new float[] { dashWidth, dashSpaceWidth }, 0)); int centerY = getHeight() / 2; mPath.reset(); mPath.moveTo(0, centerY); mPath.lineTo(getWidth(), centerY); canvas.drawPath(mPath, mPaint); } public void setDashLineColor(int bgColor) { this.backgroundColor = getResources().getColor(bgColor); }}我這里只提供了setDashLineColor方法動態設置虛線的顏色,大家可以根據需求自行添加方法。/** * 虛線顏色 * @param color */ public void setLineViewColor(int color){ mDashLineView.setDashLineColor(color); }attr.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="DashLineView"> <attr name="pt_backgroundColor" format="color"/> <attr name="pt_strokeWidth" format="dimension"/> <attr name="pt_dashWidth" format="dimension"/> <attr name="pt_dashSpaceWidth" format="dimension"/> declare-styleable>resources>ScreenUtil.java
public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }原理上還真的是比較簡單的,公司項目用到,以作記錄之用。以下是實現出的效果圖到這里就結束啦。往期精彩回顧:Android實現短信驗證碼自動填充功能
Android仿echo精美彈幕功能
Android實現頭像重疊排列功能
Android仿QQ個性標簽功能
Android仿QQ側滑刪除的功能
總結
以上是生活随笔為你收集整理的android canvas_Android自定义View之绘制虚线的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wp自定义帖子没标签_谷歌对nofoll
- 下一篇: access 导入 txt sql语句_