【问题标题】:Add glow/shadow in ImageView在 ImageView 中添加发光/阴影
【发布时间】:2018-09-06 12:40:08
【问题描述】:

我想要达到的目标:

我能够实现的目标:

代码:

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

这是我在群众之王建议后能够得到的:

现在如何将灰色环更改为阴影效果,如上图所示

编辑 4:

我也尝试过画布方式。

为此,我没有使用 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);

但是我没有得到任何效果,代码源(How to achieve feathering effect in Android?

【问题讨论】:

    标签: android animation imageview glow


    【解决方案1】:

    通过使用这些属性,您可以获得发光和阴影效果

    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 等。

    source code for reference

    【讨论】:

    • 这些功能仅适用于 Textview 而不是 Image 视图
    【解决方案2】:

    在 android studio 中有一个 build drawable,您可以使用它来将阴影应用到任何 View。这类似于投影。

    android:background="@drawable/abc_menu_dropdown_panel_holo_light"
    

    使用这个你不能改变视图的背景颜色和它的边框颜色。如果您想要自己的自定义可绘制对象,请使用图层列表

    <?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 尝试类似的操作

    【讨论】:

    • 我不知道应该在阴影部分放什么。
    • 以与您创建 xml 并作为背景相同的方式,从我的答案中创建并检查
    • 我试过了,但我无法找出阴影的代码。基本上我没有得到影子@King 的代码。
    • 或者您最好参考一些文档或文章,以便我更好地了解外阴影将如何添加。
    • 我在回答中提供了一个链接。你在那里检查它
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2013-07-13
    相关资源
    最近更新 更多