2015-06-17 28 views
1

我需要爲圖像創建陰影效果。所有側面的圖像的陰影效果

private static Bitmap getDropShadow3(Bitmap bitmap) { 

if (bitmap==null) return null; 
int think = 6; 
int w = bitmap.getWidth(); 
int h = bitmap.getHeight(); 

int newW = w - (think); 
int newH = h - (think); 

Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
Bitmap bmp = Bitmap.createBitmap(w, h, conf); 
Bitmap sbmp = Bitmap.createScaledBitmap(bitmap, newW, newH, false); 

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
Canvas c = new Canvas(bmp); 

// Right 
Shader rshader = new LinearGradient(newW, 0, w, 0, Color.GRAY, Color.LTGRAY, Shader.TileMode.CLAMP); 
paint.setShader(rshader); 
c.drawRect(newW, think, w, newH, paint); 

// Bottom 
Shader bshader = new LinearGradient(0, newH, 0, h, Color.GRAY, Color.LTGRAY, Shader.TileMode.CLAMP); 
paint.setShader(bshader); 
c.drawRect(think, newH, newW , h, paint); 

//Corner 
Shader cchader = new LinearGradient(0, newH, 0, h, Color.LTGRAY, Color.LTGRAY, Shader.TileMode.CLAMP); 
paint.setShader(cchader); 
c.drawRect(newW, newH, w , h, paint); 
c.drawBitmap(sbmp, 0, 0, null); 
return bmp; 
} 

我用上面的代碼,我得到兩面(右,底)陰影效果。我怎樣才能使所有方面的影響,包括(頂部,左)?

回答

0

您正在使用的方法;通過

public Bitmap addShadowToBitmap(final Bitmap bm, final int dstHeight, final int dstWidth, int color, int size, float dx, float dy) { 
    final Bitmap mask = Bitmap.createBitmap(dstWidth, dstHeight, Config.ALPHA_8); 

    final Matrix scaleToFit = new Matrix(); 
    final RectF src = new RectF(0, 0, bm.getWidth(), bm.getHeight()); 
    final RectF dst = new RectF(0, 0, dstWidth - dx, dstHeight - dy); 
    scaleToFit.setRectToRect(src, dst, ScaleToFit.CENTER); 

    final Matrix dropShadow = new Matrix(scaleToFit); 
    dropShadow.postTranslate(dx, dy); 

    final Canvas maskCanvas = new Canvas(mask); 
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    maskCanvas.drawBitmap(bm, scaleToFit, paint); 
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
    maskCanvas.drawBitmap(bm, dropShadow, paint); 

    final BlurMaskFilter filter = new BlurMaskFilter(size, Blur.NORMAL); 
    paint.reset(); 
    paint.setAntiAlias(true); 
    paint.setColor(color); 
    paint.setMaskFilter(filter); 
    paint.setFilterBitmap(true); 

    final Bitmap ret = Bitmap.createBitmap(dstWidth, dstHeight, Config.ARGB_8888); 
    final Canvas retCanvas = new Canvas(ret); 
    retCanvas.drawBitmap(mask, 0, 0, paint); 
    retCanvas.drawBitmap(bm, scaleToFit, null); 
    mask.recycle(); 
    return ret; 
} 

調用它:可能會導致一些問題,某些類型的圖像(不規則的)

試試這個方法,而不是(多更容易理解和更靈活)

addShadowToBitmap(arg0, arg1, arg2, arg3, arg4, arg5, arg6); 

,它會返回給您一個bitmap

希望這會有所幫助。