2015-08-24 71 views
0

如何创建可能包含多个图像并包含圆形边框的视图?以下是最终产品的几个样品。如何使用多个图像创建圆角视图

enter image description here enter image description here

图像将来自URL下载,并且如所示的样品中,有可能是从包含在视图一至四个图像。

+1

使用习惯'Drawable'其中提请4或2'Bitmap's在'draw'方法 – pskink

+0

@ pskink我该怎么做呢?有没有可以指引我的导游?谢谢! –

+1

是的,看到'android.support.v4.graphics.drawable.RoundedBitmapDrawable'的来源 – pskink

回答

0

另一种选择是创建一个自定义LinearLayout,其中包含四个ImageViews xml,并可以动态重新组织使用权重显示多少个ImageViews

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="100dp" 
    android:layout_height="100dp"> 

    <LinearLayout 
     android:id="@+id/left_container" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_width="50dp" 
     android:layout_height="100dp" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/top_left_image" 
      android:layout_width="50dp" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="#111" /> 


     <ImageView 
      android:id="@+id/bottom_left_image" 
      android:layout_width="50dp" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="#333" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/right_container" 
     android:layout_width="50dp" 
     android:layout_height="100dp" 
     android:layout_toRightOf="@+id/left_container" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/top_right_image" 
      android:layout_width="50dp" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="#555" /> 


     <ImageView 
      android:id="@+id/bottom_right_image" 
      android:layout_width="50dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" 
      android:background="#777" /> 

    </LinearLayout> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/transparent_circle_image"/> 

</RelativeLayout> 

你提到,你是从URL中加载图像,所以如果你使用这种方法,你将能够使用图书馆像Picasso加载图片,你就不必担心等待所有图片在绘制圆形图像之前下载。如果你这样做,每个图像可以独立于其他图像加载。

唯一的缺点是您将不得不使用具有透明圆形背景的图像来创建圆形图像的外观。你可以创建一个常用的drawable来使用。或者您可以尝试将其绘制到画布上。这questions有一个很好的解决方案,将创建一个自定义视图,绘制一个透明的圆圈。

如果您想使用的自定义视图只需更换

<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/transparent_circle_image"/> 

<com.app.view.TransparentCircle 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
+1

喜欢它!最后一个问题 - 我应该如何最有效地创建具有透明圆形背景的图像? –

+0

我刚刚更新了我的答案,并提供了一个链接,指向另一个为透明圆实现自定义视图的问题。要么使用它,要么只是创建一个正常的图像来使用。 –

+0

没问题,祝你好运! –

1

有几个很好的第三部分库,你可以使用这个创建圆形图像。但事先您需要将多个图像组合成一个矩形,然后可以制作成圆形。

我会看看CircleImageViewRoundedImageView。区别在于他们的名字描述CircleImageView会给你一个完美的圆形图像。 RoundedImageView实际上可以提供矩形,椭圆形或圆形的圆角。

如果您试图让您的应用轻量化并避免使用外部库,那么您也可以在中间创建一个圆形图像,并在中间使用常规图像叠加为源图像顶部的背景图像ImageView

+0

我可以在CircleImageView库中放置多个图像吗?它似乎没有支持,但尚未 –

+1

@AnkitGoyal不使用任何外部库:只是看看它是如何完成在'android.support.v4.graphics.drawable.RoundedBitmapDrawable' – pskink

+0

刚编辑我的答案,你会想要首先将图像组合成矩形。然后使用其中一个库。 –