package com.chen.demo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 自定义垂直虚线view
* chenjianqiang
* 2017/6/14
* <p>
* 使用方法:
* 在代码中findview之后,调用setLineAttribute方法,自定义虚线颜色及宽度
*/
public class ImaginaryLineView extends View {
private Context ct;
private Paint mPaint;
private Path mPath;
private PathEffect effects;
private int width;
private int height;
private int defaultColor=0xffff0000;
public ImaginaryLineView(Context context) {
this(context, null);
}
public ImaginaryLineView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public ImaginaryLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ct = context;
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
private void init() {
//初始化,并打开抗锯齿
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(defaultColor);
mPaint.setStrokeWidth(dip2px(ct, 1));
mPath = new Path();
//数组含义:里面最少要有2个值,值的个数必须是偶数个。偶数位(包含0),表示实线长度,奇数位表示断开的长度
effects = new DashPathEffect(new float[]{4, 2}, 0);
}
/**
* 设置线的必要属性
*
* @param color 十六进制颜色值
* @param lineWidth 虚线宽度,单位是dp
*/
public void setLineAttribute(int color, float lineWidth,float[] f) {
if (color == 0) {
color = defaultColor;
}
if (lineWidth == 0) {
lineWidth = 1;
}
if(f==null){
f=new float[]{4,2};
}
effects = new DashPathEffect(f, 0);
mPaint.setStrokeWidth(dip2px(ct, lineWidth));
mPaint.setColor(color);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//定义起点
mPath.moveTo(0, 0);
//定义终点
if(width>height){
//宽度比高度大,是横线
mPath.lineTo(width, 0);
}else{
//竖线。(根据实际情况,这里不考虑宽高相等情况)
mPath.lineTo(0, height);
}
mPaint.setPathEffect(effects);
canvas.drawPath(mPath, mPaint);
}
private static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="默认横线" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:background="#5500ff00" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="100dp" android:layout_height="1dp"/> <TextView android:text="自定义属性横线" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:id="@+id/horizontal_line" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="100dp" android:layout_height="1dp"/> <TextView android:text="默认横线" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="2dp" android:layout_height="100dp"/> <TextView android:text="自定义属性竖线" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.chen.demo.ImaginaryLineView android:id="@+id/vertical_line" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_width="2dp" android:layout_height="100dp"/> </LinearLayout>
package com.chen.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity {
private ImaginaryLineView horizontal_line;
private ImaginaryLineView vertical_line;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
horizontal_line= (ImaginaryLineView) findViewById(R.id.horizontal_line);
horizontal_line.setLineAttribute(0xff00ff00,5,null);
vertical_line= (ImaginaryLineView) findViewById(R.id.vertical_line);
vertical_line.setLineAttribute(0xff0000ff,5,new float[]{10,2,5,5});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有