मैंने घूर्णन घुंडी बनाया, लेकिन मैं 2 सेकंड के लिए विशिष्ट कोणों पर घुंडी को रोकना चाहता हूं। मैं इसे 260 एफ और -20 एफ पर रोकना चाहता हूं।विशिष्ट कोणों पर 2 सेकंड के लिए घूर्णन से कैनवास को कैसे रोकें?
क्या कोई सुझाव दे सकता है कि यह कैसे करें?
यह एक ब्लॉग से कोड है। मैंने अपनी आवश्यकताओं के अनुसार कई बदलाव किए हैं।
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);
}
}
कुछ क्रम नीचे दिए गए उत्तर को रद्द करने के लिए पूरी तरह से अलग करने के लिए सवाल बदलते बंद कर दें। –