java - RotaryKnob using get progress value in text view -
i refereed website can't understand how can value rotary knob. given below tried sample code. need means when rotate right side knob want increase value in text view @ same time left side rotate means decrease value in text view. minimum value 0 , maximum value 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" />
your listener isn't informed total progress; turn direction, can seen here:
int direction = (delta_theta > 0) ? 1 : -1; notifylistener(direction);
so track , sum changes in 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)); } }
Comments
Post a Comment