Android自定義水平或垂直虛線效果
項(xiàng)目中有時(shí)候會(huì)用到虛線,怎么辦?drawable下創(chuàng)建一個(gè)shape類(lèi)型的xml文件繪制,然后引用到view的background下?如果用到虛線的地方很多呢?創(chuàng)建多個(gè),分別引用?橫向的還好說(shuō),豎向的呢?垂直的虛線,普通的創(chuàng)建是顯示不出來(lái)的,如果需要,就要進(jìn)行旋轉(zhuǎn)等的操作。但是,還是那個(gè)問(wèn)題,需要很多個(gè)怎么辦?挨個(gè)創(chuàng)建?
完全沒(méi)必要,寫(xiě)個(gè)自定義,對(duì)外暴露設(shè)置虛線屬性的方法就行。源碼如下:
最后的說(shuō)明很重要!??!
最后的說(shuō)明很重要!?。?
最后的說(shuō)明很重要?。?!
效果圖:

源碼:
ImaginaryLineView
package com.chen.demo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 自定義垂直虛線view
* chenjianqiang
* 2017/6/14
* <p>
* 使用方法:
* 在代碼中findview之后,調(diào)用setLineAttribute方法,自定義虛線顏色及寬度
*/
public class ImaginaryLineView extends View {
private Context ct;
private Paint mPaint;
private Path mPath;
private PathEffect effects;
private int width;
private int height;
private int defaultColor=0xffff0000;
public ImaginaryLineView(Context context) {
this(context, null);
}
public ImaginaryLineView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public ImaginaryLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ct = context;
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
private void init() {
//初始化,并打開(kāi)抗鋸齒
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(defaultColor);
mPaint.setStrokeWidth(dip2px(ct, 1));
mPath = new Path();
//數(shù)組含義:里面最少要有2個(gè)值,值的個(gè)數(shù)必須是偶數(shù)個(gè)。偶數(shù)位(包含0),表示實(shí)線長(zhǎng)度,奇數(shù)位表示斷開(kāi)的長(zhǎng)度
effects = new DashPathEffect(new float[]{4, 2}, 0);
}
/**
* 設(shè)置線的必要屬性
*
* @param color 十六進(jìn)制顏色值
* @param lineWidth 虛線寬度,單位是dp
*/
public void setLineAttribute(int color, float lineWidth,float[] f) {
if (color == 0) {
color = defaultColor;
}
if (lineWidth == 0) {
lineWidth = 1;
}
if(f==null){
f=new float[]{4,2};
}
effects = new DashPathEffect(f, 0);
mPaint.setStrokeWidth(dip2px(ct, lineWidth));
mPaint.setColor(color);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//定義起點(diǎn)
mPath.moveTo(0, 0);
//定義終點(diǎn)
if(width>height){
//寬度比高度大,是橫線
mPath.lineTo(width, 0);
}else{
//豎線。(根據(jù)實(shí)際情況,這里不考慮寬高相等情況)
mPath.lineTo(0, height);
}
mPaint.setPathEffect(effects);
canvas.drawPath(mPath, mPaint);
}
private static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="默認(rèn)橫線" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:background="#5500ff00" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="100dp" android:layout_height="1dp"/> <TextView android:text="自定義屬性橫線" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:id="@+id/horizontal_line" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="100dp" android:layout_height="1dp"/> <TextView android:text="默認(rèn)橫線" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="2dp" android:layout_height="100dp"/> <TextView android:text="自定義屬性豎線" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:id="@+id/vertical_line" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="2dp" android:layout_height="100dp"/> </LinearLayout>
MainActivity
package com.chen.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity {
private ImaginaryLineView horizontal_line;
private ImaginaryLineView vertical_line;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
horizontal_line= (ImaginaryLineView) findViewById(R.id.horizontal_line);
horizontal_line.setLineAttribute(0xff00ff00,5,null);
vertical_line= (ImaginaryLineView) findViewById(R.id.vertical_line);
vertical_line.setLineAttribute(0xff0000ff,5,new float[]{10,2,5,5});
}
}
說(shuō)明:
1、這個(gè)自定義view,會(huì)自動(dòng)判斷是水平還是豎直。自己僅僅需要在布局文件中設(shè)置了寬高就行。
2、在自定義的源碼中,僅僅是粗略的限定了虛線路徑,準(zhǔn)確的說(shuō),應(yīng)該是寬的中點(diǎn)到高的中點(diǎn),因?yàn)橐话愕奶摼€都是1px,或者1dp寬,少數(shù)會(huì)到2dp,這么窄的值,取不取中點(diǎn)無(wú)所謂。如果虛線很寬,就會(huì)有一點(diǎn)誤差,如圖:

藍(lán)色的是繪制出來(lái)的虛線,但是這個(gè)虛線10dp寬,即:虛線畫(huà)筆比設(shè)置的寬度要小,就會(huì)這樣。不過(guò)一般不會(huì)有種情況。如果遇到,根據(jù)實(shí)際情況修改即可
3、不建議為了省事而把終點(diǎn)設(shè)置為:mPath.lineTo(width, height);
4、需要虛線的時(shí)候,布局到文件中,setLineAttribute即可,不用每次都新建一個(gè)shape
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義時(shí)間軸的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Android自定義時(shí)間軸的實(shí)現(xiàn)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android彈出DatePickerDialog并獲取值的方法
這篇文章主要為大家詳細(xì)介紹了Android彈出DatePickerDialog并獲取值的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android App打包加固后的APK無(wú)法安裝問(wèn)題解決
Android應(yīng)用當(dāng)中,很多隱私信息都是以 字符串的形式存在的,所以需要加密,本文主要介紹了Android App打包加固后的APK無(wú)法安裝問(wèn)題解決,感興趣的可以了解一下2024-01-01
Android仿一號(hào)店貨物詳情輪播圖動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了Android-仿一號(hào)店貨物詳情輪播圖動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android實(shí)現(xiàn)個(gè)性化的進(jìn)度條
這篇文章主要介紹了Android實(shí)現(xiàn)個(gè)性化的進(jìn)度條 的相關(guān)資料,需要的朋友可以參考下2016-07-07
Android中多行文本末尾添加圖片排版問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Android中多行文本末尾添加圖片排版問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android網(wǎng)絡(luò)請(qǐng)求-sign參數(shù)的設(shè)置方式
這篇文章主要介紹了Android網(wǎng)絡(luò)請(qǐng)求-sign參數(shù)的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android解析服務(wù)器端發(fā)來(lái)的xml數(shù)據(jù)示例
Android跟服務(wù)器交互數(shù)據(jù),有時(shí)數(shù)據(jù)量大時(shí),就需要以xml形式的交互數(shù)據(jù),下面與大家分享下使用XmlPullParser來(lái)解析xml數(shù)據(jù),感興趣的朋友可以參考下哈2013-06-06

