2017-02-21 20 views
0

我必须在圆内呈现图标,我试图在另一个环内画一个戒指,但我不能这样做,当我尝试它时很尴尬,我该怎么做如何在另一个环内画一个戒指

I tried to do like this but i can't

我的代码

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Larger blue circle in back --> 
<item> 
    <shape android:shape="oval"> 
     <solid android:color="#f00"/> 
     <size 
      android:width="15dp" 
      android:height="15dp"/> 
    </shape> 
</item> 
<!-- Smaller red circle in front --> 
<item> 
    <shape android:shape="oval"> 
     <!-- transparent stroke = larger_circle_size - smaller_circle_size --> 
     <stroke android:color="@android:color/transparent" 
      android:width="10dp"/> 
     <solid android:color="#fff"/> 
     <size 
      android:width="1dp" 
      android:height="1dp"/> 
    </shape> 
</item> 
<item> 
    <shape android:shape="oval"> 
     <!-- transparent stroke = larger_circle_size - smaller_circle_size --> 
     <stroke android:color="@android:color/transparent" 
      android:width="11dp"/> 
     <solid android:color="#f00"/> 
     <size 
      android:width="1dp" 
      android:height="1dp"/> 
    </shape> 
</item> 

+0

你尝试用'Canvas'? (没有xml,直接在'Bitmap'上) – Massimo

+0

不,我没试过试过我只用xml – Sasi

回答

1
<?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="oval"> 
      <size 
       android:width="200dp" 
       android:height="200dp" /> 
      <solid android:color="@android:color/transparent" /> 
      <stroke android:color="#ff0000" android:width="5dp"/> 
     </shape> 
    </item> 

    <item 
     android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"> 

     <shape android:shape="oval"> 
      <solid android:color="@android:color/transparent" /> 
      <stroke android:color="#5BB534" android:width="5dp"/> 
     </shape> 
    </item> 
</layer-list> 
+0

非常感谢 – Sasi

1

试试我的代码。您可以根据您的要求重新编写值。 将此drawable添加到任何背景时,请确保该视图是方形的。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:left="6dip" 
    android:right="6dip"> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="oval"> 
     <stroke 
      android:width="3dip" 
      android:color="#f00" /> 
    </shape> 

</item> 

<item 
    android:bottom="80dip" 
    android:left="80dip" 
    android:right="80dip" 
    android:top="80dip"> 

    <shape android:shape="oval"> 
     <stroke 
      android:width="1dip" 
      android:color="#0f0" /> 
    </shape> 
</item> 

</layer-list> 

编辑添加的图像 This what I get.

+0

@pandrideepak红色边框只显示 – Sasi

+0

@Swathy:更新了图像 –

1

尝试用帆布绘制:

DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); 
Bitmap bitmap = Bitmap.createBitmap((int) (15 * metrics.density), (int) (15 * metrics.density), Bitmap.Config.ARGB_8888); // 15dp x 15dp 
Canvas canvas = new Canvas(bitmap); 

// clear the bitmap with a transparent color 
canvas.drawColor(Color.TRANSPARENT); 
Paint paint = new Paint(); 
paint.setStrokeWidth(2 * metrics.density); // 2dp, the circle width 

// the external circle, large as the bitmap 
paint.setColor(Color.RED); 
canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint); 

// the internal one, smaller than the first one 
paint.setColor(Color.GREEN); 
canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2 - 3 * metrics.density, paint); 

你应该调整一些数字。 DisplayMetrics仅用于在dp(而不是像素)中转换大小。完成后,如果您需要将其用于视图(例如作为背景),则可以使用BitmapBitmapDrawable

0

试试这个代码

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:left="3dp" 
    android:right="3dp"> 
    <shape android:shape="oval"> 
     <stroke android:width="5dp" 
      android:color="@android:color/holo_red_dark"> 

     </stroke> 
    </shape> 
</item> 
<item 
    android:bottom="10dp" 
    android:left="10dp" 
    android:right="10dp" 
    android:top="10dp"> 
    <shape android:shape="oval"> 
     <stroke android:width="10dp" 
      android:color="@android:color/holo_green_dark"> 

     </stroke> 
    </shape> 
</item> 

相关问题