public boolean createReflectedImages() {
final int reflectionGap =4;//原圖與倒影之間的間隙
int index =0;
for(Map map : list) {
Integer id = (Integer) map.get("image");
Bitmap originalImage = BitmapFactory.decodeResource(
mContext.getResources(),id);// 獲得圖片資源
// 獲得圖片的長寬
int width = originalImage.getWidth();
int height = originalImage.getHeight();
Matrix matrix =newMatrix();
matrix.preScale(1,-1);// 實現(xiàn)圖片的反轉(zhuǎn)
Bitmap reflectionImage = Bitmap.createBitmap(originalImage,0,
height /2,width,height /2,matrix, false);// 創(chuàng)建反轉(zhuǎn)后的圖片Bitmap對象呈驶,圖片高是原圖的一半
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height /2),Config.ARGB_8888);// 創(chuàng)建標(biāo)準(zhǔn)的Bitmap對象计维,寬和原圖一致,高是原圖的1.5倍
Canvas canvas =newCanvas(bitmapWithReflection);
canvas.drawBitmap(originalImage,0,0, null);// 創(chuàng)建畫布對象含潘,將原圖畫于畫布脖镀,起點是原點位置
Paint paint =newPaint();
canvas.drawRect(0,height,width,height + reflectionGap,paint);
canvas.drawBitmap(reflectionImage,0,height + reflectionGap, null);// 將反轉(zhuǎn)后的圖片畫到畫布中
paint =newPaint();
LinearGradient shader =newLinearGradient(0,
originalImage.getHeight(),0,
bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff,0x00ffffff,TileMode.MIRROR);// 創(chuàng)建線性漸變LinearGradient對象
paint.setShader(shader);// 繪制
paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));//倒影遮罩效果
canvas.drawRect(0,height,width,bitmapWithReflection.getHeight()
+ reflectionGap,paint);// 畫布畫出反轉(zhuǎn)圖片大小區(qū)域飒箭,然后把漸變效果加到其中,就出現(xiàn)了圖片的倒影效果
ImageView imageView =newImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);// 設(shè)置帶倒影的Bitmap
//設(shè)置ImageView的大小蜒灰,可以根據(jù)圖片大小設(shè)置
// imageView.setLayoutParams(newmyGallery.LayoutParams(width,height));
imageView.setLayoutParams(newmyGallery.LayoutParams(250,500));//設(shè)置ImageView的大小弦蹂,可根據(jù)需要設(shè)置固定寬高
imageView.setScaleType(ScaleType.FIT_CENTER);//將圖片按比例縮放
mImages[index++] = imageView;
}
return true;
}