編輯:關於 Android 程式設計
先看效果:

核心其實非常樸素:一個圓柱 = 上下兩個橢圓 + 中間一片包圍面。上下橢圓用 drawOval 畫,中間部分用 Path 把左右兩側的切點連起來閉合成一個區域再 drawPath 填充,立體感就出來了。
要做出「漏斗」的層層遞進效果,只需要讓每一層圓柱的左右邊界依次向內收攏,並逐層向下偏移一段距離即可。
先定義需要用到的成員變量:
public class CylinderView extends View {
Context context;
Paint paint;
Path path;
// 橢圓內縮量、圓柱內縮量、兩圓柱的垂直間距
int sub_line = 2, sub_cicle = 24, distance = 120;
int width, height; // 屏幕寬高
// 構成圓柱的上下兩個橢圓的位置坐標
float left, right, top, bottom, left2, right2, top2, bottom2;
float start, middle, temp_top, temp_left;
float c_height1, c_height2, c_height3, c_height4, c_height5, c_height6;
RectF rel; // 橢圓
ArrayList planList = new ArrayList<>(); // 計劃值
ArrayList logList = new ArrayList<>(); // 實際值
ArrayList titleList = new ArrayList<>(); // 標題
public CylinderView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void renewCylinderView(ArrayList planList, ArrayList logList,
ArrayList titleList) {
this.planList = planList;
this.logList = logList;
this.titleList = titleList;
invalidate();
}
}
onDraw 中先準備畫筆,再按數據個數分派繪製(支持 4 層和 6 層兩種形態)。注意繪製順序是從最後一層往前畫,這樣靠上層的圖形才會覆蓋在下層之上,形成正確的前後遮擋關係:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
width = dm.widthPixels;
height = dm.heightPixels;
rel = new RectF();
sub_line = ScreenUtil.dp2px(context, 1);
sub_cicle = ScreenUtil.dp2px(context, 8);
distance = ScreenUtil.dp2px(context, 40);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true); // 去鋸齒
paint.setTextSize(ScreenUtil.dp2px(context, 16));
paint.setColor(ContextCompat.getColor(context, R.color.cricle_purplebac));
if (planList.size() == 4 || logList.size() == 4) {
draw1stMipsBac(canvas);
drawSecMipsBac(canvas);
drawThrMipsBac(canvas);
draw4stMipsBac(canvas);
draw4stMips(canvas);
drawThrMips(canvas);
drawSecMips(canvas);
draw1stMips(canvas);
} else if (planList.size() == 6 || logList.size() == 6) {
draw1stMipsBac(canvas);
drawSecMipsBac(canvas);
drawThrMipsBac(canvas);
draw4stMipsBac(canvas);
draw5stMipsBac(canvas);
draw6stMipsBac(canvas);
draw6stMips(canvas);
draw5stMips(canvas);
draw4stMips(canvas);
drawThrMips(canvas);
drawSecMips(canvas);
draw1stMips(canvas);
}
}
背景圓柱的作用是給數據圓柱提供一個「容器」參照。它的高度會被記錄下來(c_height1),後續繪製數據時按完成比例縮放:
// 第1個圓柱背景
private void draw1stMipsBac(Canvas canvas) {
left = width / 4;
right = 3 * width / 4;
top = height / 24;
bottom = top + (right - left) / 5;
left2 = left + sub_line;
right2 = right - sub_line;
top2 = top + (right - left) / 8;
bottom2 = top2 + (right - left) / 6;
c_height1 = bottom - top;
start = bottom - (bottom - top) / 2; // 下橢圓的 y 坐標
middle = bottom2 - (bottom2 - top2) / 2; // 上橢圓的 y 坐標
// 繪製橢圓2
rel.set(left2, top2, right2, bottom2);
canvas.drawOval(rel, paint);
// 繪製中間包圍面
Path path = new Path();
path.moveTo(right, start);
path.lineTo(right2, middle);
path.lineTo(left2, middle);
path.lineTo(left, start);
path.close();
canvas.drawPath(path, paint);
// 繪製橢圓1
rel.set(left, top, right, bottom);
canvas.drawOval(rel, paint);
}
數據圓柱的做法是在背景圓柱的基礎上,根據「實際值 / 計劃值」把左右邊界向中間收攏:
// 第1個圓柱
private void draw1stMips(Canvas canvas) {
paint.setColor(ContextCompat.getColor(context, R.color.common_btn_nomal));
canvas.drawText(
Integer.valueOf(logList.get(0).toString().replace(".0", "")) + "/"
+ Integer.valueOf(planList.get(0).toString().replace(".0", "")),
width / 5 * 4, height / 24 + ScreenUtil.dp2px(context, 36), paint);
canvas.drawText(titleList.get(0), width / 12,
height / 24 + ScreenUtil.dp2px(context, 36), paint);
float f;
if (planList.get(0) == 0 && logList.get(0) > 0) {
f = 1;
} else if (planList.get(0) == 0 && logList.get(0) == 0) {
return;
} else {
f = logList.get(0) / planList.get(0);
}
if (f > 1) {
f = 1; // 超額完成也按 100% 畫
}
f = 1 - f; // 剩餘比例決定收攏程度
// 左右邊界向中間收攏
left = width / 4;
right = 3 * width / 4;
temp_left = left;
left = temp_left + (right - temp_left) * f / 2;
right = right - (right - temp_left) * f / 2;
// 上下邊界按高度比例收攏
top = height / 24;
bottom = top + (right - left) / 5;
temp_top = top;
top = top + (c_height1) * f;
bottom = bottom + (c_height1) * f;
left2 = left + sub_line;
right2 = right - sub_line;
top2 = top + (right - left) / 8;
bottom2 = top2 + (right - left) / 6;
start = bottom - (bottom - top) / 2;
middle = bottom2 - (bottom2 - top2) / 2;
// 繪製橢圓2
rel.set(left2, top2, right2, bottom2);
canvas.drawOval(rel, paint);
// 繪製中間包圍面
Path path = new Path();
path.moveTo(right, start);
path.lineTo(right2, middle);
path.lineTo(left2, middle);
path.lineTo(left, start);
path.close();
canvas.drawPath(path, paint);
// 繪製橢圓1(頂面用淺色,做出受光效果)
rel.set(left, top, right, bottom);
paint.setColor(ContextCompat.getColor(context, R.color.common_btn_pressed));
canvas.drawOval(rel, paint);
}
第 2 到第 6 層的實現與第一層完全一致,只是三個參數按層級遞變:
distance * 層級序號,並對後幾層做少量像素微調,讓間距視覺上更均勻。下面以第三層為例,對比一下差異所在:
// 第3個圓柱
private void drawThrMips(Canvas canvas) {
paint.setColor(ContextCompat.getColor(context, R.color.cricle_orange2));
canvas.drawText(
Integer.valueOf(logList.get(2).toString().replace(".0", "")) + "/"
+ Integer.valueOf(planList.get(2).toString().replace(".0", "")),
width / 5 * 4, height / 24 + ScreenUtil.dp2px(context, 26) + distance * 2, paint);
canvas.drawText(titleList.get(2), width / 12,
height / 24 + ScreenUtil.dp2px(context, 26) + distance * 2, paint);
float f;
if (planList.get(2) == 0 && logList.get(2) > 0) {
f = 1;
} else if (planList.get(2) == 0 && logList.get(2) == 0) {
return;
} else {
f = logList.get(2) / planList.get(2);
}
if (f > 1) f = 1;
f = 1 - f;
left = width / 4 + sub_cicle * 2; // 關鍵:逐層向內收攏
right = 3 * width / 4 - sub_cicle * 2;
temp_left = left;
left = temp_left + (right - temp_left) * f / 2;
right = right - (right - temp_left) * f / 2;
top = height / 24 + distance * 2 - ScreenUtil.dp2px(context, 2);
bottom = top + (right - left) / 5;
temp_top = top;
top = top + (c_height3) * f;
bottom = bottom + (c_height3) * f;
left2 = left + sub_line;
right2 = right - sub_line;
top2 = top + (right - left) / 8;
bottom2 = top2 + (right - left) / 6;
start = bottom - (bottom - top) / 2;
middle = bottom2 - (bottom2 - top2) / 2;
rel.set(left2, top2, right2, bottom2);
canvas.drawOval(rel, paint);
Path path = new Path();
path.moveTo(right, start);
path.lineTo(right2, middle);
path.lineTo(left2, middle);
path.lineTo(left, start);
path.close();
canvas.drawPath(path, paint);
rel.set(left, top, right, bottom);
paint.setColor(ContextCompat.getColor(context, R.color.cricle_orange1));
canvas.drawOval(rel, paint);
}
第 4、5、6 層(draw4stMips / draw5stMips / draw6stMips)以及對應的背景方法(drawSecMipsBac 到 draw6stMipsBac)結構完全相同,把序號、sub_cicle 倍數、distance 倍數和顏色資源替換即可。
drawCylinder(canvas, level, colorTop, colorBottom) 方法,把層級當參數傳進去,代碼量能砍掉八成。
Android GridView 網格檢視的使用教學
GridView 以列與行的方式排列內容,最常見的用途是顯示圖片,例如九宮格入口。它的排列邏輯近似矩陣:當畫面上有大量元素需要以矩陣格式呈現時,GridView 就是首選
Android 消息機制詳解:為什麼子執行緒不能直接更新 UI?
先看一段看起來很正常的程式碼: public class MainActivity extends Activity implements View.OnClickLi
Android TextView 呈現 HTML 樣式文字
在 Android 介面開發中,常會遇到同一段文字需要呈現不同顏色、字級或樣式的需求。若只使用單一 TextView 屬性,往往難以同時處理姓名、數字與一般說明文字的多樣
訊飛輸入法怎麼打表情?顏表情與 emoji 兩種方式
除了微信、QQ 這些 App 自帶的表情庫,其實輸入法本身也能隨時叫出表情,不必每個應用都各自準備。以訊飛輸入法為例,主要有兩條路徑。 方法一:打顏表情 步驟 1. 在