用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - blackberry代码库

自定义View触摸监听

2017-04-04 作者: 老四和老三的老二都老大了举报

[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;
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...