1.设置自定义属性首先要在Values里面创建文件文件名为attrs.xml
<resources>
<declare-styleable name="AnnulusView">
<attr name="aaa" format="color"></attr>
</declare-styleable>
</resources>
2.创建自定义组建类
AnnulusView
public class AnnulusView extends View {
public int radius;
public int widths;
public String colors = "#ff0000";
private Paint mPaint;
private int color;
public AnnulusView(Context context) {
super(context);
}
public AnnulusView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AnnulusView);
this.color = array.getColor(R.styleable.AnnulusView_aaa, Color.RED);
//抗锯齿
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//设置圆环
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
//设置画笔颜色
mPaint.setColor(color);
//设置圆环宽度
mPaint.setStrokeWidth(widths);
//设置圆的启示位置和半径
canvas.drawCircle(300, 300, radius, mPaint);
}
}
3.在mian.xml里面引用部件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center"
android:padding="5dp"
android:text="自定义圆环"
android:textColor="#ffffff"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="圆环的半径:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_radius"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="圆环的宽度:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_width"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="圆环的颜色:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_color"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#ff0000"
android:gravity="center"
android:padding="5dp"
android:text="生成圆环展示"
android:textColor="#ffffff"
android:textSize="25sp" />
<com.bawei.yuanhuan1.AnnulusView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
custom:aaa="#f0f"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4.在主activity写代码为:
public class MainActivity extends AppCompatActivity {
private EditText etRadius;
private EditText etWidth;
private EditText etColor;
private Button bt;
private AnnulusView cv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
this.getWindowManager().getDefaultDisplay().getWidth();
etRadius = (EditText) findViewById(R.id.et_radius);
etWidth = (EditText) findViewById(R.id.et_width);
etColor = (EditText) findViewById(R.id.et_color);
bt = (Button) findViewById(R.id.bt);
cv = (AnnulusView) findViewById(R.id.cv);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String radius = etRadius.getText().toString();
String widths = etWidth.getText().toString();
String colors = etColor.getText().toString();
cv.radius = Integer.parseInt(radius);
cv.widths = Integer.parseInt(widths);
cv.colors = colors;
cv.invalidate();
}
});
}
}