編輯:關於 Android 程式設計
Canvas 的裁剪(clipping)不算難,但 Region.Op 那幾個參數很容易搞混。本文用一張圖與一份完整程式碼把六種操作方式一次講清楚。
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);
前四個參數是裁剪矩形(左、上、右、下),最後一個參數決定「這次的裁剪要如何與現有的裁剪區域組合」。
理解這幾個值的關鍵,是把「第一次設定的裁剪區」當成 A、「第二次設定的裁剪區」當成 B:
DIFFERENCE:顯示 A 有、B 沒有的部分(A − B)REPLACE:直接替換成 B,只顯示第二次的區域REVERSE_DIFFERENCE:顯示 B 有、A 沒有的部分(B − A)INTERSECT:交集,同時屬於 A 與 B 的部分(預設行為)UNION:聯集,A 與 B 的全部範圍XOR:對稱差集,聯集扣掉交集後剩下的部分
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
public class sBook extends View {
Paint mPaint;
Path mPath;
public sBook(Context context) {
super(context);
init();
}
public sBook(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public sBook(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(6);
mPaint.setTextSize(16);
mPaint.setTextAlign(Paint.Align.RIGHT);
mPath = new Path();
}
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GRAY);
// 左上角:只做兩次 clipRect,預設 INTERSECT
canvas.save();
canvas.translate(10, 10);
drawScene(canvas);
canvas.restore();
// 右上:XOR
canvas.save();
canvas.translate(160, 10);
canvas.clipRect(10, 10, 90, 90);
canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);
drawScene(canvas);
canvas.restore();
// 左中:clipPath + REPLACE
canvas.save();
canvas.translate(10, 160);
mPath.reset();
mPath.cubicTo(0, 0, 100, 0, 100, 100);
mPath.cubicTo(100, 100, 0, 100, 0, 0);
canvas.clipPath(mPath, Region.Op.REPLACE);
drawScene(canvas);
canvas.restore();
// 右中:UNION
canvas.save();
canvas.translate(160, 160);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
drawScene(canvas);
canvas.restore();
// 左下:XOR
canvas.save();
canvas.translate(10, 310);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
drawScene(canvas);
canvas.restore();
// 右下:REVERSE_DIFFERENCE
canvas.save();
canvas.translate(160, 310);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);
drawScene(canvas);
canvas.restore();
}
private void drawScene(Canvas canvas) {
canvas.clipRect(0, 0, 100, 100);
canvas.drawColor(Color.WHITE);
mPaint.setColor(Color.RED);
canvas.drawLine(0, 0, 100, 100, mPaint);
mPaint.setColor(Color.GREEN);
canvas.drawCircle(30, 70, 30, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawText("Clipping", 100, 30, mPaint);
}
}
clipRect() 等同於 Region.Op.INTERSECT,也就是每呼叫一次就裁剪得更小。clipPath() 與 clipRegion()。前者可以裁出任意形狀(圓形、自訂曲線),做圓形頭像裁切就靠它。clipPath() 對 Region.Op 的支持有變化。在硬體加速開啟的情況下,非 INTERSECT 的操作行為可能與預期不同,必要時可對該 View 關閉硬體加速(android:hardwareAccelerated="false")。PorterDuffXfermode 去遮,不如直接 clip,能少畫很多不必要的像素。以上就是 Canvas 畫布切割的用法整理,把 save/restore 與 Region.Op 這兩件事搞熟,大部分自訂 View 的繪圖需求都能應付。
Android 漫游記(2)——ELF 可執行檔格式入門
ELF(Executable and Linkable Format)是類 Unix 系統的可執行檔格式,Android 自然也不例外,.so 與 .o 同樣採用這個標準
Android AlertDialog 五種對話框完整實戰:確認、單選、多選、列表與自定義
幾乎每一款 App 都離不開對話框:刪除前的二次確認、性別選擇、興趣勾選、部門挑選,甚至是帶輸入框與圖片的「自定義彈窗」。本文用一個可直接跑起來的示例,把 AlertDi
Android Drawable 與 LayerDrawable 完全解析
先看一個實際需求:做出一個外圈白色描邊、內圈綠色填色、中央再疊一張圖示的圓形按鈕。 用 layer-list 就能乾淨地解決: </layer-list>
Android 逆向分析實例:某酒店 App 登陸請求的加解密還原
為了練手、順便補一下逆向分析的知識,這次挑了一個很典型的題目:分析某酒店 App 的登陸請求。 目標很明確——弄清登陸請求資料的加解密原理,把傳出去的密文還原成明文,順便