2015-01-12 58 views
5

有没有什么方法可以用这种渐变创建圆?android创建带渐变的饼图

enter image description here

据我得到的是这样的:

<shape 
     android:innerRadiusRatio="3" 
     android:thicknessRatio="10" 
     android:shape="ring"> 
    <gradient 
       android:endColor="@color/cyan_dark" 
       android:startColor="@color/red" 
       android:type="radial" 
       android:gradientRadius="340" 
       android:centerX="50%" 
       android:centerY="0" /> 
</shape> 
+0

什么是你的实际结果如何呢? – TheRedFox

回答

3

XML

<ProgressBar 
     android:id="@+id/yourId" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="89dp" 
     android:layout_height="89dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:max="100" 
     android:progress="70" 
     android:progressDrawable="@drawable/shapering" /> 

形状环形绘制

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadiusRatio="3" 
    android:shape="ring" 
    android:thicknessRatio="10" > 

    <gradient 
     android:centerColor="#D6DE47" 
     android:centerX="50%" 
     android:centerY="0" 
     android:endColor="#DE47A7" 
     android:gradientRadius="340" 
     android:startColor="#6D47DE" 
     android:type="sweep" /> 

</shape> 

结果

enter image description here

+0

感谢您的重播,但是,我需要支持100个进度,在您的答案中,如果您将进度更改为100,则“黄色效果”将显示在两侧, –

+0

已更新我的答案。 –

+0

谢谢!工作无懈可击 –

3

使用本

<ProgressBar 
     android:id="@+id/progressWheel" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="152dp" 
     android:layout_height="152dp" 
     android:layout_centerInParent="true" 
     android:progress="100" 
     android:indeterminate="false" 
     android:progressDrawable="@drawable/circular_progress_bar" /> 
在绘制circular_progress_bar

(改变颜色根据您的需要)

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:id="@android:id/progress"> 
    <rotate 
     android:fromDegrees="270" 
     android:toDegrees="270" 
     android:pivotX="50%" 
     android:pivotY="50%" > 
     <shape 
      android:innerRadiusRatio="2.5" 
      android:shape="ring" 
      android:thicknessRatio="25.0" > 
      <gradient 
       android:centerColor="@color/red" 
       android:endColor="@color/gray" 
       android:startColor="@color/gray" 
       android:type="sweep" /> 
     </shape> 
    </rotate> 
</item> 
<item android:id="@android:id/secondaryProgress"> 
    <rotate 
     android:fromDegrees="270" 
     android:toDegrees="270" 
     android:pivotX="50%" 
     android:pivotY="50%" > 
     <shape 
      android:innerRadiusRatio="2.5" 
      android:shape="ring" 
      android:thicknessRatio="25.0" > 
      <gradient 
       android:centerColor="@color/green" 
       android:endColor="@color/green" 
       android:startColor="@color/green" 
       android:type="sweep" /> 
     </shape> 
    </rotate> 
</item> 
</layer-list>  
1

你应该用type = “扫” 而不是 “放射状” 创建渐变像o在图片中。例如:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <shape 
     android:innerRadiusRatio="3" 
     android:shape="ring" 
     android:thicknessRatio="7.0"> 
     <gradient 
      android:startColor="#FF0000" 
      android:centerColor="#00FF00" 
      android:endColor="#0000FF" 
      android:type="sweep" /> 
    </shape> 
</item> 

3

使用本

<ProgressBar 
    android:id="@+id/progressWheel" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="152dp" 
    android:layout_height="152dp" 
    android:layout_centerInParent="true" 
    android:progress="100" 
    android:indeterminate="false" 
    android:progressDrawable="@drawable/circular_progress_bar" /> 
在绘制circular_progress_bar

(改变颜色根据您的需要)

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:id="@android:id/progress"> 
<rotate 
    android:fromDegrees="270" 
    android:toDegrees="270" 
    android:pivotX="50%" 
    android:pivotY="50%" > 
    <shape 
     android:innerRadiusRatio="2.5" 
     android:shape="ring" 
     android:thicknessRatio="25.0" > 
     <gradient 
      android:centerColor="@color/red" 
      android:endColor="@color/gray" 
      android:startColor="@color/gray" 
      android:type="sweep" /> 
    </shape> 
</rotate> 
</item> 
<item android:id="@android:id/secondaryProgress"> 
<rotate 
    android:fromDegrees="270" 
    android:toDegrees="270" 
    android:pivotX="50%" 
    android:pivotY="50%" > 
    <shape 
     android:innerRadiusRatio="2.5" 
     android:shape="ring" 
     android:thicknessRatio="25.0" > 
     <gradient 
      android:centerColor="@color/green" 
      android:endColor="@color/green" 
      android:startColor="@color/green" 
      android:type="sweep" /> 
    </shape> 
</rotate> 
</item> 
</layer-list>