Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

TouchTuner in Android

TouchTuner in Android


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchTuner extends View {

    private Paint circlePaint;
    private Paint borderPaint;
    private RectF circleRect;
    private float centerX, centerY;
    private float radius = 100.0f;
    private float touchX, touchY;
    private boolean isTouched = false;

    public TouchTuner(Context context) {
        super(context);
        init();
    }

    public TouchTuner(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchTuner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.WHITE);
        circlePaint.setStyle(Paint.Style.FILL);

        borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setColor(Color.BLACK);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(10.0f);

        circleRect = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        centerX = w / 2.0f;
        centerY = h / 2.0f;

        circleRect.left = centerX - radius;
        circleRect.top = centerY - radius;
        circleRect.right = centerX + radius;
        circleRect.bottom = centerY + radius;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Draw the circle
        canvas.drawCircle(centerX, centerY, radius, circlePaint);
        canvas.drawCircle(centerX, centerY, radius, borderPaint);

        // If the circle is touched, draw a crosshair
        if (isTouched) {
            canvas.drawLine(touchX, 0.0f, touchX, getHeight(), borderPaint);
            canvas.drawLine(0.0f, touchY, getWidth(), touchY, borderPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                // Check if the touch is within the circle
                if (circleRect.contains(x, y)) {
                    touchX = x;
                    touchY = y;
                    isTouched = true;
                    invalidate();
                }
                break;

            case MotionEvent.ACTION_UP:
                isTouched = false;
                invalidate();
                break;

            default:
                return false;
        }

        return true;
    }
}

        

Post a Comment