简体   繁体   English

在ImageView中添加辉光/阴影

[英]Add glow/shadow in ImageView

What I am trying to achieve: 我想要达到的目标:

在此处输入图片说明

What I am able to achieve: 我能达到的目标:

在此处输入图片说明

Code: 码:

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

This is what I am able to get after King of masses suggestions: 这是我得到群众之王建议的能力:

Now How would I change the grey ring to shadow effect as shown in above figure 现在如何将灰色环变为阴影效果,如上图所示 在此处输入图片说明

Edit 4: 编辑4:

I tried with the canvas way also. 我也尝试了画布方式。

For this,So instead of setting the white ring with the xml, I am using the image with white circle as shown above(image 2). 为此,我没有使用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);

But I didn't get any effect, Code source ( How to achieve feathering effect in Android? ) 但是我没有得到任何效果,代码源( 如何在Android中实现羽化效果?

In android studio there is in build drawable that you can use for apply shadow to any View. 在android studio中,有一个可绘制的构建,可用于将阴影应用于任何视图。 That is similar like a drop shadow. 这就像一个投影一样。

android:background="@drawable/abc_menu_dropdown_panel_holo_light"

Using this you can not change the background color of the view & its border color. 使用此方法,您无法更改视图的背景颜色及其边框颜色。 If you want your own custom drawable then use 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>

and apply to your view like below 并适用于您的视图,如下所示

android:background="@drawable/custom_drop_shadow_drawable"

If this not work you can try something like this with ShadowImageView 如果这不起作用,您可以使用ShadowImageView尝试类似的操作

By playing with these attributes you can get glow and shadow effects 通过使用这些属性,您可以获得光晕和阴影效果

android:shadowColor
android:shadowDx
android:shadowDy
android:shadowRadius

Should you need to achieve the same effect programatically. 您是否需要以编程方式实现相同的效果。 Android provides the following shadow related API. Android提供了以下与阴影相关的API。

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

So, I tried it with some different fonts, shadow settings, colors and transparency settings. 因此,我尝试了一些不同的字体,阴影设置,颜色和透明度设置。

here is a results image 这是结果图像

you can also do this with any view ie imageview, button, textview etc. 您还可以使用任何视图(例如imageview,button,textview等)执行此操作

source code for reference 供参考的源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM