简体   繁体   English

Android圆角布局,带有填充的平铺图像

[英]Android Rounded Corner Layout with Filled Tiled Image

I'm hoping there is an xml solution to this... I am trying to create a layout that has rounded corners and then filling that layout with an image (tiled, repeat). 我希望对此有一个xml解决方案...我试图创建一个具有圆角的布局,然后用图像填充该布局(平铺,重复)。 So basically I have a LinearLayout that has a drawble layered-list assigned to the background. 所以基本上我有一个LinearLayout,其中有一个分配给背景的可绘制层列表。 Here is the test layout (xml): 这是测试布局(xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/rate_buttons"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:padding="0dp" 
        android:background="@drawable/test_rounded">
    </LinearLayout>
</LinearLayout>

here is the test_rounded drawable xml: 这是test_rounded可绘制xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item ><bitmap android:src="@drawable/my_tiled_image" android:tileMode="repeat"/></item>
    <item >
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >     
            <stroke android:width="3dip" android:color="#B1BCBE" />
            <corners android:radius="20dip"/>   
        </shape>
    </item>
</layer-list>

The image is overflowing at the corners of the shape/layout border. 图像在形状/布局边框的角处溢出。 I get the following 我得到以下

角落在这里满溢

As you can see the corners are overflowed. 如您所见,角落四溢。 I heard about modifying the image part (blue pattern) in code but I have issues with that since I do modify it and animate it along the way. 我听说过要在代码中修改图像部分(蓝色图案),但是由于我确实对其进行了修改和动画处理,因此我对此有疑问。 SO, if its at all possible to just fill in the layout with that image, please let me know. 所以,如果有可能仅用该图像填充布局,请告诉我。 Thank you all for your help. 谢谢大家的帮助。

You can do this only programatically. 您只能以编程方式执行此操作。 But if you want to do this using xml, you have to add the background image in a layout above the rounded corner layout with the help of Relative layout and set margin. 但是,如果要使用xml进行此操作,则必须在相对布局的帮助下将背景图像添加到圆角布局上方的布局中,并设置边距。 This will not give a perfect solution but will solve your problem. 这不会提供完美的解决方案,但可以解决您的问题。

The first is the XML way Create an xml file in your respective drawable folder for the round border. 第一种是XML方式,在您各自的可绘制文件夹中为圆形边框创建xml文件。 I have namedd it as popupborder.xml. 我已将其命名为popupborder.xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" />
<corners android:radius="15dip" />
</shape>


The main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/popupborder" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" >

<LinearLayout
    android:id="@+id/property_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Inner view with Background and margin"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>

i recommend you to use an image loader library such as universal image loader, picasso etc. Here is an example code for it: 我建议您使用图像加载器库,例如通用图像加载器,毕加索等。这是其示例代码:

String url = "file://" + your_file_path
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, ivPicture, options);

You have to add 您必须添加

.displayer(new RoundedBitmapDisplayer(px))

to your Universal Image Loader display options. 到您的Universal Image Loader显示选项。

Also there is another library (i think this is what you are looking for) https://github.com/vinc3m1/RoundedImageView 另外还有另一个库(我认为这是您正在寻找的) https://github.com/vinc3m1/RoundedImageView

You can check this. 您可以检查一下。 But in second way you can get "Out of Memory Exception" cause of loading big sized image. 但是,以第二种方式,您可以获得加载大型图像的“内存不足异常”原因。

I hope this'll help you. 希望对您有帮助。

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

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