Skip to content

Instantly share code, notes, and snippets.

@lovelycateyes
Created December 23, 2011 04:56
Show Gist options
  • Save lovelycateyes/1513185 to your computer and use it in GitHub Desktop.
Save lovelycateyes/1513185 to your computer and use it in GitHub Desktop.
Reflection Effect
public class BitmapHelper {
//gapRatio:間距的比例
//reflectRatio:鏡像的比例
public static Bitmap reflection(Bitmap src, float gapRatio, float reflectRatio) {
int origWidth = src.getWidth();
int origHeight = src.getHeight();
int gapHeight = Float.valueOf(origHeight * gapRatio).intValue();
int reflectHeight = Float.valueOf(origHeight * reflectRatio).intValue();
//統一色彩定義,讓圖片色盤不會跑掉。不然jpg沒有alpha channel會很怪。
//畫上原本的圖像
Bitmap result = Bitmap.createBitmap(origWidth, origHeight + gapHeight + reflectHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
//畫上鏡射的圖
Matrix matrix = new Matrix();
matrix.postScale(1, -1);
Bitmap reflect = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
canvas.drawBitmap(reflect, 0, origHeight + gapHeight, null);
reflect.recycle();
//替鏡射的圖加上漸透明的漸層
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, origHeight, 0, result.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, origHeight, origWidth, result.getHeight(), paint);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment