[英]How to apply shadow to ImageView?
我想对ImageView
应用阴影。 当我将阴影应用于TextView
,我得到了它,但同样它没有达到ImageView
。 我怎么解决这个问题?
我们还可以使用CardView提供圆角背景和阴影。 要使用它,您需要将v7 CardView 库作为依赖项添加到build.gradle中的项目,如下所示。
dependencies {
compile 'com.android.support:cardview-v7:23.0.1'
-------
}
注意:将上面一行中的
23.0.1
更改为受尊重的版本。
所以我用CardView
包围了ImageView
以制作如下所示的阴影。
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@android:color/white">
<ImageView
android:id="@+id/dish_image"
android:layout_width="match_parent"
android:layout_height="120dp"
android:adjustViewBounds="true" />
</android.support.v7.widget.CardView>
它会在图像周围添加阴影。
注意:我不知道这是否是一个好的解决方法。 我是初学者。 我试图实现
CardView
,它给出了为此实现相同的想法。 如果它不好,请告诉我原因。
这摘自 Romain Guy 在 Devoxx 的演讲,pdf 在这里找到。
Paint mShadow = new Paint();
// radius=10, y-offset=2, color=black
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
// in onDraw(Canvas)
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
希望这可以帮助。
笔记
不要忘记蜂窝及以上你需要调用setLayerType(LAYER_TYPE_SOFTWARE, mShadow)
,否则你将看不到你的影子! (@Dmitriy_Boichenko)
不幸的是, SetShadowLayer
不适用于硬件加速,因此它大大降低了性能(@Matt Wear)
答案摘自这里
对于大于 21 的 Api。您可以在 imageview 或 Button 中尝试在 xml 中: 在开发人员站点阅读此处
android:elevation="5dp"
创建文件 shadow_rect.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<corners android:radius="0dp"/>
</shape>
</item>
<item android:right="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="1dp"/>
</shape>
</item>
</layer-list>
并将其用作android:background="@drawable/shadow_rect在您的 Imageview 中。
您可以创建一个可绘制图层列表,并将您的两个项目(可绘制对象)放入其中以用于图像和阴影。
您的阴影项目的位置和内容可能会根据您要应用阴影的位置(顶部、左侧、右侧、右侧和左侧等)和阴影样式而变化。
图像视图
<ImageView
......
android:elevation="2dp"
android:background="@drawable/myrect"/>
背景可绘制对象定义为带圆角的矩形:
<!-- res/drawable/myrect.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>
你可以使用outlineSpotShadowColor
将阴影设为true尝试setOutlineSpotShadowColor
和@color/black
并将elevation
增加到 12dp
使用@android:color/transparent
将阴影设置为false setOutlineSpotShadowColor
并降低elevation
CardView
是完美的解决方案,但如果您不想使用,则可以使用 XML 层。 在这里,我在图像的第 4 面应用阴影。
drawable/bg_shadow.xml:如果你需要更宽的边框,那么你可以更改/删除 android 的值android:right android:left android:top android:bottom
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="#e5e5e5" />
<corners android:radius="4dp"/>
</shape>
</item>
<item android:right=".5dp" android:left=".5dp" android:top=".5dp" android:bottom=".5dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="4dp"/>
</shape>
</item>
</layer-list>
在布局上,你必须用它作为背景, android:elevation
会给出一个浅色风格。 为避免裁剪,您可以使用android:layout_margin
。
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:elevation="2dp"
android:layout_margin="2dp"
android:background="@drawable/bg_shadow"
android:src="@drawable/ic_image" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.