//-----------------自定义MyView继承Imageview-------------------------------
package com.bw.yuanhuan;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.JetPlayer;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MyView extends ImageView{
private float cx=150;//x轴坐标
private float cy=150;//Y轴坐标
private float radius;//半径
private int ku;//宽
private Paint paint;//画笔
private Paint paint1;//画笔1
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//创建一个画笔
paint = new Paint();
//画笔颜色
paint.setColor(Color.WHITE);
//创建一个画笔
paint1 = new Paint();
//画笔颜色
paint1.setColor(Color.GREEN);
}
//实现onDraw方法
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (jiekou!=null) {
radius=jiekou.ban();
ku=jiekou.kuan();
}
//绿色 画布画圆
canvas.drawCircle(cx, cy, radius+ku, paint1);
//白色 画布画圆
canvas.drawCircle(cx, cy, radius, paint);
//刷新
invalidate();
}
//声明接口
private JieKou jiekou;
//创建一个接口
public interface JieKou{
int ban();
int kuan();
}
public void diaoyou(JieKou jiekou){
this.jiekou=jiekou;
}
}
//-----------------------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" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="半径:"
/>
<EditText
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/et_banjing"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宽度:"
/>
<EditText android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/et_kuandu"/>
</LinearLayout>
<Button android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="换"
android:id="@+id/bt_button"/>
<com.bw.yuanhuan.MyView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/myView"/>
</LinearLayout>
//------------------MainActivity 中----------------------------------
package com.bw.yuanhuan;
import com.bw.yuanhuan.MyView.JieKou;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
private EditText et_banjing;
private EditText et_kuandu;
private Button bt_button;
private MyView myView;
private String banjing;
private String kuandu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件EditText
et_banjing = (EditText) findViewById(R.id.et_banjing);
et_kuandu = (EditText) findViewById(R.id.et_kuandu);
//找到空件
bt_button = (Button) findViewById(R.id.bt_button);
//设置监听事件
bt_button.setOnClickListener(this);
//找到自定义view
myView = (MyView) findViewById(R.id.myView);
}
//点击事件的方法
@Override
public void onClick(View v) {
//获得输入的值
banjing = et_banjing.getText().toString().trim();
kuandu = et_kuandu.getText().toString().trim();
//调用myView中的接口方法
myView.diaoyou(new JieKou() {
//宽
@Override
public int kuan() {
//返回int类型的值
return Integer.parseInt(kuandu);
}
//高
@Override
public int ban() {
//返回int类型的值
return Integer.parseInt(banjing);
}
});
}
}