在ImageView中添加发光/阴影效果

7

我想要实现的目标:

输入图像描述

我能够实现的:

输入图像描述

代码:

<de.hdodenhof.circleimageview.CircleImageView
        android:padding="5dp"
        android:background="@drawable/sub_cat_background"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/tab_image"
        android:src="@drawable/mehendi_tab"
        android:layout_gravity="center_horizontal"/>

sub_cat_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:innerRadius="0dp"
       android:shape="ring"
       android:thicknessRatio="2"
       android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="5dp"
        android:color="@color/white" />
</shape>

这是我在King of masses建议后得到的结果:
现在,我要如何将灰色环变成像上图中展示的阴影效果? enter image description here 编辑4: 我也尝试过使用canvas。
因此,我不是用xml来设置白色环,而是使用带有白色圆圈的图片,如上所示(图2)。
 Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.salon_selected);

            int imageMaxSize = Math.max(bitmap.getWidth(), bitmap.getHeight());


            RadialGradient gradient = new RadialGradient(imageMaxSize / 2, imageMaxSize / 2, imageMaxSize / 2,
                    new int[] {0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF},
                    new float[] {0.0f, 0.8f, 1.0f},
                    android.graphics.Shader.TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setShader(gradient);


            Canvas canvas=new Canvas();
// in onDraw(Canvas)
            canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint);

            tabImage.setImageBitmap(bitmap);

但是我没有得到任何效果,代码源码 (如何在Android中实现羽化效果?)

2个回答

0
通过调整这些属性,您可以获得辉光和阴影效果。
android:shadowColor
android:shadowDx
android:shadowDy
android:shadowRadius

如果您需要以编程方式实现相同的效果,Android 提供了以下与阴影相关的 API。

public void setShadowLayer (float radius, float dx, float dy, int color)

所以,我尝试了一些不同的字体、阴影设置、颜色和透明度设置。

这是一个结果 image

你也可以对任何视图进行此操作,例如imageview、button、textview等。

参考源代码


6
这些功能仅适用于TextView,而不是ImageView。 - Ankur_009

-1
在Android Studio中,有一个内置的drawable可以用于为任何视图应用阴影。这类似于投影阴影。
android:background="@drawable/abc_menu_dropdown_panel_holo_light"

使用此方法无法更改视图的背景颜色和边框颜色。如果您想要自定义绘制,则请使用层列表(layer-list)。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--the shadow comes from here-->
    <item
        android:bottom="0dp"
        android:drawable="@android:drawable/dialog_holo_light_frame"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">

    </item>

    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
        <!--whatever you want in the background, here i preferred solid white -->
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />

        </shape>
    </item>
</layer-list>

并在您的视图中按照以下方式应用

android:background="@drawable/custom_drop_shadow_drawable"

如果这个不起作用,你可以尝试使用ShadowImageView之类的东西。

我无法弄清楚阴影部分应该放什么。 - Ankur_009
以同样的方式创建XML并将其设置为背景,从我的答案中创建并检查。 - King of Masses
或者最好您可以参考一些文档或文章,以便我更好地理解外阴影的添加方式。 - Ankur_009
我在我的回答中提供了一个链接。你可以在那里查看它。 - King of Masses
请查看我以前类似问题的答案 https://dev59.com/0Gcs5IYBdhLWcg3wPxrN#35914091 - King of Masses
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接