EditText输入数值画圆环

来源:互联网 发布:java怎么sleep 编辑:程序博客网 时间:2024/06/07 12:13

效果如图



### 新建一个类 AnnulusView继承View


public class AnnulusView extends View {    private int radius;//半径    private int widths;//圆环宽度    private String colors = "#ff0000";    private Paint paint = new Paint();//画笔    public int getRadius() {        return radius;    }    public void setRadius(int radius) {        this.radius = radius;    }    public int getWidths() {        return widths;    }    public void setWidths(int widths) {        this.widths = widths;    }    public String getColors() {        return colors;    }    public void setColors(String colors) {        this.colors = colors;    }    public AnnulusView(Context context) {        super(context);    }    public AnnulusView(Context context, AttributeSet attrs) {        super(context, attrs);        //抗锯齿        paint.setAntiAlias(true);        //设置圆环        paint.setStyle(Paint.Style.STROKE);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //设置空心线宽        paint.setStrokeWidth(1);        //设置内圆        canvas.drawCircle(200, 200, radius, paint);        //设置圆环        paint.setStrokeWidth(widths);        paint.setColor(Color.parseColor(colors));        canvas.drawCircle(200, 200, radius + 1 + widths / 2, paint);        //设置外圆        paint.setStrokeWidth(2);        canvas.drawCircle(200, 200, radius + widths, paint);    }}



###  main布局

<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.example.edittext.AnnulusView        android:id="@+id/cv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

### MainActivity

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);// 找到控件        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.setRadius(Integer.parseInt(radius));                cv.setWidths(Integer.parseInt(widths));                cv.setColors(colors);                cv.invalidate();            }        });    }}













0 0