android vertical seekbar github code example

Example 1: android vertical seekbar github

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent


class VerticalSeekBar : androidx.appcompat.widget.AppCompatSeekBar {
    private var changeListener: OnSeekBarChangeListener? = null
    private var x = 0
    private var y = 0
    private var z = 0
    private var w = 0

    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    @Synchronized
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(h, w, oldh, oldw)
        x = w
        y = h
        z = oldw
        this.w = oldh
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec)
        setMeasuredDimension(measuredHeight, measuredWidth)
    }

    override fun onDraw(c: Canvas) {
        c.rotate(-90f)
        c.translate(-height.toFloat(), 0f)
        super.onDraw(c)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (!isEnabled) {
            return false
        }
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                isSelected = true
                isPressed = true
                if (changeListener != null) changeListener!!.onStartTrackingTouch(this)
            }
            MotionEvent.ACTION_UP -> {
                isSelected = false
                isPressed = false
                if (changeListener != null) changeListener!!.onStopTrackingTouch(this)
            }
            MotionEvent.ACTION_MOVE -> {
                val progress = (max
                        - (max * event.y / height).toInt())
                setProgress(progress)
                onSizeChanged(width, height, 0, 0)
                if (changeListener != null) changeListener!!.onProgressChanged(this, progress, true)
            }
            MotionEvent.ACTION_CANCEL -> {
            }
        }
        return true
    }

    @Synchronized
    override fun setOnSeekBarChangeListener(
        listener: OnSeekBarChangeListener
    ) {
        changeListener = listener
    }

    @Synchronized
    override fun setProgress(progress: Int) {
        if (progress >= 0) super.setProgress(progress) else super.setProgress(0)
        onSizeChanged(x, y, z, w)
        if (changeListener != null) changeListener!!.onProgressChanged(this, progress, false)
    }
}

Example 2: android vertical seekbar github

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {

	protected OnSeekBarChangeListener changeListener;
	protected int x, y, z, w;

	public VerticalSeekBar(Context context) {
		super(context);
	}

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

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

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

		this.x = w;
		this.y = h;
		this.z = oldw;
		this.w = oldh;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(heightMeasureSpec, widthMeasureSpec);
		setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
	}

	@Override
	protected void onDraw(Canvas c) {
		c.rotate(-90);
		c.translate(-getHeight(), 0);
		super.onDraw(c);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!isEnabled()) {
			return false;
		}

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setSelected(true);
			setPressed(true);
			if (changeListener != null)
				changeListener.onStartTrackingTouch(this);
			break;
		case MotionEvent.ACTION_UP:
			setSelected(false);
			setPressed(false);
			if (changeListener != null)
				changeListener.onStopTrackingTouch(this);
			break;
		case MotionEvent.ACTION_MOVE:
			int progress = getMax()
					- (int) (getMax() * event.getY() / getHeight());
			setProgress(progress);
			onSizeChanged(getWidth(), getHeight(), 0, 0);
			if (changeListener != null)
				changeListener.onProgressChanged(this, progress, true);
			break;

		case MotionEvent.ACTION_CANCEL:
			break;
		}
		return true;
	}

	@Override
	public synchronized void setOnSeekBarChangeListener(
			OnSeekBarChangeListener listener) {
		changeListener = listener;
	}

	@Override
	public synchronized void setProgress(int progress) {
		if (progress >= 0)
			super.setProgress(progress);

		else
			super.setProgress(0);
		onSizeChanged(x, y, z, w);
		if (changeListener != null)
			changeListener.onProgressChanged(this, progress, false);
	}
}

Tags:

Misc Example