public class SawtoothBlackView extends View {
//自定義畫筆的屬性
private Paint paint;
//獲取屏幕的系數(shù)
private DisplayMetrics dm;
//不規(guī)則塊的高
private int big_heigth;
//總的寬度
private int big_width;
//小角的高度
private int triangle_height;
//小角的高度
private int triangle_width;
//是否是向上畫的
private boolean state=true;
public boolean getState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public void setDm(DisplayMetrics dm) {
this.dm = dm;
}
public void setBig_heigth(int big_heigth) {
this.big_heigth = big_heigth;
}
public void setBig_width(int big_width) {
this.big_width = big_width;
}
public SawtoothBlackView(Context context) {
super(context);
init();
}
public SawtoothBlackView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SawtoothBlackView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public SawtoothBlackView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
dm= new DisplayMetrics();
((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
paint = new Paint();
//是否設(shè)置抗鋸齒效果
paint.setAntiAlias(true);
//paint.setStyle(Paint.Style.STROKE);
paint.setColor(getResources().getColor(R.color.black));
//paint.setTextSize((float) 2.0);
//paint.setStrokeWidth(3);
this.big_heigth = (int) (30*dm.density);
this.big_width = dm.widthPixels;
this.triangle_width= (int) (20*dm.density);
this.triangle_height=(int)(10*dm.density);
}
@Override
protected void onDraw(Canvas canvas) {
Path path = new Path(); //定義一條路徑
/**
* 首先畫出定點(diǎn)距離然后根據(jù)小角的寬度計(jì)算花多少次角
* 解決鋪不滿的問題
* 余數(shù)除以畫的角的數(shù)量
* 然后畫每個(gè)角的時(shí)候加
* 保證能鋪滿
*/
if(state) {
path.moveTo(0, 0);
path.lineTo(0, big_heigth);
int i = 1;
int remainder= big_width%triangle_width;
int lineCount=big_width /triangle_width*2;
for (; i <= lineCount; i++) {
path.lineTo((triangle_width / 2+((float)remainder/(float) lineCount)) * i, i % 2 == 0 ? big_heigth : big_heigth - triangle_height);
}
path.lineTo((triangle_width / 2+((float)remainder/(float) lineCount)) * (i > 1 ? i - 1 : i), 0);
path.lineTo(0, 0);
}else{
path.moveTo(0,0);
int i=1;
int remainder= big_width%triangle_width;
int lineCount=big_width /triangle_width*2;
//path.lineTo(triangle_width/2*i+remainder,i%2==0?0:triangle_height);
for(; i< lineCount; i++){
path.lineTo( ((triangle_width/2+((float)remainder/(float) lineCount))*i),i%2==0?0:triangle_height);
}
path.lineTo(big_width,0);
path.lineTo(big_width, big_heigth);
//path.lineTo(dm.widthPixels, big_heigth);
path.lineTo(0, big_heigth);
path.lineTo(0,0);
}
canvas.drawPath(path, paint);
}
}
使用
<net.sourceforge.simcpux.customviewsawtoothstyle.SawtoothBlackView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/sawtooth"
/>
默認(rèn)是占滿全屏幕的
修改參數(shù)參照
SawtoothBlackView sawtooth1= (SawtoothBlackView) findViewById(R.id.sawtooth1);
sawtooth1.setState(false);