2016-01-09 188 views
1

我想向ImageView(右側和底側)添加陰影。
下面我張貼了2張帶陰影效果的圖片。
我想要相同的陰影效果。向ImageView添加陰影

如何將這個陰影效果應用到我的ImageView?

與右側圖像陰影
enter image description here

圖像與右側和底部的陰影
enter image description here

+0

http://stackoverflow.com/questions/3693234/custom-imageview-with-drop-shadow – Vyacheslav

+0

任何其他方式上面我提到,如XML @Vyacheslav – YUVRAJ

+0

我不知道創建這個影子,但也許使用卡片視圖幫助你 – Ashkan

回答

6

設置填充大小右邊和底部和背景設置你的影子

<ImageView 
    android:id="@+id/imageview" 
    android:background="@drawable/drop_shadow" 
    <!--android:background="#660000"--> This breaks the syntax highlight 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
    android:paddingRight="10px" 
    android:paddingBottom="10px" 
    android:src="@drawable/pic1" 
/> 
+0

什麼背景drop_shadow @sasikumar – YUVRAJ

+0

你也可以使用顏色也android:background =「#660000」 – sasikumar

+0

9補丁會給你更柔和的陰影。 –

4

嘗試使用位圖

public Bitmap imgshadow(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; 
    } 



final Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
    final Bitmap shadows = imgshadow(src, src.getHeight(), src.getWidth(), Color.BLACK, 3, 1, 3); 
    final ImageView iv = (ImageView)findViewById(R.id.imageview1); 
    iv.setImageBitmap(shadows);