【问题标题】:How to make ring shape with color slice android xml如何用颜色切片android xml制作环形
【发布时间】:2017-04-14 18:03:48
【问题描述】:

我想为我的进度条创建一个这样的drawable

我已经尝试过这段代码,但我没有得到我想要的

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:useLevel="false">
            <gradient
                android:centerColor="#ffff00"
                android:endColor="#00ff00"
                android:startColor="#fb0000"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

这就是我所拥有的

是否可以仅使用 xml 来执行此操作? 或者这应该用java代码来完成?

【问题讨论】:

  • 老实说,你得到的那个看起来更好:D
  • 这应该用java代码来完成。
  • @Vucko 但是当某些部分为空/透明时,进度条看起来很奇怪。
  • @A.Badakhshan 如何使用 java 代码做到这一点?

标签: android xml


【解决方案1】:

由于您在 cmets 中询问您是如何做到这一点的,而我无法将这些链接放在评论中,所以我会向您推荐这三个链接。阅读它们并提取您想要的内容:

how to draw a half circle in android

Draw a semicircle in the background of a View

https://github.com/devadvance/circularseekbar

【讨论】:

    【解决方案2】:

    这是您问题的解决方案。

    DifferentColorCircularBorder.java

    public class DifferentColorCircularBorder {
        private RelativeLayout parentLayout;
    
        public DifferentColorCircularBorder(RelativeLayout parentLayout) {
            this.parentLayout = parentLayout;
        }
    
        public void addBorderPortion(Context context, int color, int startDegree, int endDegree) {
            ProgressBar portion = getBorderPortion(context, color, startDegree, endDegree);
            parentLayout.addView(portion);
        }
    
        private ProgressBar getBorderPortion(Context context, int color, int startDegree, int endDegree) {
            LayoutInflater inflater = LayoutInflater.from(context);
    
            ProgressBar portion = (ProgressBar) inflater.inflate(R.layout.border_portion, parentLayout, false);
            portion.setRotation(startDegree);
            portion.setProgress(endDegree - startDegree);
    
            portion.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) portion.getLayoutParams();
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            portion.setLayoutParams(params);
    
            return portion;
        }
    }
    

    border_portion.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:progressDrawable="@drawable/circle_exterior"
        android:layout_centerInParent="true"
        android:max="360"/>
    

    circle_exterior.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="ring"
        android:innerRadius="100dp"
        android:thickness="10dp" >
    
        <solid android:color="#ff111111" />
    </shape>
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RelativeLayout interiorLayout = (RelativeLayout) findViewById(R.id.interior);
    
            DifferentColorCircularBorder border = new DifferentColorCircularBorder(interiorLayout);
            border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleBlue), 0, 50);
            border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleGreen), 50, 120);
            border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleYellow), 120, 220);
            border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleRed), 220, 360);
        }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2d2d2d">
    
        <RelativeLayout
            android:id="@+id/interior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <View
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                android:background="@drawable/profilepic" />
        </RelativeLayout>
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2011-07-25
      • 2012-12-24
      • 1970-01-01
      • 2018-07-20
      • 2015-03-06
      • 1970-01-01
      相关资源
      最近更新 更多