自定義view用的很多了, 說實在的就是把人家的輪子拿來用 , 現(xiàn)在我自己造了一個, 雖然比較簡單 , 但是開始了
這篇就隨便寫了個平衡滑動的view?, 先上個效果圖再上計算平衡值的算法以及轉(zhuǎn)化view需要的參數(shù)
SliderView mBalance; // 這個是自定義view啦
if (Math.abs(vLeft - vRight) != 0) {// 先判斷左右不等于0? 否則平衡值就等于0.5了? 就是左右的中間
LR = Math.abs(vLeft - vRight) / (vRight + vLeft) / 2; // 計算左右偏移的比重
mZhong = 0.5f;
?if (vLeft > vRight) {? // 判斷偏左還是偏右 , 然后轉(zhuǎn)成view需要的參數(shù)
mZhong -= LR ;
} else {
mZhong += LR ;
}
mBalance.setZhong(mZhong); //
}
// 傳遞過來的參數(shù)進行一個最大值和最小值的限定再賦值給畫筆
public void setZhong(float zhong) {
if (zhong>1){
zhong = 1;
} if (zhong<0){
zhong = 0;
}
this.zhong = zhong;
}
// onDraw 部分代碼? 很簡單? 我是來騙贊的
float zhong = 0.5f;
@SuppressLint("DrawAllocation")
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
width = getMeasuredWidth();
height = getMeasuredHeight();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.GRAY);
Path path = new Path();
// 畫一個三角形,設(shè)置顏色和填充 , 設(shè)置三個頂點的位置 , 以實現(xiàn)滑動的效果
path.moveTo((int)(width*zhong),0);
path.lineTo((int)(width*(zhong+0.02)),height);
path.lineTo((int)(width*(zhong-0.02)),height);
path.close();
canvas.drawPath(path,paint);
}