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" />