用xml画水平虚线和竖直虚线.md
1.畫水平虛線
直接建一個shape,設置stroke屬性就行了,再將這個屬性直接作為background的drawable屬性引入就行了
注意在4.0以上的真機加一句
2.畫豎直虛線
這里借鑒一個 rotate屬性去實現(xiàn),代碼如下
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:left="-30dp"android:right="-30dp"><rotateandroid:drawable="@drawable/dottde_line"android:visible="true"android:fromDegrees="90"></rotate></item> </layer-list> <Viewandroid:id="@+id/content_2"android:layout_width="30dp"android:layout_height="match_parent"android:layout_gravity="center_horizontal"android:background="@drawable/dottde_v_line"android:layerType="software" />原理:
設置了fromDegress之后,會先畫一條水平虛線,然后再瞬間順時針旋轉(zhuǎn)90度角,但是單這樣還不行,因為我的view的寬度設置的是2dp,高度是match_parent,發(fā)現(xiàn)出來的只有一個點,郁悶了。
之前說過,他的原理是先畫一條水平線,然后再旋轉(zhuǎn),那么view的寬度只有2dp,他就只能畫2dp,所以旋轉(zhuǎn)后也就是一個點。因此用item的可以設置偏移量的屬性,我們將rotate節(jié)點放于一個item節(jié)點下面,然后給item設置左右都為-30dp的偏移量,這樣他在先畫水平線的時候,由于負的偏移量(就和負的margin一樣),就可以畫出60dp長的線,然后再旋轉(zhuǎn),就可以得到一條豎直虛線。
3用自定義view去畫
public class DashedLineView extends View{public Context ctx;public DashedLineView(Context context, AttributeSet attrs) {super(context, attrs);ctx=context;}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setColor(getResources().getColor(R.color.red));paint.setStrokeWidth(dip2px(ctx,2));Path path = new Path();path.moveTo(0, 0);path.lineTo(0, 900);PathEffect effects = new DashPathEffect(new float[]{6, 4, 4, 4}, 2);paint.setPathEffect(effects);canvas.drawPath(path, paint);}/*** 根據(jù)手機的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素)*/public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);} }原理:
4.認識圖層列表
地址 https://developer.android.google.cn/guide/topics/resources/drawable-resource.html?hl=zh-cn#LayerList
轉(zhuǎn)載于:https://www.cnblogs.com/prophet-it/p/6651033.html
總結(jié)
以上是生活随笔為你收集整理的用xml画水平虚线和竖直虚线.md的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux - chmod
- 下一篇: Jmeter测试监控 Summary R