如何在Android中创建虚线/虚线?

Jas*_*son 231 android

我想做一个虚线.我现在正在使用这个实线:

LinearLayout divider = new LinearLayout( this );
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, 2 );
divider.setLayoutParams( params );
divider.setBackgroundColor( getResources().getColor( R.color.grey ) );
Run Code Online (Sandbox Code Playgroud)

我需要这样的东西,但点缀而不是坚实.我想避免在透明布局和实体布局之间交替进行数百种布局.

emb*_*mbo 466

没有java代码:

绘制/ dotted.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">

    <stroke
       android:color="#C7B299"
       android:dashWidth="10px"
       android:dashGap="10px"
       android:width="1dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

view.xml用:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:src="@drawable/dotted"
    android:layerType="software" />
Run Code Online (Sandbox Code Playgroud)

  • 没关系,我在这里找到了解决方案:http://stackoverflow.com/questions/10843402/android-dashed-line-drawable-potential-ics-bug只需将图层类型设置为软件:android:layerType ="software" (72认同)
  • 我不明白为什么我的手机上有一条没有间隙的线,而我正在看Eclipse中的图形布局中的空白. (20认同)
  • 使用`dp`而不是`px`. (10认同)
  • 使用`canvas.drawLine()`时,存在[已知错误](https://code.google.com/p/android/issues/detail?id=29944).作为禁用硬件加速的替代方法,您可以使用`canvas.drawPath`代替. (7认同)
  • 验证这适用于Android 6.0.1和4.4.4.有两点需要注意:1)你确实需要一个ImageView,一个带有drawable的View作为背景没有做到; 2)必须在ImageView上设置`android:layerType ="software"`. (6认同)
  • 在我的例子中,`ImageView` 的`layout_height` 必须大于`Stroke` 的`width`,如果这些值相等,虚线将消失。 (2认同)

sil*_*gle 202

路径效果在paint对象上设置

Paint fgPaintSel = new Paint();
fgPaintSel.setARGB(255, 0, 0,0);
fgPaintSel.setStyle(Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
Run Code Online (Sandbox Code Playgroud)

您可以通过在int []数组中提供更多数字来创建各种虚线模式,它指定了破折号和间隙的比率.这是一条简单,同样虚线的线.

  • 我在android开发方面略有新意.我想问一下,带有虚线效果的paint对象是如何添加到我刚刚创建的线性布局中的? (4认同)
  • 我直接复制粘贴了这段代码,但是没有任何反应。我应该编写额外的代码来使其工作吗? (2认同)

小智 48

这会对你有所帮助.使用XML创建虚线.在可绘制文件夹中创建xml,并将该背景提供给要设置虚线边框的项目.

---->创建XML背景"dashed_border"

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#ffffff" />
            <stroke
                android:dashGap="5dp"
                android:dashWidth="5dp"
                android:width="1dp"
                android:color="#0000FF" />
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

----->将该背景添加到项目中

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/dashed_border"/>
Run Code Online (Sandbox Code Playgroud)

  • 我认为对于单个项目来说图层列表是不必要的。我使用 &lt;shape&gt; 作为根元素,它的工作原理是相同的。 (2认同)

小智 37

创建xml(view_line_dotted.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="-1dp"
            android:left="-1dp"
            android:right="-1dp"
            android:top="0dp">

            <shape android:shape="rectangle">
                <stroke
                    android:width="1dp"
                    android:color="#ffff0017"
                    android:dashGap="3dp"
                    android:dashWidth="1dp" />

                <solid android:color="@android:color/transparent" />

                <padding
                    android:bottom="10dp"
                    android:left="10dp"
                    android:right="10dp"
                    android:top="10dp" />
            </shape>
        </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

设置为视图的背景:

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/view_line_dotted" />
Run Code Online (Sandbox Code Playgroud)


Ton*_* Vu 22

当我想画一条虚线时我做的是定义一个可绘制的dash_line.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line" >
<stroke
    android:dashGap="3dp"
    android:dashWidth="2dp"
    android:width="1dp"
    android:color="@color/black" />
</shape>
Run Code Online (Sandbox Code Playgroud)

然后在布局中定义一个背景为dash_line的视图.注意包括android:layerType ="software",否则它将无效.

<View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@drawable/dash_line"
            android:layerType="software" />
Run Code Online (Sandbox Code Playgroud)


rui*_*dge 20

我定制了一条支持水平和垂直虚线的虚线.代码如下:

public class DashedLineView extends View
{
private float density;
private Paint paint;
private Path path;
private PathEffect effects;

public DashedLineView(Context context)
{
    super(context);
    init(context);
}

public DashedLineView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    init(context);
}

public DashedLineView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context)
{
    density = DisplayUtil.getDisplayDensity(context);
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(density * 4);
    //set your own color
    paint.setColor(context.getResources().getColor(R.color.XXX));
    path = new Path();
    //array is ON and OFF distances in px (4px line then 2px space)
    effects = new DashPathEffect(new float[] { 4, 2, 4, 2 }, 0);

}

@Override
protected void onDraw(Canvas canvas)
{
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    paint.setPathEffect(effects);
    int measuredHeight = getMeasuredHeight();
    int measuredWidth = getMeasuredWidth();
    if (measuredHeight <= measuredWidth)
    {
        // horizontal
        path.moveTo(0, 0);
        path.lineTo(measuredWidth, 0);
        canvas.drawPath(path, paint);
    }
    else
    {
        // vertical
        path.moveTo(0, 0);
        path.lineTo(0, measuredHeight);
        canvas.drawPath(path, paint);
    }

}
}
Run Code Online (Sandbox Code Playgroud)


Ngu*_*Dat 8

通过使用此类,您可以将"虚线和下划线"效果应用于多行文本.要使用DashPathEffect,您必须关闭TextView的hardwareAccelerated(尽管DashPathEffect方法有长文本问题).你可以在这里找到我的示例项目:https://github.com/jintoga/Dashed-Underlined-TextView/blob/master/Untitled.png.

public class DashedUnderlineSpan implements LineBackgroundSpan, LineHeightSpan {

    private Paint paint;
    private TextView textView;
    private float offsetY;
    private float spacingExtra;

    public DashedUnderlineSpan(TextView textView, int color, float thickness, float dashPath,
                               float offsetY, float spacingExtra) {
        this.paint = new Paint();
        this.paint.setColor(color);
        this.paint.setStyle(Paint.Style.STROKE);
        this.paint.setPathEffect(new DashPathEffect(new float[] { dashPath, dashPath }, 0));
        this.paint.setStrokeWidth(thickness);
        this.textView = textView;
        this.offsetY = offsetY;
        this.spacingExtra = spacingExtra;
    }

    @Override
    public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
                             Paint.FontMetricsInt fm) {
        fm.ascent -= spacingExtra;
        fm.top -= spacingExtra;
        fm.descent += spacingExtra;
        fm.bottom += spacingExtra;
    }

    @Override
    public void drawBackground(Canvas canvas, Paint p, int left, int right, int top, int baseline,
                               int bottom, CharSequence text, int start, int end, int lnum) {
        int lineNum = textView.getLineCount();
        for (int i = 0; i < lineNum; i++) {
            Layout layout = textView.getLayout();
            canvas.drawLine(layout.getLineLeft(i), layout.getLineBottom(i) - spacingExtra + offsetY,
                    layout.getLineRight(i), layout.getLineBottom(i) - spacingExtra + offsetY,
                    this.paint);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

虚线下划线


Dun*_*gan 7

如果您正在寻找垂直线,请使用此可绘制对象。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-8dp"
        android:bottom="-8dp"
        android:left="-8dp">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke
                android:width="4dp"
                android:color="#ffffff"
                android:dashGap="4dp"
                android:dashWidth="4dp"/>
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

顶部和底部的负值和左值将删除形状的多余边,并留下一条虚线。

在这样的视图中使用它。

<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="@drawable/dash_line_vertical"
android:layerType="software" />
Run Code Online (Sandbox Code Playgroud)


小智 6

对于画布上的点状效果,将此属性设置为绘制对象:

paint.setPathEffect(new DashPathEffect(new float[] {0,30}, 0));
Run Code Online (Sandbox Code Playgroud)

并根据您的渲染适合您更改值 30:它代表每个点之间的“距离”。

在此处输入图片说明


Wik*_*ski 5

我为 EditText 创建了虚线。干得好。创建新的 xml。例如 dashed_border.xml 这里的代码:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="2dp"
            android:color="#000000"
            android:dashGap="3dp"
            android:dashWidth="1dp" />

        <solid android:color="#00FFFFFF" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />
    </shape>
</item></layer-list>
Run Code Online (Sandbox Code Playgroud)

并在 EditText 中使用新的 xml 文件,例如:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dashed_border"/>
Run Code Online (Sandbox Code Playgroud)

干杯! :)