Android 向 ImageView 和 Drop-Shadow 添加框架或边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10795550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:00:06  来源:igfitidea点击:

Add Frame or Border to ImageView and Drop-Shadow

androidandroid-layout

提问by Shrikant

What I'm trying to do will work better with an example image. As you can see below I have a grey background, ontop of that sits a container with some padding containing an image. The container also has a slight dropshadow to it.

我正在尝试做的事情会更好地与示例图像一起使用。正如你在下面看到的,我有一个灰色的背景,上面是一个容器,里面有一些包含图像的填充。容器也有轻微的阴影。

What I want to know, is if there's so non-painstaking way of doing this in my layout.xml? In a normal HTML document this would've been easy. But since this is for a mobile app and for a number of screen resolutions and so on, it's proving a bit difficult.

我想知道的是,在我的 layout.xml 中是否有这么简单的方法?在普通的 HTML 文档中,这很容易。但由于这是针对移动应用程序和许多屏幕分辨率等,事实证明这有点困难。

Any advice?

有什么建议吗?

enter image description here

在此处输入图片说明

Edit: I eventually settled using a 9patch image. Everyting went really smooth in the creation of it but when I actually use it in my app I see these dark stripes on the right and bottom of the image. The dropshadow seems to work, it's a very light dropshadow.. but those darn stripes??

编辑:我最终使用 9patch 图像解决了问题。一切在创建过程中都非常顺利,但是当我在我的应用程序中实际使用它时,我会在图像的右侧和底部看到这些深色条纹。阴影似乎有效,它是一个非常轻的阴影……但是那些该死的条纹??

enter image description here

在此处输入图片说明

采纳答案by orchidrudra

This can be done with proper padding and 9 patch image. See this link, may be it can help you.

这可以通过适当的填充和 9 个补丁图像来完成。看到这个链接,也许它可以帮助你。

回答by Shrikant

You can provide a border to a view by writing an xml file (say editBorder.xml) in drawable folder:

您可以通过在 drawable 文件夹中编写一个 xml 文件(例如 editBorder.xml)来为视图提供边框:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke 
        android:width="5dp"
        android:color="#EBDDE2" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="4dp" />

    <gradient
        android:centerColor="@color/white" 
        android:endColor="@color/white"
        android:startColor="@color/white" />

    <corners android:radius="8dp" />

</shape>

and to provide this border, use statement in ImageView as android:background="@drawable/editBorder"

并提供此边框,在 ImageView 中使用语句作为 android:background="@drawable/editBorder"

This should solve your problem. :)

这应该可以解决您的问题。:)

回答by Mohammed Azharuddin Shaikh

ImageView having two property android:backgroundand android:src

ImageView 有两个属性android:backgroundandroid:src

http://developer.android.com/reference/android/widget/ImageView.html

http://developer.android.com/reference/android/widget/ImageView.html

Create a blank white frame with drop shadow(Photoshop recommended).

创建一个带有阴影的空白白框(推荐使用 Photoshop)。

So just do this

所以就这样做

<ImageView android:id="@+id/imageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/image"
     android:background="@drawable/whiteFrame" 
     android:padding="10dp" >
</ImageView>

回答by Thi?n Kopites

I found this imageView. It's may good for you

我找到了这个imageView。这可能对你有好处

enter image description here

在此处输入图片说明