自定义圆环输入值改变圆环宽、半径、颜色
//--------------------------MainActivity中---------------------------------------------
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
private EditText et_banjing;
private EditText et_kuan;
private EditText et_yanse;
private Button bt_button;
private MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
et_banjing = (EditText) findViewById(R.id.et_banjing);
et_kuan = (EditText) findViewById(R.id.et_kuan);
et_yanse = (EditText) findViewById(R.id.et_yanse);
myView = (MyView) findViewById(R.id.my_view);
}
public void shengcheng(View view){
String banjing=et_banjing.getText().toString().trim();
String kuan=et_kuan.getText().toString().trim();
String yanse=et_yanse.getText().toString().trim();
//自定义view中的方法传值过去
myView.initView(kuan,banjing,yanse);
}
}
//----------------------------布局文件------------------------------------------
<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">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆环宽:"/>
<EditText android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/et_kuan"
android:hint="请输入圆环宽"
android:password="true"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆环半径:"/>
<EditText android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/et_banjing"
android:password="true"
android:hint="请输入圆环半径"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆环颜色:"/>
<EditText android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/et_yanse"
android:hint="请输入圆环颜色"/>
</LinearLayout>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击生成圆环"
android:onClick="shengcheng"/>
<com.example.yuan.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/my_view"></com.example.yuan.MyView>
</LinearLayout>
//------------------自定义view------需要继承view-----实现3个有参构造------------------------
package com.example.yuan;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{
private Paint paint;
private String skuan=""+5;//设置默认宽
private String sbanjing=""+20;//设置默认半径
private String syanse="#ff00ff";//设置默认颜色
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//创建画笔
paint = new Paint();
//设置抗锯齿
paint.setAntiAlias(true);
//设置画笔类型
paint.setStyle(Paint.Style.STROKE);
}
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置画笔颜色
paint.setColor(Color.parseColor(syanse));
//设置画笔宽度,也就是圆环宽度
paint.setStrokeWidth(Integer.parseInt(skuan));
//设置画圆
canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, Float.parseFloat(sbanjing), paint);
}
public void initView(String kuan, String banjing, String yanse) {
//将输入的值传到这
skuan=kuan;
sbanjing=banjing;
syanse=yanse;
//刷新
invalidate();
}
}