android中bitmap缩放和阴影效果

1.缩放

/**

* zoom bitmap to certain size

*

* @param bitmap

* @param width

* @param height

* @return

*/

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {

if (bitmap == null) {

return null;

}

int w = bitmap.getWidth();

int h = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidth = ((float) width / w);

float scaleHeight = ((float) height / h);

matrix.postScale(scaleWidth, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

return newbmp;

}

2. 阴影

/**

* add shadow to bitmap

*

* @param originalBitmap

* @return

*/

private Bitmap drawImageDropShadow(Bitmap originalBitmap) {

BlurMaskFilter blurFilter = new BlurMaskFilter(1,

BlurMaskFilter.Blur.NORMAL);

Paint shadowPaint = new Paint();

shadowPaint.setAlpha(50);

shadowPaint.setColor(activity.getResources()

.getColor(R.color.solid_red));

shadowPaint.setMaskFilter(blurFilter);

int[] offsetXY = new int[2];

Bitmap shadowBitmap = originalBitmap

.extractAlpha(shadowPaint, offsetXY);

Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);

Canvas c = new Canvas(shadowImage32);

c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);

return shadowImage32;

}

摘自 fhy_2008的专栏

补充:移动开发 , Android ,

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐