con*_*are 33 xml android shadow dropshadow imageview
我已经做了一些广泛的代码示例搜索,但找不到任何东西.
特别是,我希望为我在ImageView中使用的png drawable添加阴影.这个png drawable是一个带有透明角的圆角矩形.
有人可以提供一个代码示例,说明如何在代码或XML中为视图添加一个不错的投影?
Jos*_*osh 32
您可以使用Bitmap.extractAlpha和BlurMaskFilter的组合为您需要显示的任何图像手动创建投影,但这仅在您的图像偶尔加载/显示一次时才有效,因为该过程很昂贵.
伪代码(甚至可能编译!):
BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(blurFilter);
int[] offsetXY = new int[2];
Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);
/* Might need to convert shadowImage from 8-bit to ARGB here, can't remember. */
Canvas c = new Canvas(shadowImage);
c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);
Run Code Online (Sandbox Code Playgroud)
然后将shadowImage放入ImageView.如果此图像永远不会改变但显示很多,您可以创建它并将其缓存在onCreate中以绕过昂贵的图像处理.
即使这不起作用,它应该足以让你朝着正确的方向前进.
om2*_*345 27
对于Drop shadow使用下面的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#ffffff"
android:centerColor="#d3d7cf"
android:endColor="#2e3436"
android:angle="90" />
</shape>
Run Code Online (Sandbox Code Playgroud)
使用上面的drawable作为视图的背景
<View
android:id="@+id/divider"
android:background="@drawable/black_white_gradient"
android:layout_width="match_parent"
android:layout_height="10sp"
android:layout_below="@+id/buildingsList"/>
Run Code Online (Sandbox Code Playgroud)
小智 10
这有助于我让影子工作,所以我想分享工作代码:
private Bitmap createShadowBitmap(Bitmap originalBitmap) {
BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(blurFilter);
int[] offsetXY = new int[2];
Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);
/* Need to convert shadowImage from 8-bit to ARGB here. */
Bitmap shadowImage32 = shadowImage.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(shadowImage32);
c.drawBitmap(originalBitmap, -offsetXY[0], -offsetXY[1], null);
return shadowImage32;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66446 次 |
| 最近记录: |