日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android canvas_Android自定义View之绘制虚线

發(fā)布時間:2024/10/14 Android 76 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android canvas_Android自定义View之绘制虚线 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
開發(fā)中遇到需要畫虛線,我們首先就會想到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"是干什么的?這是因為我們現(xiàn)在的手機默認都是開啟了硬件加速的,而dashGap不支持硬件加速,我們需要把硬件加速關了就好了。使用ShapeDrawable實現(xiàn)虛線的方式雖然簡單,但是簡單就意味著不靈活。比如說要求虛線是根據(jù)用戶操作來判斷要不要添加的,要實現(xiàn)動態(tài)改變虛線的粗細和顏色等樣式呢,不可能一個顏色寫一個ShapeDrawable吧,這種情況下就不如使用Canvas來實現(xiàn)方便了。我們都知道Canvas只有drawLine方法,沒有畫虛線的方法,但是我們可以用畫筆Paint的setPathEffect(PathEffect effect)方法,PathEffect一共有五個子類:ComposePathEffect, CornerPathEffect,DashPathEffect, DiscretePathEffect,?PathDashPathEffect, SumPathEffect,?其中的DashPathEffect就是可以用來實現(xiàn)虛線效果的,但是通過查閱資料發(fā)現(xiàn)它有一個弊端:不支持硬件加速!好吧,那我們用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方法動態(tài)設置虛線的顏色,大家可以根據(jù)需求自行添加方法。/** * 虛線顏色 * @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); }原理上還真的是比較簡單的,公司項目用到,以作記錄之用。以下是實現(xiàn)出的效果圖

到這里就結(jié)束啦。往期精彩回顧:
  • Android實現(xiàn)短信驗證碼自動填充功能

  • Android仿echo精美彈幕功能

  • Android實現(xiàn)頭像重疊排列功能

  • Android仿QQ個性標簽功能

  • Android仿QQ側(cè)滑刪除的功能

總結(jié)

以上是生活随笔為你收集整理的android canvas_Android自定义View之绘制虚线的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。