我怎樣才能在安卓系統中找到一個垂直的SeekBar ? - How can I get a working vertical SeekBar in Android? -开发者知识库

我怎樣才能在安卓系統中找到一個垂直的SeekBar ? - How can I get a working vertical SeekBar in Android? -开发者知识库,第1张

I've implemented the commonly-pointed-to VerticalSeekBar post here. As it is, the SeekBar operates a little quirky. Here is my slightly adapted onTouchEvent() from that example:

我在這里實現了通常指向垂直方向的鏈接。實際上,SeekBar的運作方式有點古怪。下面是我對onTouchEvent()稍加修改的示例:

public boolean onTouchEvent(MotionEvent event)
    {
            xPos = event.getX();
            yPos = event.getY();
            oOffset = this.getThumbOffset();
            oProgress = this.getProgress();

            //Code from example - Not working
            //this.setThumbOffset( progress * (this.getBottom()-this.getTop()) );

            this.setProgress((int)(29*yPos/this.getBottom()));
            return true;
    }

I've managed to implement one VerticalSeekBar in which the progress updates as expected and is fully-functional, but the thumb does not follow suit. This is only a graphical glitch, so I'm overlooking it for now. But, it would be nice to have that working. This SeekBar has max = 20.

我已經實現了一個垂直的seekbar,其中進度更新如預期,並且功能齊全,但是拇指沒有跟隨。這只是一個圖形化的小故障,所以我現在可以忽略它。但是,如果能讓它發揮作用就好了。這個SeekBar的最大值是20。

However, I tried implementing another VerticalSeekBar with max = 1000. Obviously, it uses the same code, so you'd assume the same behavior. I'm only able to achieve a progress of 0~35, even as my finger slides beyond the SeekBar and eventually off the screen. If I just tap near the end of the progress bar (which should be progress ~ 900) it returns a progress of about 35 and the yellow progress bar reflects that value by staying near the top.

但是,我嘗試使用max = 1000實現另一個垂直的seekbar。顯然,它使用了相同的代碼,因此您將假設相同的行為。我只能取得0~35的進步,即使我的手指滑過了SeekBar,最終離開了屏幕。如果我只在進度條的末尾點擊(應該是progress ~ 900),它會返回一個大約35的進度,黃色進度條會通過保持接近頂部來反映這個值。

My question is: Does anyone have a link to a working vertical SeekBar, or know how to adapt this particular example?

我的問題是:是否有人與正在工作的垂直SeekBar有關聯,或者知道如何適應這個特定的示例?

8 个解决方案

#1


138  

Here is a working VerticalSeekBar implementation:

下面是一個有效的垂直seekbar實現:

package android.widget;

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

public class VerticalSeekBar extends SeekBar {

    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);
    }

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

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

    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:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

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

To implement it, create a new class in your project, choosing the right package:

要實現它,在項目中創建一個新類,選擇正確的包:

我怎樣才能在安卓系統中找到一個垂直的SeekBar ? - How can I get a working vertical SeekBar in Android? -开发者知识库,第2张

There, paste the code and save it. Now use it in your XML layout:

在那里,粘貼代碼並保存它。現在在XML布局中使用它:

<android.widget.VerticalSeekBar
  android:id="@ id/seekBar1"
  android:layout_width="wrap_content"
  android:layout_height="200dp"
  />

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复