android自定义View---生成虚线的View
来源:互联网 发布:淘宝退款不退货违法 编辑:程序博客网 时间:2024/04/28 00:33
1.在res/values/目录下新建文件attrs.xml
<? xml version= "1.0" encoding= "utf-8" ?>< resources><declare-styleable name = "dashedline"><attr name = "lineColor" format= "color" /></declare-styleable ></ resources>
2.创建自定义View,DashedLine.java
public class DashedLine extends View {private Paint paint = null;private Path path = null;private PathEffect pe = null;public DashedLine(Context paramContext) {this(paramContext, null);}public DashedLine(Context paramContext, AttributeSet paramAttributeSet) {super(paramContext, paramAttributeSet);//通过R.styleable.dashedline获得我们在attrs.xml中定义的//<declare-styleable name="dashedline"> TypedArrayTypedArray a = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable. dashedline);//我们在attrs.xml中<declare-styleable name="dashedline">节点下//添加了<attr name="lineColor" format="color" />//表示这个属性名为lineColor类型为color。当用户在布局文件中对它有设定值时//可通过TypedArray获得它的值当用户无设置值是采用默认值0XFF00000int lineColor = a.getColor(R.styleable. dashedline_lineColor, 0XFF000000);a.recycle();this. paint = new Paint();this. path = new Path();this. paint.setStyle(Paint.Style. STROKE);this. paint.setColor(lineColor);this. paint.setAntiAlias( true);this. paint.setStrokeWidth(BitmapHelper. dip2px(getContext(), 2.0F));float[] arrayOfFloat = new float[4];arrayOfFloat[0] = BitmapHelper. dip2px(getContext(), 2.0F);arrayOfFloat[1] = BitmapHelper. dip2px(getContext(), 2.0F);arrayOfFloat[2] = BitmapHelper. dip2px(getContext(), 2.0F);arrayOfFloat[3] = BitmapHelper. dip2px(getContext(), 2.0F);this. pe = new DashPathEffect(arrayOfFloat, BitmapHelper.dip2px(getContext(), 1.0F));}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);this. path.moveTo(0.0F, 0.0F);this. path.lineTo(getMeasuredWidth(), 0.0F);this. paint.setPathEffect( this. pe);canvas.drawPath( this. path, this. paint);}}
3.新建布局文件dashedline.xml
<? xml version= "1.0" encoding= "utf-8" ?>< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"xmlns:dash= "http://schemas.android.com/apk/res/com.example.martixtest"android:layout_width= "fill_parent"android:layout_height= "fill_parent"android:background= "#eee"android:orientation= "vertical" ><com.example.martixtest.myview.DashedLineandroid:layout_width= "fill_parent"android:layout_height= "1dip"android:layout_margin= "10dip"dash:lineColor= "#ff50f8" /><com.example.martixtest.myview.DashedLineandroid:layout_width= "fill_parent"android:layout_height= "1dip"android:layout_margin= "10dip"/><com.example.martixtest.myview.DashedLineandroid:layout_width= "fill_parent"android:layout_height= "1dip"android:layout_margin= "10dip"dash:lineColor= "#f34f71" /></ LinearLayout>
4.最终运行效果如下:
- android自定义View---生成虚线的View
- android自定义View---生成虚线的View
- Android 自定义虚线View
- android自定义水平虚线View
- 自定义view--虚线的绘制
- Android虚线View
- Android之view虚线
- 自定义view实践之虚线
- 自学自定义view--画虚线
- Android 绘制虚线<XML><自定义view中onDraw绘制>
- android的自定义View
- android view 虚线分割线
- Android View---自定义View
- Android View---自定义View
- Android自定义View研究:View的大小
- Android自定义View研究:View的大小
- Android自定义View研究:View的大小
- Android自定义View(一) View的测量
- ASP.NET之旅—再一次与ASP谋面
- DELPHI中自适应窗体的实现
- 强大的dex反编译器
- C++】多重继承,救世主还是鸡肋
- The Synchronous FileSystem API for Workers-工作者的同步文件系统应用程序接口
- android自定义View---生成虚线的View
- 北京信息科技大学CSDN高校俱乐部举办的首次活动------考研经验交流会
- ffmpeg中的sws_scale算法性能测试
- AsyncTask介绍
- DP DAG 9-3硬币问题(算法竞赛入门经典p162)
- HEX文件格式
- 遇到神机
- 笔记本安装CentOS的步骤以及安装过程中黑屏的解决办法
- linux udp socket编程实例