如何将imageview插入自定义xml形状

时间:2017-07-02 12:36:48

标签: android

我想在android中使用抽屉xml文件创建的自定义形状中显示imageview。我在互联网上搜索了几个星期但没有结果。我找到的唯一答案是使用常规形状(圆形,矩形)或在图层中使用xml(将图像作为背景),但图像是从服务器动态的。

这是图片视图

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    app:srcCompat="@drawable/upload_newdish_icon"
    android:layout_below="@+id/dish_name_et"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:id="@+id/imageView2" />

这是自定义形状

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="70dp" android:height="70dp" android:viewportHeight="700.000000" android:viewportWidth="700.000000">

<group android:scaleX="0.100000" android:scaleY="-0.100000" android:translateY="700.000000">
<path android:fillColor="@color/colorPrimary" android:pathData="M3065 6779 c-632 -31 -1270 -127 -1895 -285 -274 -70 -351 -105 -455 -209 -104 -104 -139 -181 -209 -455 -145 -574 -232 -1117 -278 -1745 -19 -256 -16 -979 6 -1245 46 -580 135 -1128 271 -1665 71 -280 105 -355 210 -460 104 -104 181 -139 455 -209 544 -137 1088 -226 1670 -273 301 -24 1019 -24 1320 0 582 47 1126 136 1670 273 274 70 351 105 455 209 104 104 139 181 209 455 145 574 232 1117 278 1745 19 256 16 979 -6 1245 -46 580 -135 1128 -271 1665 -71 280 -105 355-210 460 -104 104 -181 139 -455 209 -914 231 -1864 329 -2765 285z"/>
</group>
</vector>

1 个答案:

答案 0 :(得分:0)

解决此问题的唯一方法是使用包含两个元素的FrameLayout:自定义形状和包含imageview的relativelayout

<FrameLayout>

<Custom_shape
android:layout_width="xdp"
android:layout_height="ydp"
>

<RelativeLayout
android:layout_width="xdp"
android:layout_height="ydp">

<imageview
centerinparent="true"
//dimension of the image view (should <(x,y) )
//or set wrap content for the widh and the height + set a padding to get the imageview in the center of relativelayout

>

</RelativeLayout>

</FrameLayout>

自定义形状和RelativeLayout应具有相同的尺寸(宽度和高度)