android 如何将阴影应用到ImageView?

eivnm1vs  于 2023-01-11  发布在  Android
关注(0)|答案(7)|浏览(173)

我想对ImageView应用阴影。当我对TextView应用阴影时,我得到了它,但同样它没有得到ImageView。我该如何解决这个问题?

brtdzjyr

brtdzjyr1#

我们也可以使用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,这给了一个想法,以实现同样的这一点。如果它是不好的,请更新我的原因。

vkc1a9a2

vkc1a9a22#

这是从罗曼盖伊的演示文稿在Devoxx,pdf找到here

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

希望这个有用。

    • 注**

别忘了对于Honeycomb和更高版本,你需要调用setLayerType(LAYER_TYPE_SOFTWARE, mShadow),否则你将看不到你的影子!(@Dmitriy_Boichenko)
不幸的是,SetShadowLayer无法与硬件加速配合使用,因此大大降低了性能(@Matt Wear)
答案取自Here
对于大于21的Api,您可以尝试在imageview或Button中使用xml:Read here at developer site

android:elevation="5dp"
cbjzeqam

cbjzeqam3#

创建文件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>

并在Imageview中将其用作android:background="@drawable/shadow_rect

sr4lhrrt

sr4lhrrt4#

图像视图

<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>
vhipe2zx

vhipe2zx5#

你可以创建一个图层列表,并把你的两个项目(绘图)在它的图像和你的阴影。
您的阴影项目的位置和内容可能会根据您想要应用阴影的位置(顶部,左侧,右侧,左右两侧等)和阴影的样式而改变。

p1tboqfb

p1tboqfb6#

可以使用属性outlineSpotShadowColor
要将阴影设置为true,请尝试setOutlineSpotShadowColor@color/black,并将elevation增加到12dp
将阴影设置为falsesetOutlineSpotShadowColor@android:color/transparent并减小elevation

i2byvkas

i2byvkas7#

CardView是完美的解决方案,但如果你不想使用,那么你可以使用XML层。这里我应用阴影的图像第四侧。

  • drawable/bg_shadow.xml:* 如果您需要更宽的边框,则可以更改/删除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" />

相关问题