Android 图像视图阴影状材质设计

Android 图像视图阴影状材质设计,android,android-imageview,material-design,Android,Android Imageview,Material Design,我知道有很多问题,但对我来说并不可行,因此我发布了以下问题: 我想在我的应用程序中添加一个圆形的Imageview,我想给它添加一些提升/阴影,这样它不仅可以在21岁以后在所有Api上工作。那我该怎么办 这是我的代码: xml: 请帮帮我。在android studio中工作的i ma在API 21之前无法工作,但您可以借助阴影进行工作。在mybackground中创建一个可自行绘制的阴影(在您的示例中为圆形)。使用层列表作为父元素,并使用渐变在其中创建阴影层 <?xml version=

我知道有很多问题,但对我来说并不可行,因此我发布了以下问题:

我想在我的应用程序中添加一个圆形的Imageview,我想给它添加一些提升/阴影,这样它不仅可以在21岁以后在所有Api上工作。那我该怎么办

这是我的代码:

xml:


请帮帮我。在android studio中工作的i ma在API 21之前无法工作,但您可以借助阴影进行工作。在mybackground中创建一个可自行绘制的阴影(在您的示例中为圆形)。使用层列表作为父元素,并使用渐变在其中创建阴影层

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <gradient android:startColor="#d1c4e9"
                android:endColor="@android:color/transparent"
                android:angle="270"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="3dp">
        <shape android:shape="oval">
            <solid android:color="#673ab7"/>
        </shape>
    </item>
</layer-list>

在API 21之前,立面无法工作,但您可以借助阴影进行调整。在mybackground中创建一个可自行绘制的阴影(在您的示例中为圆形)。使用层列表作为父元素,并使用渐变在其中创建阴影层

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <gradient android:startColor="#d1c4e9"
                android:endColor="@android:color/transparent"
                android:angle="270"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="3dp">
        <shape android:shape="oval">
            <solid android:color="#673ab7"/>
        </shape>
    </item>
</layer-list>

我们可以在CardView内拍摄ImageView,并且高度和宽度与CardView匹配
尝试一次,它会工作。

我们可以在CardView内拍摄ImageView,并且高度和宽度与CardView匹配
再试一次,就可以了。

请给出一个代码,我在我的背景中添加了阴影,但它没有显示出来。我用的是moto g第一代和棒棒糖,所以它应该显示海拔高度。它也没有显示出这一点。这是一条评论,不是回答,感谢你的帮助,但是代码或链接会更有用。添加代码,在你的res文件夹中绘制,并在ImageView上添加xml作为背景。我在ImageView后面得到一个胚珠阴影,它是水平椭圆,末端颜色为透明的渐变,这就是为什么它看起来像一个水平的椭圆形。根据需要更改颜色。请给出代码,我在我的背景中添加了阴影,但没有显示。我用的是moto g第一代和棒棒糖,所以它应该显示海拔高度。它也没有显示出这一点。这是一条评论,不是回答,感谢你的帮助,但是代码或链接会更有用。添加代码,在你的res文件夹中绘制,并在ImageView上添加xml作为背景。我在ImageView后面得到一个胚珠阴影,它是水平椭圆,末端颜色为透明的渐变,这就是为什么它看起来像一个水平的椭圆形。根据需要改变颜色。
public class MainActivity extends AppCompatActivity {
Bitmap bmp;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.frames);
        bmp = getroundedBitmap(bmp);
        ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bmp);
       //iv.setBackground(new BitmapDrawable(getResources(),bmp));
        Outline outline = null;
       /* if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
         //   outline = new Outline();
          //  outline.setOval(0,0,iv.getWidth(),iv.getHeight());
          iv.setElevation(1500);

        }*/

    }

    private Bitmap getroundedBitmap(Bitmap bmp) {
        int targetwidth = 250;
        int targetheight = 250;
        Bitmap targetBitmap = Bitmap.createBitmap(targetwidth,targetheight,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetwidth - 1) / 2, ((float) targetheight - 1) / 2, Math.min(targetwidth, targetheight) / 2, Path.Direction.CCW);
        canvas.clipPath(path);

        Paint p = new Paint();
        p.setColor(Color.WHITE);
        canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getHeight()), new Rect(0, 0, targetwidth, targetheight), null);

        return targetBitmap;
    }


}
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <gradient android:startColor="#d1c4e9"
                android:endColor="@android:color/transparent"
                android:angle="270"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="3dp">
        <shape android:shape="oval">
            <solid android:color="#673ab7"/>
        </shape>
    </item>
</layer-list>