編輯:關於 Android 程式設計
本文的目標是用 Matrix 搭配漸變色,做出圖片的倒影效果。思路可以拆成三步:
1. 取得需要倒影的原圖,這裡只取原圖的下半部分作為倒影素材。
2. 用 Matrix 做 Y 軸翻轉,讓素材上下顛倒。
3. 在倒影圖片上疊一層從不透明到透明的線性漸變,模擬真實倒影隨距離衰減的視覺。
下面以自定義 View 的形式給出完整實現:
package com.flection.view;
import com.flection.main.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;
public class FlectionView extends View {
Context mContext = null;
public FlectionView(Context context) {
super(context);
}
public FlectionView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
//設置背景色
this.setBackgroundColor(Color.parseColor("#8B8378"));
Bitmap oldBitmap = BitmapFactory.decodeResource(
mContext.getResources(), R.drawable.dropbox);
Bitmap newBitmap = createFlectionBitmap(oldBitmap);
canvas.drawBitmap(newBitmap, newBitmap.getWidth(), newBitmap.getHeight(), new Paint());
this.invalidate();
}
/** 獲取原圖 + 倒影圖的 bitmap */
private Bitmap createFlectionBitmap(Bitmap oldBitmap) {
int mWidth = oldBitmap.getWidth();
int mHeight = oldBitmap.getHeight();
//原圖和倒影圖之間的縫隙
int gap = 2;
Matrix matrix = new Matrix();
//Y 軸翻轉
matrix.preScale(1, -1);
//只取下半部分並翻轉,得到倒影素材
Bitmap flection = Bitmap.createBitmap(oldBitmap, 0, mHeight / 2,
mWidth, mHeight / 2, matrix, false);
//創建足夠高的畫布容納原圖與倒影
Bitmap background = Bitmap.createBitmap(
mWidth, mHeight + gap + mHeight / 2, Config.ARGB_8888);
Canvas canvas = new Canvas(background);
Paint p1 = new Paint();
//畫出原圖
canvas.drawBitmap(oldBitmap, 0, 0, p1);
//畫出倒影圖
canvas.drawBitmap(flection, 0, mHeight + gap, p1);
//漸變遮罩:從半透明白過渡到全透明
Paint shaderPaint = new Paint();
LinearGradient shader = new LinearGradient(0, mHeight, 0, flection.getHeight(),
0x70ffffff, 0x00ffffff, TileMode.MIRROR);
shaderPaint.setShader(shader);
//DST_IN 模式讓漸變只作用於已繪製的倒影區域
shaderPaint.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(0, mHeight + gap, mWidth, background.getHeight(), shaderPaint);
return background;
}
}
效果圖:

三個技術點值得單獨強調一下。Matrix.preScale(1, -1) 是翻轉的關鍵,Y 軸取負即上下顛倒。PorterDuffXfermode.Mode.DST_IN 的作用是「保留目標圖像與來源圖像交集,並按來源圖像的透明度衰減」,因此漸變畫上去之後,只會在倒影區域生效而不會污染整張畫布。最後要提醒的是:onDraw 中頻繁 decodeResource 與 createBitmap 是非常重的操作,實務上應把這些位圖計算提前到構造方法或異步任務中完成,避免掉幀。
ImageView 屬性 android:scaleType 的作用分析
網路上關於 scaleType 的說明大多是互相轉抄,讀起來反而更糊塗。以下依據 API 文件的定義逐一整理,並用同一張測試圖說明各值的實際差異。 基本用法 <I
Android 訊息推送的六種方案比較與選型建議
推送已經是行動 App 的標配。新聞客戶端靠它即時送達頭條,社群軟體靠它提醒新訊息。但同一套技術也可以被拿來狂推廣告,這就讓使用者頭痛了。撇開這個爭議,本文想談的是更實際
Android 外賣購物車實作全解:懸停、聯動、拋物線動畫與資料同步
又有兩週沒寫 Blog 了。不是不想寫,是真的不知道寫什麼,總不能為了更新而更新。前幾天專案裡要加購物車,試看了幾家 App 的效果覺得做得不錯,雖然這次自己沒用上,但畢
用 TabLayout 打造仿新聞 App 的頁籤效果(Design 支持庫實戰)
前言 在仿做 36Kr 客戶端的過程中,因為對方的新聞分類很多,所以我決定參考網易新聞 App 首頁的做法:頂部一條可滑動的新聞頁籤,搭配下方跟著一起換頁的內容區。 實作