Ethan
Ethan

Reputation: 39

custom view can be clicked

I want to make my custom view be clicked.And now I do not know which is wrong,when I click it ,it happens nothing.Thanks in advance.This view is that I use canvas to draw a ring,and I want the inside of the ring can be clicked.

public class CircleProgressBar extends View{
    OnClickListener progressButton;
    private int hour;
    private int maxProgress = 24;
    private int progress;
    int progress1;
    private int progressStrokeWidth = 32;

    RectF oval;
    Paint paint;
    Paint paint1;




    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        oval = new RectF();
        paint = new Paint();
        paint1 = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        Calendar c=Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        progress = hour;
        int width = this.getWidth();
        int height = this.getHeight();

        if(width != height){

            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setColor(Color.LTGRAY);
        canvas.drawColor(Color.TRANSPARENT);
        paint.setStrokeWidth(15);
        paint.setStyle(Style.STROKE);

        oval.left = progressStrokeWidth / 2; // 左上角x
        oval.top = progressStrokeWidth / 2; // 左上角y
        oval.right =height - progressStrokeWidth / 2; // 左下角x
        oval.bottom = height - progressStrokeWidth / 2; // 右下角y
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
        canvas.drawArc(oval, -90, 360, false, paint);

        paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
        paint.setStrokeCap(Paint.Cap.ROUND);
        if(progress == 9){
            canvas.drawArc(oval, -90, 134, false, paint);
        }else{
        canvas.drawArc(oval, -90, (long)(((float) progress / maxProgress) * 360), false, paint);
        }
        paint.setColor(Color.CYAN);
        paint.setAntiAlias(true);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));

        canvas.drawArc(oval, -90, 135+0.5f, false, paint);

        paint.setStrokeWidth(15);



        String text = (long)(((float) progress / maxProgress) * 100) + "%";
        int textHeight = height / 4;
        paint.setTextSize(textHeight);
        int textWidth = (int) paint.measureText(text, 0, text.length());
        paint.setStyle(Style.FILL);
        canvas.drawText(text, width / 2 - textWidth / 2, height / 2 +textHeight/2, paint);
    }
    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        this.invalidate();
    }



    public void setProgressNotInUiThread(int progress) {
        this.progress = progress;
        this.postInvalidate();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_DOWN){

        }
        return true;

    }

This is my main activity.

public class MainActivity extends Activity{

Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.circle_fit);


    CircleProgressBar progressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar);
    progressBar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();

        }
    });

here is my xml

<com.example.circlefit.CircleProgressBar
        android:id="@+id/circleProgressbar"
        android:layout_width="200dp"
        android:layout_height="200dp" />

Upvotes: 0

Views: 138

Answers (3)

Rahul Gupta
Rahul Gupta

Reputation: 5295

Remove the touch listener in your custom class. It is ovveriding the click functionality :-

/

/Remove this piece of code from your class and it will work just fine
    @Override
    public boolean onTouchEvent(MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){

    }
    return true;

}

Upvotes: 1

Rathan Kumar
Rathan Kumar

Reputation: 2577

try like this:

private OnClickListener clickListener; private boolean isMoved;

@Override
public void setOnClickListener(OnClickListener l) {
    this.clickListener = l;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        isMoved = false;
        if (isValidtouchAndIsInsideCircle(event)) {
            return true;
        } else {
            return false;
        }
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        isMoved = true;
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (clickListener != null && !isMoved) {
            clickListener.onClick(this);
        }
        isMoved = false;
    }
    return false;
}

private boolean isValidtouchAndIsInsideCircle(MotionEvent event) {
    int xPoint = (int) event.getX();
    int yPoint = (int) event.getY();
    // here validate the x and y points with your circle coordinates if it
    // is in circle return true or else return false
    return false;
}

Upvotes: 0

max59
max59

Reputation: 606

Add this in your XML:

android:clickable="true"
android:focusable="true"

Upvotes: 0

Related Questions