[blackberry]代码库
//自定义接口
public interface OnCircleClickListener {
public void smallCircleListener();
public void bigCircleListener();
public void rectClickListener();
}
public class CustomView extends View {
private Paint paint=new Paint();
private int rectColor;
private int sCircleColor;
private int bCircleColor;
private float downX;
private float downY;
private OnCircleClickListener onCircleClickListener;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomColor);
rectColor=typedArray.getColor(R.styleable.CustomColor_rectColor, Color.parseColor("#000000"));
sCircleColor=typedArray.getColor(R.styleable.CustomColor_sCircleColor,Color.parseColor("#0000ff"));
bCircleColor=typedArray.getColor(R.styleable.CustomColor_bCircleColor,Color.parseColor("#ffffff"));
typedArray.recycle();
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(rectColor);
canvas.drawRect(getWidth()/2-120,getHeight()/2-120,getWidth()/2+120,getHeight()/2+120,paint);
paint.setColor(bCircleColor);
canvas.drawCircle(getWidth()/2,getHeight()/2,100,paint);
paint.setColor(sCircleColor);
canvas.drawCircle(getWidth()/2,getHeight()/2,50,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_UP:
float upX=event.getX();
float upY=event.getY();
//判断是不是点击的
if(upX==downX&&upY==downY){
int xx=positionS(upX,upY);
if(xx==1){
if(onCircleClickListener!=null){
onCircleClickListener.smallCircleListener();
}
}else if(xx==2){
if(onCircleClickListener!=null){
onCircleClickListener.bigCircleListener();
}
}else if(xx==3){
if(onCircleClickListener!=null){
onCircleClickListener.rectClickListener();
}
}
}
break;
}
return true;
}
public int positionS(float x, float y){
float v=(x-getWidth()/2)*(x-getWidth()/2)+(y-getHeight()/2)*(y-getHeight()/2);
float vv= (float) Math.sqrt(v);
if(vv<50){
return 1;
}else if(vv>50&&vv<100){
return 2;
}else if(vv>100){
return 3;
}
return -1;
}
public void setOnRectClickListener(OnCircleClickListener onCircleClickListener){
this.onCircleClickListener=onCircleClickListener;
}
}
by: 发表于:2017-10-25 10:46:21 顶(0) | 踩(0) 回复
??
回复评论