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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="自定义圆环"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="圆环的半径 :"
android:textSize="20sp"/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/et_r"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="圆环的宽:"
android:textSize="20sp"/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/et_h"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="圆环的 颜色:"
android:textSize="20sp"/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/et_c"/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="生成的圆环展示"
android:onClick="show"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ll"></LinearLayout>
<!-- <com.bwie.text.MyImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"></com.bwie.text.MyImageView> -->
</LinearLayout>
MainActivity方法中
public class MainActivity extends Activity {
//定义属性
private EditText etR;
private EditText etH;
private EditText etC;
private LinearLayout ll;
private MyImageView miv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得控件
etR = (EditText) findViewById(R.id.et_r);
etH = (EditText) findViewById(R.id.et_h);
etC = (EditText) findViewById(R.id.et_c);
ll = (LinearLayout) findViewById(R.id.ll);
}
//按钮的点击事件
public void show(View view){
if(miv != null){
ll.removeView(miv);
miv = null;
}
//获得到页面的信息
String r = etR.getText().toString().trim();
String h = etH.getText().toString().trim();
String c = etC.getText().toString().trim();
miv = new MyImageView(this, Float.parseFloat(r), Float.parseFloat(h), c);
ll.addView(miv);
}
}
自定义view
public class MyImageView extends View{
//定义属性
private float r;
private float h;
private String c;
private Context context;
public MyImageView(Context context, float r, float h, String c) {
super(context);
this.context = context;
this.r = r;
this.h = h;
this.c = c;
//重绘
invalidate();
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获得画笔
Paint paint = new Paint();
paint.setColor(Color.BLUE);
//空心圆
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(h);
paint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(getWidth()/2-2*r-10-h, getHeight()/2, r, paint);
paint.reset();
paint.setColor(Color.BLACK);
//空心圆
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(h);
paint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(getWidth()/2, getHeight()/2, r, paint);
paint.reset();
paint.setColor(Color.RED);
//空心圆
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(h);
paint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(getWidth()/2+2*r+10+h, getHeight()/2, r, paint);
paint.reset();
paint.setColor(Color.YELLOW);
//空心圆
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(h);
paint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(getWidth()/2-r-5-h, getHeight()/2+r, r, paint);
paint.reset();
paint.setColor(Color.GREEN);
//空心圆
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(h);
paint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(getWidth()/2+r+5+h, getHeight()/2+r, r, paint);
paint.reset();
}
}