編輯:關於 Android 程式設計
近期產品涉及訂單頁,設計稿上出現一個類似「餓了麼」、「支付寶口碑外賣」訂單頁的彩帶元素——通俗來說,就是由兩種顏色交錯拼接而成的分隔線。


共同特色是:以兩色相間拼接,長度則完全由螢幕寬度決定。正因為寬度不固定,用一張圖片當 ImageView 背景並不理想。與其如此,不如自訂 View 把它畫出來。
public class ColourLineView extends View{
public ColourLineView(Context context) {
super(context, null);
}
public ColourLineView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public ColourLineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
Path path = new Path();
canvas.save();
path.reset();
path.moveTo(width / 2, 0); // 左上
path.lineTo(0, height); // 左下
path.lineTo(width - width / 2, height); // 右下
path.lineTo(width, 0); // 右上
canvas.clipPath(path); // 把路徑範圍裁出來
canvas.drawColor(Color.RED);
path.reset();
canvas.restore();
}
}
關鍵在 onDraw:先取得 View 的寬高,建立 Path,把起點移到(寬的二分之一, 0),接著依序連到(0, 高)、(寬的二分之一, 高)、(寬, 0)。四個點連完後呼叫 clipPath 把範圍裁出來,再填色,一個平行四邊形就完成了。
在版面中試用:
<com.example.yang.statubardemo.ColourLineView android:layout_width="80dp" android:layout_height="80dp" android:background="#000"/>





形狀出來了。學會畫一塊磚,砌成整面牆自然不成問題。
一個夠用的彩色分隔線應該允許調整:線條高度、單一色塊寬度、色塊間距、傾斜程度、兩種顏色以及底色。先在 attrs.xml 宣告:
<declare-styleable name="ColourLineView"> <!-- 線條高度 --> <attr name="line_height" format="dimension"/> <!-- 第一種顏色塊的寬度 --> <attr name="item_width" format="dimension"/> <!-- 第二種顏色塊的寬度 --> <attr name="separation_width" format="dimension"/> <!-- 平行四邊形傾斜的程度 --> <attr name="lean_degree" format="dimension"/> <!-- 第一種顏色 --> <attr name="first_color" format="color"/> <!-- 第二種顏色 --> <attr name="second_color" format="color"/> <!-- 線條底色 --> <attr name="canvas_color" format="color"/> </declare-styleable>
接著在自訂 View 中讀取這些屬性並重複繪製色塊:
public class ColourLineView extends View{
private float line_height; // 線條高度
private float item_width; // 每個顏色塊的寬度
private float separation_width; // 兩個色塊之間的間距
private float lean_degree; // 傾斜程度
private int first_color; // 第一種顏色
private int second_color; // 第二種顏色
private int canvas_color; // 底色
public ColourLineView(Context context) {
super(context, null);
}
public ColourLineView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttr(context, attrs);
}
public ColourLineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttr(context, attrs);
}
public void initAttr(Context context, AttributeSet attrs){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColourLineView);
line_height = typedArray.getDimension(R.styleable.ColourLineView_line_height, 20);
item_width = typedArray.getDimension(R.styleable.ColourLineView_item_width, 20);
separation_width = typedArray.getDimension(R.styleable.ColourLineView_separation_width, 20);
lean_degree = typedArray.getDimension(R.styleable.ColourLineView_lean_degree, 5);
first_color = typedArray.getColor(R.styleable.ColourLineView_first_color, Color.RED);
second_color = typedArray.getColor(R.styleable.ColourLineView_second_color, Color.GREEN);
canvas_color = typedArray.getColor(R.styleable.ColourLineView_canvas_color, Color.WHITE);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path = new Path();
int lineWidth = getWidth();
int lineHeight = getHeight();
int count = (item_width + separation_width == 0) ? 0
: lineWidth / (int) (item_width + separation_width) + 1;
for (int i = 0; i < count; i++) {
canvas.save();
path.reset();
path.moveTo(lean_degree + (item_width + separation_width) * i, 0);
path.lineTo((item_width + separation_width) * i, lineHeight);
path.lineTo(item_width * (i + 1) + separation_width * i, lineHeight);
path.lineTo(lean_degree + item_width * (i + 1) + separation_width * i, 0);
canvas.clipPath(path);
if (i % 2 == 0) {
canvas.drawColor(first_color);
} else {
canvas.drawColor(second_color);
}
canvas.restore();
}
}
}
initAttr 只是單純讀取屬性值,重點在 onDraw。要間隔繪製多個平行四邊形,得先算出總共需要幾塊:把「一個色塊 + 一段間距」視為一個單位,用整體寬度除以它再進位,就是數量。
真正的難點是四個端點的定位,這裡用歸納法說明:
當 i = 0,左上角是(lean_degree, 0),左下角是(0, line_height),右上與右下各自加上 item_width。
當 i = 1,左上角要在前一個的基礎上再加一組「色塊 + 間距」,也就是(lean_degree + (item_width + separation_width) × 1, 0),其餘三點同理。
推廣到 i = i,四點分別為:
(lean_degree + (item_width + separation_width) × i, 0)
((item_width + separation_width) × i, lineHeight)
(item_width × (i + 1) + separation_width × i, lineHeight)
(lean_degree + item_width × (i + 1) + separation_width × i, 0)
最後依序數的奇偶決定要填哪一色,兩色交錯的效果就完成了。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="https://schemas.android.com/apk/res-auto" android:orientation="vertical" android:gravity="center" tools:context="com.example.zjyang.statubardemo.MainActivity"> <com.example.zjyang.statubardemo.ColourLineView android:layout_width="match_parent" android:layout_height="5dp" android:background="#fff" app:first_color="@color/colorAccent" app:second_color="@color/colorPrimary" app:item_width="15dp" /> </LinearLayout>
高度 5dp、單一色塊寬 15dp、白底、兩色相間,成果如下:

百度地圖實戰:添加覆蓋物並實現點擊查看詳情
前面講過百度地圖的定位與基礎操作,這篇接著來解決兩個常見需求:如何往地圖上添加覆蓋物(Marker),以及如何讓點擊覆蓋物後彈出詳情。 一、準備數據模型 添加覆蓋物至少
Android 網路框架設計實踐:從 Request 抽象到文件上傳
本文從零設計一個輕量 Android 網路框架,重點不在代碼行數,而在分層思路:請求如何抽象、佇列如何調度、結果如何回到主線程、文件上傳如何封裝。 一、構建思路 定
實時監聽手機旋轉角度,判斷橫豎屏佈局的加載時機
場景是這樣的 近期做橫豎屏切換時遇到一個尷尬:佈局的切換由操作系統感知,開發者無法直接得知 Activity 究竟在哪一刻加載了橫屏佈局、哪一那一刻加載了豎屏佈局。既然拿
Android 實現向右滑動返回上一頁的功能(含 Demo)
在 iOS 7 中,蘋果的原生應用幾乎都能透過向右滑動來返回上一個頁面,如此便能避免使用者單手操作時,還得用大拇指去點擊那個遙遠的返回鍵(iPhone 5 的這種返回設計