我想做一个虚线.我现在正在使用这个实线:
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)
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 []数组中提供更多数字来创建各种虚线模式,它指定了破折号和间隙的比率.这是一条简单,同样虚线的线.
小智 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)
小智 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)
通过使用此类,您可以将"虚线和下划线"效果应用于多行文本.要使用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)
结果:
如果您正在寻找垂直线,请使用此可绘制对象。
<?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:它代表每个点之间的“距离”。
我为 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)
干杯! :)