views:

4726

answers:

4

I'm trying to get a vertical seekbar going with the emulator, but I'm sort of stuck. I can get the seekbar to display the way I want it to, and I can get the progress to do what I want, and I can modify the onTouchEvent to get the thumb to go vertically instead of horizontally. What I can't do is get the thumb to move outside of the default 29 horizontal pixels without using setThumbOffset(). This in itself isn't a problem. The problem is coming from the fact that I don't understand the thumbOffset at all -- I guess. I think I could (properly) resize the widget, which I am pretty sure I'm not doing right. Or maybe I could just use the thumbOffset if I could figure it out. Since I can calculate the progress correctly I thought I would just use a linear function of progress * (getTop() - getBottom()) of the widget but that doesn't seem to do it. But I can't figure out what the offset is centered around.

As a somewhat aside, I am really unsure if what I am doing in onSizeChanged() is sane or if it's going to bite me in the ass later on.

Here's the main.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.mobilsemantic.mobipoll.SlideBar
        android:id="@+id/slide"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:max="100"
        android:progress="0"
        android:secondaryProgress="25" />

        <Button android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Hello, I am a Button" />

    <TextView android:id="@+id/tracking"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

And the class (ignore the debugging junk):

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

public class SlideBar extends SeekBar {

        private int oHeight = 320, oWidth = 29;
        private int oProgress = -1, oOffset = -1;;
        private float xPos = -1, yPos = -1;
        private int top = -1, bottom = -1, left = -1, right = -1;

        public SlideBar(Context context) {
                super(context);
        }
        public SlideBar(Context context, AttributeSet attrs)
        {
                super(context, attrs);
                oOffset = this.getThumbOffset();
                oProgress = this.getProgress();
        }
        public SlideBar(Context context, AttributeSet attrs, int defStyle)
        {
                super(context, attrs, defStyle);
        }

        protected synchronized void onMeasure(int widthMeasureSpec, intheightMeasureSpec)
        {
                int height = View.MeasureSpec.getSize(heightMeasureSpec);
                oHeight = height;
                this.setMeasuredDimension(oWidth, oHeight);

        }
        protected void onSizeChanged(int w, int h, int oldw, int oldh)
        {
                super.onSizeChanged(h, w, oldw, oldh);
        }
        protected void onLayout(boolean changed, int l, int t, int r, int b)
        {
                super.onLayout(changed, l, t, r, b);
                left = l;
                right = r;
                top = t;
                bottom = b;
        }
        protected void onDraw(Canvas c)
        {
                c.rotate(90);
                c.translate(0,-29);
                super.onDraw(c);
        }
        public boolean onTouchEvent(MotionEvent event)
        {
                xPos = event.getX();
                yPos = event.getY();
                float progress = (yPos-this.getTop())/(this.getBottom()-this.getTop());
                oOffset = this.getThumbOffset();
                oProgress = this.getProgress();
                Log.d("offset" + System.nanoTime(), new Integer(oOffset).toString());
                Log.d("progress" + System.nanoTime(), new Integer(oProgress).toString());

                float offset;

                offset = progress * (this.getBottom()-this.getTop());

                this.setThumbOffset((int)offset);

                Log.d("offset_postsetprogress" + System.nanoTime(), new Integer(oOffset).toString());
                Log.d("progress_postsetprogress" + System.nanoTime(), new Integer(oProgress).toString());

                this.setProgress((int)(100*event.getY()/this.getBottom()));
                return true;
        }

}
+1  A: 

Take a look at android source . I think you need to change at least trackTouchEvent and there maybe a few other places where you also need to swap the x,y coordinates to take into account your rotation of the control.

hacken
A: 

Hi, I am also stucking in this problem.. float progress = getThumbOffset() + (getMax() * (yPos - getTop()))/(getBottom() - getTop()); I am getting the right progress.. But Seek Thumb is not adjusting. I think, when we call setProgress(), thumb should adjust automatically.. if you solve this probs.. please help me out too..

Thanks, Peeyush [email protected]

A: 

please help me out too...

[email protected]

nam ki seok
+1  A: 

you can download at http://560b.sakura.ne.jp/android/VerticalSlidebarExample.zip, i hope this may can help you

pengwang