我怎样才能在Android的工作垂直搜索栏? [英] How can I get a working vertical SeekBar in Android?

查看:123
本文介绍了我怎样才能在Android的工作垂直搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了共同指向的VerticalSeekBar后<一href="http://stackoverflow.com/questions/631238/modifying-the-android-seekbar-widget-to-operate-vertically">here.正因为如此,在搜索栏操作有点古怪。这是我稍微适应的onTouchEvent()从这个例子:

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:

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

我已经成功地实现其最新进展的预期,是全功能性的VerticalSeekBar,但拇指不跟风。这仅仅是一个图形化的小故障,所以我俯瞰它现在。但是,它的将会的是不错的工作。这个搜索栏有最大= 20

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.

不过,我想再添VerticalSeekBar与最大= 1000 。很显然,它使用相同的code,所以你承担相同的行为。我只能够实现0〜35的进度,即使我超越了搜索栏手指滑动,最终在屏幕上。如果我只是附近的进度栏的末尾挖掘(其中的应该的是进度〜900 )将返回约35进度和黄色的进展酒吧反映了价值通过保持接近顶部。

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.

我的问题是:有没有人有一个链接到一个工作垂直搜索栏,的知道如何去适应这个特殊的例子

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

推荐答案

下面是一个工作VerticalSeekBar实现:

Here is a working VerticalSeekBar implementation:

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:

在那里,贴code和保存。现在用它在你的XML布局:

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

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

这篇关于我怎样才能在Android的工作垂直搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆