第一步: 复写
onDraw方法,在方法中画出我们的视图(字符串为例)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
// 填充消除据此
paint.setStyle(Paint.Style.FILL);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
String string = "画字符串";
// 对于的xy坐标是文本左下角的坐标
canvas.drawText(string, 10, 100, paint);
//得到所画字符串的高宽
Rect rect = new Rect();
paint.getTextBounds(string, 0, string.length(), rect);
int with=rect.width();
int height=rect.height();
// 利用xy坐标和高宽得到字符在view中的矩形框
RectF rectF=new RectF();
rectF.left=10;
rectF.bottom=100;
rectF.right=rectF.left+with;
rectF.top=rectF.bottom-height;
// 得到监听view
this.rectF=rectF;
}
第二步 :生成一个自定义的监听,来监听我们自定义事件
/**
* 监听器
*
* @author WZG
*
*/
public interface PercentListener {
void clike();
}
第三步: 在view中设置一个传过来的自定义监听
/**
* 设置监听
*
* @param listener
*/
public void setPercnetListener(PercentListener listener) {
this.percentListener = listener;
}
第四步:在onTouchEvent方法中比较event的xy坐标和我们需要监听的view矩形框比较
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getX() > rectF.left && event.getX() < rectF.right
&& event.getY() > rectF.top
&& event.getY() < rectF.bottom) {
if(percentListener!=null){
percentListener.clike();
}
}
return super.onTouchEvent(event);
}
第五步:主视图中调用监听
public class MainActivity extends Activity implements PercentListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrViewTest view=(StrViewTest)findViewById(R.id.percent);
view.setPercnetListener(this);
}
@Override
public void clike() {
Log.e("tag", "点击了");
}
}
全部的代码:
package com.example.androidtest.activity.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
public class StrViewTest extends View {
/**
* 画笔
*/
private Paint paint;
/**
* 需要监听view的矩形框
*
*/
private RectF rectF;
/**
* 点击监听
*/
private PercentListener percentListener;
public StrViewTest(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
// 填充消除据此
paint.setStyle(Paint.Style.FILL);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
String string = "画字符串";
// 对于的xy坐标是文本左下角的坐标
canvas.drawText(string, 10, 100, paint);
//得到所画字符串的高宽
Rect rect = new Rect();
paint.getTextBounds(string, 0, string.length(), rect);
int with=rect.width();
int height=rect.height();
// 利用xy坐标和高宽得到字符在view中的矩形框
RectF rectF=new RectF();
rectF.left=10;
rectF.bottom=100;
rectF.right=rectF.left+with;
rectF.top=rectF.bottom-height;
// 得到监听view
this.rectF=rectF;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getX() > rectF.left && event.getX() < rectF.right
&& event.getY() > rectF.top
&& event.getY() < rectF.bottom) {
if(percentListener!=null){
percentListener.clike();
}
}
return super.onTouchEvent(event);
}
/**
* 设置监听
*
* @param listener
*/
public void setPercnetListener(PercentListener listener) {
this.percentListener = listener;
}
/**
* 监听器
*
* @author WZG
*
*/
public interface PercentListener {
void clike();
}
}