2016-08-18 123 views
4

它已經有一段時間了,我一直堅持這一點。我是一個新手android開發人員。根據屏幕尺寸縮放可繪製圓圈

我有一個可繪製的圓。

circular_shape.xml

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:useLevel="false" 
    android:shape="ring" 
    android:thickness="0.8dp"> 
    <solid android:color="@android:color/holo_blue_dark"/> 
</shape> 

現在我想用在我的活動佈局這個圈子,因爲它覆蓋了整個屏幕的方式。也就是說,屏幕的寬度應該等於圓的直徑。 請注意,我沒有設置圓的半徑。

example_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/exampleLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.anil.raceorganizer.ExampleActivity"> 
    <ImageView 
     android:id="@+id/race_field" 
     android:src="@drawable/circular_shape" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/darker_gray" 
     android:padding="0dp" 
     android:layout_margin="0dp" 
     ></ImageView> 
</FrameLayout> 

請注意,我試圖把padding和margin爲0,但沒有奏效。 我也嘗試通過代碼設置ImageView的大小,但它只是改變了ImageView的整體大小,而不是單獨循環。

這就是我用這段代碼得到的結果。

enter image description here

任何幫助是極大的讚賞。

+1

如果你想使用畫布繪製圖像比XML,我可以給和示例 – Stallion

+1

如果你想我可以告訴你如何用畫指南針 – lelloman

+0

@Stallion - 請確實顯示 –

回答

1

你可以試試這個代碼:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:thickness="0.8dp" android:useLevel="false" android:innerRadiusRatio="2.1"> <solid android:color="@android:color/holo_blue_dark" /> </shape>

+0

我不認爲innerradiusratio與屏幕尺寸有任何關係 –

+1

您可以更改屏幕大小來測試我的代碼。我保證它會正常工作。 :))。
默認情況下,innerRadiusRatio = 1,這意味着你的circle =你的寬度的1/2。 –

+0

恐怕我無法改變我的客戶手機屏幕大小來匹配您的價值_2.1_!這是一個現場項目! –

0

試試這個 您可以繪製

android:innerRadius="150dp" 

改變使用此標記半徑相應

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

使用寬度從上述

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:useLevel="false" 
     android:shape="ring" 
     android:innerRadius="150dp" 
     android:thickness="0.8dp"> 
     <solid android:color="@android:color/holo_blue_dark"/> 
    </shape> 
+0

但我希望它是屏幕的大小。不是150dp。你知道如何動態設置這個半徑嗎? –

+0

是等一下 –

+0

如何設置半徑?這是我的問題1 –

1

使用畫布,我們可以繪製與屏幕寬度匹配的形狀。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); 

    Canvas c = new Canvas(bmp); 

    RectF rect = new RectF(0,0,width,width); 
    drawCircle(rect, c, width, height); 

} 

private void drawCircle(RectF rect, Canvas c, int width, int height) { 
     Paint paint = new Paint(); 
     paint.setARGB(255, 255 , 10, 21); 
     paint.setStrokeWidth(8); 
     paint.setAntiAlias(true); 
     paint.setStrokeCap(Paint.Cap.BUTT); 
     paint.setStyle(Paint.Style.STROKE); 
     int radius; 
     if(width < height) 
      radius = width/2; 
     else 
      radius = height/2; 

     radius-= 4; 
     c.drawCircle(width/2, height/2, radius, paint); 
    } 

不知道它是否可以幫助你。如果不是,請告訴我。我寧願刪除,也不願意獲得降價!

+0

好吧我不喜歡你的解決方案,你可能會做同樣的事情擴展一個Drawable和IMHO它看起來會更好...但我沒有勇氣來倒敘Rambo – lelloman

+0

@lelloman回答之前我問我是否可以把這個作爲答案。根據anil_pulikoden的回覆,只有我發佈。 – Stallion

+0

它的工作原理。但我不認爲我需要一個畫布來完成我的活動。格拉西亞斯! –

0

使用這種方法得到環狀結構。

使用尺寸標籤保持1:1寬高比

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

<size android:width="50dp" 
android:height="50dp"/> 

<stroke android:color="#000" android:width="0.1dp"/> 

</shape> 
+0

我把這段代碼。但是圈內沒有變化。我的問題是如何將圓直徑更改爲屏幕寬度。有任何想法嗎? –