说明
可以设置内部填充或不填充 可以社会中边框颜色
View
public class MyRingView extends RelativeLayout { private Context mContext; private ImageView iv_circle; private ImageView iv_solid; public MyRingView(Context context) { super(context); initView(context); } public MyRingView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public MyRingView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { mContext = context; inflate(mContext, R.layout.view, this); iv_circle = (ImageView) findViewById(R.id.iv_circle); iv_solid = (ImageView) findViewById(R.id.iv_solid); } /** * @param color 颜色 * @param isSolid 内部是否填充 */ public void setStyle(int color, boolean isSolid) { ((GradientDrawable) iv_circle.getBackground()).setStroke(2, color);//圆环的颜色 if (isSolid) { ((GradientDrawable) iv_solid.getBackground()).setColor(color);//指定内部圆的背景色 ((GradientDrawable) iv_solid.getBackground()).setStroke(0, color);//内部圆无描边,必须显示设为0 } else { ((GradientDrawable) iv_solid.getBackground()).setColor(0xffffffff);//内部圆的背景是白色的 ((GradientDrawable) iv_solid.getBackground()).setStroke(2, color);//内部圆有描边 } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/iv_circle" android:layout_width="15dp" android:layout_height="15dp" android:layout_centerInParent="true" android:background="@drawable/ring" android:src="#0000" /> <ImageView android:id="@+id/iv_solid" android:layout_width="10dp" android:layout_height="10dp" android:layout_centerInParent="true" android:background="@drawable/solid" android:src="#0000" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="4.5dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <solid android:color="#fff" /> <stroke android:width="0.5dp" android:color="#ffa726" /> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#00f" /> </shape>
使用
public class MainActivity extends Activity { private MyRingView[] views = new MyRingView[4]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); views[0] = (MyRingView) findViewById(R.id.item1); views[1] = (MyRingView) findViewById(R.id.item2); views[2] = (MyRingView) findViewById(R.id.item3); views[3] = (MyRingView) findViewById(R.id.item4); views[0].setStyle(0xff666666, false); views[1].setStyle(0xff00b0ff, true); views[2].setStyle(0xffffa726, false); views[3].setStyle(0xffff1744, true); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="horizontal" android:padding="5dp" > <com.bqt.shape53.MyRingView android:id="@+id/item1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" /> <com.bqt.shape53.MyRingView android:id="@+id/item2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" /> <com.bqt.shape53.MyRingView android:id="@+id/item3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" /> <com.bqt.shape53.MyRingView android:id="@+id/item4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" /> </LinearLayout>
时间: 2024-11-08 17:09:46