gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:32:06 24 4
gpt4 key购买 nike

我推荐了一些网站,但我不明白如何从旋钮中获取值(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com