java - 旋转旋钮在 TextView 中使用获取进度值

标签 java android eclipse android-studio

我推荐了一些网站,但我不明白如何从旋钮中获取值(value)。下面给出了我尝试的示例代码。我需要的意思是,当旋转右侧旋钮时,我想增加 TextView 中的值,同时左侧旋转意味着减少 TextView 中的值。最小值为 0,最大值为 25。

RotaryKnobView class here

public class RotaryKnobView extends ImageView  {

private float angle = 0f;
private float theta_old=0f;

private RotaryKnobListener listener;

public interface RotaryKnobListener {
    public void onKnobChanged(int arg);
}

public void setKnobListener(RotaryKnobListener l )
{
    listener = l;
}

public RotaryKnobView(Context context) {
    super(context);
    initialize();
}

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

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

private float getTheta(float x, float y)
{
    float sx = x - (getWidth() / 2.0f);
    float sy = y - (getHeight() / 2.0f);

    float length = (float)Math.sqrt( sx*sx + sy*sy);
    float nx = sx / length;
    float ny = sy / length;
    float theta = (float)Math.atan2( ny, nx );

    final float rad2deg = (float)(180.0/Math.PI);
    float thetaDeg = theta*rad2deg;

    return (thetaDeg < 0) ? thetaDeg + 360.0f : thetaDeg;
}

public void initialize()
{
    this.setImageResource(R.drawable.ic_launcher);
    setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX(0);
            float y = event.getY(0);
            float theta = getTheta(x,y);

            switch(event.getAction() & MotionEvent.ACTION_MASK)
            {
            case MotionEvent.ACTION_POINTER_DOWN:
                theta_old = theta;
                break;
            case MotionEvent.ACTION_MOVE:
                invalidate();
                float delta_theta = theta - theta_old;
                theta_old = theta;
                int direction = (delta_theta > 0) ? 1 : -1;
                angle += 3*direction;
                notifyListener(direction);
                break;
            }
            return true;
        }
    });
}

private void notifyListener(int arg)
{
    if (null!=listener)
        listener.onKnobChanged(arg);
}

protected void onDraw(Canvas c)
{
    c.rotate(angle,getWidth()/2,getHeight()/2);   
    super.onDraw(c);
}

Main Activity class code here

final TextView tView = (TextView)findViewById(R.id.tv);
    RotaryKnobView jogView = (RotaryKnobView)findViewById(R.id.knob);
    jogView.setKnobListener(new RotaryKnobView.RotaryKnobListener()
    {
        @Override
        public void onKnobChanged(int progress) {

            if (progress > 0){
                // rotate right 
                tView.setText(""+progress);
            } else{
                // rotate left 
                tView.setText(""+progress);
            }
        }
    });

MainActivity Xml code here

<com.example.ghvhjf.RotaryKnobView
    android:id="@+id/knob"
    android:layout_width="100dip"
    android:layout_height="100dip" />

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

最佳答案

您的听众不会获知总进度;就转弯方向而言,如下所示:

int direction = (delta_theta > 0) ? 1 : -1;
notifyListener(direction);

因此您可以在 RotaryKnobListener 中跟踪并总结这些更改:

new RotaryKnobView.RotaryKnobListener() {

    private int progress = 0;

    @Override
    public void onKnobChanged(int direction) {
        progress += direction;
        progress = Math.max(0, Math.min(25, progress));
        tView.setText(Integer.toString(progress));
    }
}

关于java - 旋转旋钮在 TextView 中使用获取进度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35033397/

相关文章:

java - 延迟会干扰我的计时逻辑吗?

java - 短路评估的性能影响

java - 当我在一项 Activity 中点击“返回”按钮时,我的应用程序崩溃了

android - 更新支持库后的 ActionBarSherlock 依赖问题

eclipse - 无法使用 https 和 SSH 链接在 Eclipse 中克隆 git 存储库

java - 关于更好的处理属性(property)的方法

java - 获取实现给定接口(interface)的所有类

android - 如何在ActionBar的正确位置设置文本?

java - 错误 org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException

java - Eclipse Java - 发生错误 (Win10)