android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼
更新時間:2013年06月14日 10:35:06 作者:
android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼,需要的朋友可以參考一下
[html]
復(fù)制代碼 代碼如下:
/**
* 畫一個圓角圖
*
* @param bitmap
* @param roundPx
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 創(chuàng)建倒影效果
*
* @return
*/
public boolean createReflectedImages() {
// 倒影圖和原圖之間的距離
final int reflectionGap = 4;
int index = 0;
for (GalleryWith3DData imageId : mImageIds) {
// 返回原圖解碼之后的bitmap對象
Bitmap originalImage = BitmapFactory.decodeResource(
mContext.getResources(), imageId.getInteger());
int width = originalImage.getWidth();
int height = originalImage.getHeight();
// 創(chuàng)建矩陣對象
Matrix matrix = new Matrix();
// 指定矩陣(x軸不變,y軸相反)
matrix.preScale(1, -1);
// 將矩陣應(yīng)用到該原圖之中,返回一個寬度不變,高度為原圖1/2的倒影位圖
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);
// 創(chuàng)建一個寬度不變,高度為原圖+倒影圖高度的位圖
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
// 將上面創(chuàng)建的位圖初始化到畫布
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(getRoundedCornerBitmap(originalImage, 20), 0, 0,
null);
int len = imageId.getstr().length();
double lenWeght = len * 50 * 0.9;
int ban = width / 2;
int ban1 = (int) (lenWeght / 2);
int hua = ban - ban1;
if (imageId.getFlagRecommend()) {
canvas.rotate(30);
canvas.drawText(mStrRecommend, hua - 20, 150,
createPaint(Color.RED));
canvas.rotate(-30);
}
Paint deafaultPaint = new Paint();
deafaultPaint.setAntiAlias(false);
canvas.drawBitmap(getRoundedCornerBitmap(reflectionImage, 20), 0,
height + reflectionGap, null);
Paint paint = new Paint();
paint.setAntiAlias(false);
/**
* 參數(shù)一:為漸變起初點(diǎn)坐標(biāo)x位置, 參數(shù)二:為y軸位置, 參數(shù)三和四:分辨對應(yīng)漸變終點(diǎn), 最后參數(shù)為平鋪方式,
* 這里設(shè)置為鏡像Gradient是基于Shader類,所以我們通過Paint的setShader方法來設(shè)置這個漸變
*/
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff, 0x00ffffff, TileMode.MIRROR);
// 設(shè)置陰影
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.DST_IN));
// 用已經(jīng)定義好的畫筆構(gòu)建一個矩形陰影漸變效果
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
canvas.drawText(imageId.getstr(), hua, 430,
createPaint(Color.WHITE));
// 創(chuàng)建一個ImageView用來顯示已經(jīng)畫好的bitmapWithReflection
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);
// 設(shè)置imageView大小 ,也就是最終顯示的圖片大小
imageView.setLayoutParams(new GalleryWith3D.LayoutParams(150, 250));
// imageView.setScaleType(ScaleType.MATRIX);
mImages[index++] = imageView;
}
return true;
}
下面是效果圖:

您可能感興趣的文章:
- android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)動態(tài)向Gallery中添加圖片及倒影與3D效果示例
- Android中使用Matrix控制圖形變換和制作倒影效果的方法
- Android編程滑動效果之倒影效果實(shí)現(xiàn)方法(附demo源碼下載)
- Android應(yīng)用開發(fā)之簡易、大氣音樂播放器實(shí)現(xiàn)專輯倒影效果
- Android 輕松實(shí)現(xiàn)圖片倒影效果實(shí)例代碼
- Android 圖像處理(類型轉(zhuǎn)換,比例縮放,倒影,圓角)的小例子
- Android 倒影算法的實(shí)現(xiàn)代碼
- Android自定義TextView實(shí)現(xiàn)文字傾斜效果
- Android實(shí)現(xiàn)文字翻轉(zhuǎn)動畫的效果
- Android實(shí)現(xiàn)文字和圖片混排(文字環(huán)繞圖片)效果
- Android編程實(shí)現(xiàn)文字倒影效果的方法
相關(guān)文章
android實(shí)現(xiàn)圖片閃爍動畫效果的兩種實(shí)現(xiàn)方式(實(shí)用性高)
本文通過兩種方法給大家講解了android實(shí)現(xiàn)圖片閃爍動畫效果,實(shí)用性非常高,對這兩種方法感興趣的朋友一起通過本文學(xué)習(xí)吧2016-09-09淺析Android手機(jī)衛(wèi)士之號碼歸屬地查詢
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之號碼歸屬地查詢的相關(guān)資料,需要的朋友可以參考下2016-04-04Android APK應(yīng)用安裝原理解析之AndroidManifest使用PackageParser.parserPac
這篇文章主要介紹了Android APK應(yīng)用安裝原理解析之AndroidManifest使用PackageParser.parserPackage原理,結(jié)合實(shí)例形式分析了PackageManagerService調(diào)用PackageParser.parserPackage方法解析APK清單相關(guān)原理與操作技巧,需要的朋友可以參考下2017-12-12Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn)
這篇文章主要介紹了Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn) ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Android樣式的開發(fā):layer-list實(shí)例詳解
本文主要介紹Android樣式開發(fā)layer-list,這里整理了詳細(xì)的資料,及簡單示例代碼有興趣的小伙伴可以參考下2016-09-09