您的当前位置:首页正文

Android实例] 圆角图片加阴影

2022-02-18 来源:伴沃教育


Android 实现圆角图片并添加阴影

此方法可实现圆角图片加阴影背景,先上图看下效果:

主要的代码在MyCoverImageView类里面,具体实现是对bitmap的转换合成过程!

废话少说,下面直接贴代码

1、 工具类

public class MyCoverImageView extends ImageView {

private Bitmap src =null;

public Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

private void init(Context context){

src = BitmapFactory.decodeResource(getResources(), R.drawable.src);

// src = zoomImg(src,300,300);

// src = toRoundCorner(src,30);

}

public MyCoverImageView(Context context) {

super(context);

init(context);

}

public MyCoverImageView(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

public MyCoverImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

}

public void setImageBitmapWithCoverIn(Bitmap bm) {

Bitmap out = toRoundCorner(bm,20);

// int w = bm.getWidth();

// int h = bm.getHeight();

//cover

// Bitmap dd =BitmapFactory.decodeResource(getResources(),

R.drawable.goods_img_bg);

// Bitmap cover = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

// Canvas canvas_cover = new Canvas(cover);

// NinePatch np = new NinePatch(dd, dd.getNinePatchChunk(), null);

// Rect r = new Rect(0,0,w,h);

// np.draw(canvas_cover, r);

// Bitmap out = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

// Canvas canvas_out = new Canvas(out);

// paint.setAntiAlias(false);

// canvas_out.drawBitmap(cover,0, 0, paint); //dst

// paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

// canvas_out.drawBitmap(bm, 0, 0, paint); //src

// try {

// File myCaptureFile = new File(\"/sdcard/test.png\");

// myCaptureFile.deleteOnExit();

// myCaptureFile.createNewFile();

// BufferedOutputStream bos = new BufferedOutputStream(new

FileOutputStream(myCaptureFile));

// out.compress(Bitmap.CompressFormat.PNG,100, bos);

// bos.flush();

// bos.close();

// } catch (FileNotFoundException e) {

// e.printStackTrace();

// } catch (IOException e) {

// e.printStackTrace();

// }

super.setImageBitmap(out);

}

static void setCornerRadii(GradientDrawable drawable, float r0, float r1,

float r2, float r3) {

drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3, r3 });

}

/**

* @param bitmap

* @param pixels

* @return

*/

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

Bitmap output_shadow = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

Rect rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());

RectF rectF= new RectF(rect);

Canvas canvas = new Canvas(output);

// Canvas canvas_shadow = new Canvas(output_shadow);

//这里画一个渐变的图形

canvas.drawColor(0x00000000);

GradientDrawable mDrawable = new

GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF });

mDrawable.setBounds(rect);

mDrawable.setShape(GradientDrawable.RECTANGLE);

mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));

mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);

//这里设置圆角。这里不是四边圆角的,要的话可以自己设置喔.可参加setCornerRadii方法

setCornerRadii(mDrawable,0,pixels,0,pixels);

// paint.setShadowLayer(5f, 5.0f, 5.0f,0xFF909090);

// canvas.saveLayer(rectF, paint, Canvas.ALL_SAVE_FLAG);

mDrawable.draw(canvas);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

paint.setShadowLayer(0, 5.0f, 5.0f,0xFF909090);

canvas.saveLayer(rectF, paint, Canvas.ALL_SAVE_FLAG);

BitmapDrawable imageDrawable = new BitmapDrawable(bitmap);

imageDrawable.setBounds(rect);

imageDrawable.draw(canvas);

canvas.restore();

output_shadow = drawImageDropShadow(output);

output.recycle();

// Paint shadowPaint = new Paint();

//// canvas_shadow.drawColor(0xFF000000);

// shadowPaint.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);

// canvas_shadow.drawBitmap(output,0,0, shadowPaint);

return output_shadow;

}

/**

* 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;

}

/**

* @param originalBitmap

* @return

*/

private static Bitmap drawImageDropShadow(Bitmap originalBitmap) {

float radius = 8f;

float readiusHalf = radius/2;

// EmbossMaskFilter filter = new EmbossMaskFilter(new float[]{ 0, 0, 1 }, 0.1f, 20, 20.0f);

BlurMaskFilter filter = new

BlurMaskFilter(radius,BlurMaskFilter.Blur.NORMAL);

Paint shadowPaint = new Paint();

shadowPaint.setAlpha(10);

shadowPaint.setStyle(Paint.Style.FILL);

shadowPaint.setColor(Color.WHITE);

// shadowPaint.setAntiAlias(true);

shadowPaint.setMaskFilter(filter);

int[] offsetXY = new int[2];

Bitmap shadowBitmap = originalBitmap.extractAlpha(shadowPaint, offsetXY);

Bitmap shadowBitmap32 =

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

Canvas c = new Canvas(shadowBitmap32);

Log.d(\"flydy\

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

shadowBitmap.recycle();

return shadowBitmap32;

}

public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){

int width = bm.getWidth();

int height = bm.getHeight();

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

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

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

return newbm;

}

}

2、使用方法

setContentView(R.layout.activity_main);

MyCoverImageView miv = (MyCoverImageView)findViewById(R.id.iv);

Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.src);

miv.setImageBitmapWithCoverIn(src);

因篇幅问题不能全部显示,请点此查看更多更全内容