編輯:關於 Android 程式設計
前言
專案裡需要加入水印效果。嘗試過幾種做法後,最後選擇以 ItemDecoration 來實現,整個過程可以拆成兩大步:先自訂一個 Drawable 把水印圖案畫出來,再用 ItemDecoration 負責排版與重複鋪設。
Demo 位於 WatermarkFragment 中,效果如下圖:

一、自訂 Drawable 完成水印圖案
public class MyDrawable extends Drawable {
Paint mPaint;
public MyDrawable() {
mPaint = new Paint();
mPaint.setColor(Color.parseColor("#1A000000"));
mPaint.setAntiAlias(true);
mPaint.setTextAlign(Paint.Align.LEFT);// 從文字最左邊開始畫
mPaint.setTextSize(54);
}
@Override
public void draw(@NonNull Canvas canvas) {
Rect r = getBounds();
// 畫上傾斜的文字
canvas.save();
canvas.rotate(-30, r.left, r.bottom);
canvas.drawText("哈哈哈哈哈哈哈", r.left, r.bottom, mPaint);
canvas.restore();
}
/* 覆寫這兩個方法,是為了在控制項使用 wrap_content 時能自行測出寬高,同時也方便後續排版。 */
// 傾斜 30 度,可以據此推算出高度
@Override
public int getIntrinsicHeight() {
return (int) (Math.sqrt(3) / 3 * getIntrinsicWidth() + 0.5F);
}
@Override
public int getIntrinsicWidth() {
return (int) (mPaint.measureText("DecorationDraw") + 0.5F);
}
// ...其餘模板方法省略
}
自訂 Drawable 本身並不難,難的是能否想到這一層。一開始我打算直接在 ItemDecoration 裡邊排版邊繪製,實作起來太過複雜,於是改把繪製邏輯獨立成 Drawable,後續就順利多了。
二、以 ItemDecoration 排版水印
public class MyDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int mScrollY;
public MyDecoration() {
mDivider = new MyDrawable();
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
// 清除先前畫的內容
// canvas.drawColor(Color.WHITE);
/*
* 之所以會跟著滑動,是因為 bounds 的 top 一直在變
*/
int top = UIUtil.dp(20) - mScrollY;
// 要畫幾個水印可依業務需求調整,這裡直接畫到 itemCount 個
int itemCount = parent.getAdapter().getItemCount();
for (int i = 0; i < itemCount; ++i) {
int left = i % 2 == 0 ? UIUtil.dp(20) : parent.getMeasuredWidth() - mDivider.getIntrinsicWidth() - UIUtil.dp(20);
// 透過 setBounds 控制水印的左右位置
mDivider.setBounds(left, top, parent.getMeasuredWidth(), top + mDivider.getIntrinsicHeight());
mDivider.draw(canvas);
if (i % 2 == 0) {
top += UIUtil.dp(20) + mDivider.getIntrinsicHeight();
} else {
top += UIUtil.dp(140) + mDivider.getIntrinsicHeight();
}
}
}
/*
* mScrollY 用於追蹤 RecyclerView 的滑動距離,
* 此處採用 OnScrollListener 中 dy 的累加值,
* 在 item 沒有增刪操作的前提下是準確的。
*/
public void setScrollY(int scrollY) {
this.mScrollY = scrollY;
}
}
三、在 RecyclerView 中掛上裝飾
private int totallyY = 0;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
totallyY += dy;
myDecoration.setScrollY(totallyY);
}
});
結語
寫下來看步驟並不複雜,但最初摸索時確實花了些功夫。RecyclerView 的擴充設計真的很漂亮,ItemDecoration 這種鉤子讓各種自訂繪製都有了落腳點。
以上就是本文的全部內容,希望對大家的學習有所幫助。
Android 單位換算與用程式碼動態修改控制項布局
XML 布局並非萬能。當我們需要依據執行期條件決定控制項的尺寸或位置時,就必須在程式碼中動態調整。要做到這件事,先得把 Android 的各種單位搞清楚。 Android
Android 4.4 KitKat Phone 工作流程淺析(九):狀態通知流程
概要 當手機 Modem 狀態發生變化後,會將狀態變化信息上報到上層。結合《Android 4.4 KitKat Phone 工作流程淺析(八):Phone 狀態分析》與
用 GridView 打造相冊瀏覽,復刻微信的選圖體驗
上傳圖片幾乎是每個專案的標配,選圖流程的順手程度直接影響使用者觀感。最近專案裡正好需要這個功能,順手把實作整理成筆記。先看效果: 整體結構不複雜:兩個 Activity
Android 側滑導覽選單實作(下):跟進 Material Design 的 NavigationView 標準做法
上一篇我們快速堆砌了幾種側滑導覽選單的效果,這一篇則要來探討「當下」該如何依照 Android 官方設計規範來打造側滑選單。之所以強調「當下」,是因為這套設計標準並非一成