gpt4 book ai didi

android - 如何暂停 Canvas 以特定角度旋转 2 秒?

转载 作者:IT老高 更新时间:2023-10-28 23:00:53 29 4
gpt4 key购买 nike

我做了一个旋钮,但我想在特定角度停止旋钮 2 秒。我想在 260f 和 -20f 停止它。

谁能建议怎么做?

这是来自博客的代码。我根据自己的要求做了很多改动。

public class RotatoryKnobView extends ImageView  {

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

private RotaryKnobListener listener;

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

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

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

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

public RotatoryKnobView(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.rotoron);
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 += 5*direction;
notifyListener(angle+20);
break;
}
return true;
}
});
}

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

protected void onDraw(Canvas c)
{if(angle==257f){
try {
synchronized (c) {

c.wait(5000);
angle=260f;
}

} catch (InterruptedException e) {
}
}
else if(angle==-16f)
{
try {
synchronized (c) {
c.wait(5000);
angle=-20f;
}

} catch (InterruptedException e) {

}
}
else
if(angle>260f)
{

angle=-20f;
}
else if(angle<-20f)
{

angle=260f;
}
else{
c.rotate(angle,getWidth()/2,getHeight()/2);

}
super.onDraw(c);
}
}

最佳答案

你可以设置一个固定的角​​度,2秒后使用postDelayed清除。

    public class RotatoryKnobView extends ImageView {

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

private RotaryKnobListener listener;

private Float fixedAngle;
private float settleAngle;

private Runnable unsetFixedAngle = new Runnable() {
@Override
public void run() {
angle = settleAngle;
fixedAngle = null;
invalidate();
}
};

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

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

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

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

public RotatoryKnobView(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.rotoron);
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 += 5*direction;
notifyListener(angle+20);
break;
}
return true;
}
});
}

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

void setFixedAngle(float angle, float settleAngle) {
fixedAngle = angle;
this.settleAngle = settleAngle;
postDelayed(unsetFixedAngle, 2000);
}

protected void onDraw(Canvas c)
{
if(fixedAngle==null) {
if (angle > 270) {
setFixedAngle(270, -15);
} else if (angle < -20f) {
setFixedAngle(-20, 260);
}
}
Log.d("angle", "angle: " + angle + " fixed angle: " + fixedAngle);
c.rotate(fixedAngle == null ? angle : fixedAngle,getWidth()/2,getHeight()/2);

super.onDraw(c);
}
}

`

关于android - 如何暂停 Canvas 以特定角度旋转 2 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759871/

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