画一条虚线
2017-03-18 07:28
134 查看
方法一:
1、
通过自定义控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.drawxuxian.DotView
android:layout_marginTop="10dp"
android:layout_height="2dp"
android:layout_width="match_parent">
</com.example.drawxuxian.DotView>
</LinearLayout>
2、
复制进自己项目的时候注意上面xml中自定义控件名称
public class DotView extends View {
private Paint p;
private int width;
private int height;
private int dash;
public DotView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DotView(Context context) {
super(context);
init(context);
}
private void init(Context context){
p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStyle(Style.FILL);
p.setColor(Color.parseColor("#d8d8d8"));
final float scale = context.getResources().getDisplayMetrics().density;
dash=(int) (6 * scale + 0.5f);
}
@Override
protected void onDraw(Canvas canvas) {
if(width>10){
for(int i=0;i<width;i+=dash){
canvas.drawLine(i, 0, i+=dash, 0, p);
}
}
super.onDraw(canvas);
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width=w;
height=h;
p.setStrokeWidth(height);
this.postInvalidate();
}
}
方法二:
1、
在drawable目录下,定义一个line.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线 -->
<stroke android:width="1dp" android:color="#D5D5D5"
android:dashWidth="2dp" android:dashGap="3dp" />
<!-- 虚线的高度 -->
<size android:height="2dp" />
</shape>
2、
在布局文件中定义一个view
<View
android:layout_marginTop="10dp"
android:layout_height="2dp"
android:layout_width="match_parent"
android:background="@drawable/line"
/>
3、
关闭硬件加速:(这句代码一定要有)
<activity
XXX
android:hardwareAccelerated="false"
XXX
</activity>
1、
通过自定义控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.drawxuxian.DotView
android:layout_marginTop="10dp"
android:layout_height="2dp"
android:layout_width="match_parent">
</com.example.drawxuxian.DotView>
</LinearLayout>
2、
复制进自己项目的时候注意上面xml中自定义控件名称
public class DotView extends View {
private Paint p;
private int width;
private int height;
private int dash;
public DotView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DotView(Context context) {
super(context);
init(context);
}
private void init(Context context){
p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStyle(Style.FILL);
p.setColor(Color.parseColor("#d8d8d8"));
final float scale = context.getResources().getDisplayMetrics().density;
dash=(int) (6 * scale + 0.5f);
}
@Override
protected void onDraw(Canvas canvas) {
if(width>10){
for(int i=0;i<width;i+=dash){
canvas.drawLine(i, 0, i+=dash, 0, p);
}
}
super.onDraw(canvas);
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width=w;
height=h;
p.setStrokeWidth(height);
this.postInvalidate();
}
}
方法二:
1、
在drawable目录下,定义一个line.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线 -->
<stroke android:width="1dp" android:color="#D5D5D5"
android:dashWidth="2dp" android:dashGap="3dp" />
<!-- 虚线的高度 -->
<size android:height="2dp" />
</shape>
2、
在布局文件中定义一个view
<View
android:layout_marginTop="10dp"
android:layout_height="2dp"
android:layout_width="match_parent"
android:background="@drawable/line"
/>
3、
关闭硬件加速:(这句代码一定要有)
<activity
XXX
android:hardwareAccelerated="false"
XXX
</activity>
相关文章推荐
- 画一条虚线
- 画一条虚线的学问
- 在html中打出一条虚线
- iOS笔记之画一条虚线
- 【Android开发笔记】光速码出一条万能虚线
- ps怎么画出一条直的虚线?
- Android画一条虚线
- android画一条虚线
- 在android中画一条虚线
- 在word中,怎么插入一条水平线、粗线、波浪线、双直线、虚线 -------自动边框线
- 论如何画好一条虚线
- android shape的应用之画一条虚线
- 如何添加一条虚线
- div+css中一条直线和一条虚线的实现。
- SVG(可缩放矢量图形)虚线相关属性与线条动画原理:一条会动的线
- flash绘图API:绘制一条虚线
- Android画一条虚线
- 一条SMS最大字符数,字符数达到多少按MMS处理
- 关于嵌入式如何学习(看了不后悔,给学技术的同行一条光明的路)
- ResultSet取得一条记录的列数