我正在寻找一个自定义小部件来绘制一个具有多种边框颜色的圆.
比方说,如果我的总圆圈代表0-360,我需要用不同的颜色为我的圆圈边框着色.
例如,我需要用红色标记0-60,用绿色标记61-120,用洋红色标记121-300和用黄色边框颜色标记301-360.
请建议我如何在android中做到这一点.
解决方法
你的应用程序非常简单.我不建议您使用外部库.您可以快速实现绘制和管理所需形状的类.举例说明:
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 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,Mode.SRC_ATOP); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) portion.getLayoutParams(); params.addRule(RelativeLayout.CENTER_IN_PARENT); portion.setLayoutParams(params); return portion; } }
border_portion定义如下:
<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在这里定义:
<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类定义如下:
public class MainActivity extends Activity { @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(),Color.RED,40); border.addBorderPortion(getApplicationContext(),Color.GREEN,40,90); border.addBorderPortion(getApplicationContext(),Color.BLUE,90,270); border.addBorderPortion(getApplicationContext(),0xFF123456,270,360); } }
最后activity_main布局是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <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:background="@drawable/circle_interior_bg" android:layout_centerInParent="true" /> </RelativeLayout> </RelativeLayout>
关于维度的说明:这是一个例子.在这里,我选择了尺寸以完美贴合圆圈.根据您的应用更改这些.
图片样本: