[Android自訂控制項]Android自訂虛線

來源:互聯網
上載者:User

很多時候,畫虛線都是使用美工切圖(一個實點,一個虛點),然後使用Bitmap的repeat屬性

 



 

下面我們來畫一條虛線,首先定義一個 dashedline類,繼承於View,重寫Ondraw()方法

 

public class DashedLine extends View {private final String namespace = "http://www.android-study.com/";private float startX;private float startY;private float endX;private float endY;private Rect mRect;public DashedLine(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);//空心paint.setColor(Color.DKGRAY);Path path = new Path();//通過moveto,lineto的x,y座標確定虛線實橫,縱,還是傾斜path.moveTo(0, 10);//Set the beginning of the next contour to the point (x,y)path.lineTo(480, 10);//Add a line from the last point to the specified point (x,y).//DashPathEffect  可以使用DashPathEffect來建立一個虛線的輪廓(虛線/小圓點),而不是使用實線//float[] { 5, 5, 5, 5 }值控制虛線間距,密度PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);paint.setPathEffect(effects);canvas.drawPath(path, paint);}}


 

 

定義完View,就可以在XML中使用了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.