Some controls for Android

Source: Internet
Author: User
Tags gety

The original friend gave a display time sample, but also can change the time, but the machine has the root authority ability to change.


Change the image on the basis of the example on this timesheet. The background map can achieve the desired effect.





: http://download.csdn.net/detail/kongbaidepao/8090083




Examples of 1 windmill effects

is based on the speed of the hand slide.


Not much code, just stick it over.

public class Mainactivity extends Activity {    @Override public    void OnCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Rotatview rotatview= (Rotatview) Findviewbyid (R.id.myrotatview);        Rotatview.setrotatdrawableresource (r.drawable.cycle);    }}

public class Rotatview extends View {//original heart coordinate x float o_x;    float o_y;    /** * Handling Inertia of Handler */Handler Handler;    Handler processing the message interval int delayedtime = 20;    /** * Message signal, scrolling logo */static final int play = 0;    /** * message signal, stop scrolling the logo */static final int stop = 1;     /** * Last recorded time, calculate the Radian, calculate the speed of a certain time.    */Double currenttime = 0;    /** * Image Width */int width;    /** * Image Height */int height; /** * View's true width and height: because it is rotated.    So this view is a square, and its value is the diagonal length of the picture */double maxwidth;    /** * Rotated picture */Bitmap Rotatbitmap;        Public Rotatview (context context, AttributeSet Attrs) {Super (context, attrs);    Init ();        }/** * Initialize handler with speed Calculator */private void init () {Vrecord = new Vrecord (); Handler = new Handler () {@Override public void Handlemessage (Message msg) {Double D                Etatime = System.currenttimemillis ()-currenttime; Switch (MSG.What) {case play: {if (isclockwise) {speed = speed                            -A * detatime;                            if (speed <= 0) {return;                            } else {handler.sendemptymessagedelayed (play, delayedtime);                            }} else {speed = speed + A * detatime;                            if (speed >= 0) {return;                            } else {handler.sendemptymessagedelayed (play, delayedtime);                        }} adddegree ((float) (Speed * detatime + (A * detatime * detatime)/2));                        if (a < A_max) {//A = (float) (A + a_add*detatime);                        System.out.println ("A:" +a); // } currenttime = System.currenttimemillis ();                        Invalidate ();                    Break                        } case STOP: {speed = 0;                    Handler.removemessages (play);            }} super.handlemessage (msg);        }        };    By default, there is a picture of the initsize ();        } public void Setrotatbitmap (Bitmap Bitmap) {rotatbitmap = Bitmap;        Initsize ();    Postinvalidate (); The public void Setrotatdrawableresource (int id) {bitmapdrawable drawable = (bitmapdrawable) getcontext (). Getresou        RCEs (). getdrawable (ID);    Setrotatdrawable (drawable);        } public void Setrotatdrawable (Bitmapdrawable drawable) {rotatbitmap = Drawable.getbitmap ();        Initsize ();    Postinvalidate (); } private void Initsize () {if (Rotatbitmap = = null) {//throw new Nobitmaperror ("Error,no bitmap in rotatview! ");        Return        } width = Rotatbitmap.getwidth ();        Height = rotatbitmap.getheight ();                MaxWidth = math.sqrt (Width * width + height * height); o_x = O_y = (float) (MAXWIDTH/2);//Determine center coordinates}/** * This method to control the degree of rotation, assuming that more than 360, let it seek redundancy, prevent, the value is too large to cause the cross-border * * @param add        Ed */private void Adddegree (float added) {deta_degree + = added;        if (Deta_degree > | | Deta_degree < -360) {Deta_degree = deta_degree% 360;        }} @Override protected void OnDraw (canvas canvas) {Matrix matrix = new Matrix ();        Set the hinge position matrix.settranslate ((float) width/2, (float) HEIGHT/2);        Start turning matrix.prerotate (Deta_degree);        Shaft Reduction matrix.pretranslate (-(float) WIDTH/2,-(float) HEIGHT/2);        Send the location to the center of the View Matrix.posttranslate ((float) (maxwidth-width)/2, (float) (maxwidth-height)/2);    Canvas.drawbitmap (Rotatbitmap, matrix,paint);            Super.ondraw (canvas);    } Paint paint=new paint (); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method        Stub super.onmeasure (Widthmeasurespec, Heightmeasurespec);    Its width height is not the width height of the picture, but the length of the diagonal of the rectangle at a right angle of width setmeasureddimension (int) maxwidth, (int) maxwidth);    }/** * The initial x coordinates of the finger touch screen */float down_x;    /** * The coordinates of the initial y of the finger touch screen */float down_y;    /** * The coordinates of x when moving * */float target_x;    /** * The coordinates of y when moving * */float target_y;    /** * The coordinates of x when you let go * * float up_x;    /** * The coordinates of y when letting go * */float up_y;    /** * Current radians (dots in the center of the View) */float Current_degree;    /** * The radian when letting go (the center of the view is a dot) */float Up_degree;    /** * The arc of the current disc (dots at the center of the View) */float Deta_degree;    /** * Last gesture slip time */double lastmovetime = 0;    /** * Minimum acceleration (when the finger is letting go) */public static final float a_min = 0.001f; /** * Acceleration Increment     */public static final float a_add = 0.001f;    /** * acceleration */float a = a_min;    /** * Maximum acceleration (when finger is pressed) */public static final Float A_max = a_min * 5;    /** * rotational speed (degrees/msec) */double velocity = 0;    /** * Speed Calculator */Vrecord Vrecord;    /** * is clockwise rotation */Boolean isclockwise; @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated Method stub if (Rotatbi        TMap = = null) {throw new Nobitmaperror ("Error,no bitmap in rotatview!");                } switch (Event.getaction ()) {case Motionevent.action_down: {down_x = Event.getx ();                down_y = Event.gety ();                Current_degree = Detadegree (o_x, O_y, down_x, down_y);                Vrecord.reset ();                Handler.sendemptymessage (stop);                A = A_max;            Break } case motionevent.action_move: {down_x = target_x = Event. GetX ();                down_y = Target_y = Event.gety ();                float degree = detadegree (o_x, O_y, target_x, target_y);                Sliding arc increment float dete = degree-current_degree;                    Suppose that the less than-90 degree indicates that it spans weeks, requiring special handling of 350->17, if (Dete < -270) {dete = dete + 360; Assuming that it is greater than 90 degrees, it requires special handling -350-&GT;-17,} else if (Dete > +) {dete = Dete                -360;                } lastmovetime = System.currenttimemillis ();                Vrecord.add (Dete, lastmovetime);                Adddegree (DETE);                Current_degree = degree;                                Postinvalidate ();            Break                } case MotionEvent.ACTION_CANCEL:case Motionevent.action_up: {a = A_min;                Double lastuptime = System.currenttimemillis ();               Double detatime = lastuptime-lastmovetime; up_x = Event.getx ();                Up_y = Event.gety ();                Up_degree = Detadegree (o_x, O_y, up_x, up_y);                Speed at the time of letting go = velocity + vrecord.getspeed ();                if (Speed > 0) {speed = Math.min (vrecord.max_speed, speed);                } else {speed = Math.max (-vrecord.max_speed, speed);                }//System.out.println ("Speed:" + speed);                    if (Speed > 0) {isclockwise = true;                v = 1;                    } else {isclockwise = false;                v =-1;                } currenttime = System.currenttimemillis ();                Handler.sendemptymessage (0);            Break    }} return true; }/** * Calculates (src_x,src_y) as the coordinate circle point, establishes the right angle system, computes (target_x,target_y) coordinates and the x axis angle * Mainly uses the inverse tangent function knowledge to calculate the angle * * @param src_ X * @param src_y * @param target_x    * @param target_y * @return * * float detadegree (float src_x, float src_y, float target_x, float target_y) {        float detax = target_x-src_x;        float Detay = target_y-src_y;        Double D;            if (Detax! = 0) {Float tan = math.abs (detay/detax);                if (Detax > 0) {if (detay >= 0) {d = Math.atan (tan);                } else {d = 2 * Math.pi-math.atan (TAN);                }} else {if (detay >= 0) {d = Math.pi-math.atan (tan);                } else {d = Math.PI + Math.atan (tan);            }}} else {if (Detay > 0) {d = MATH.PI/2;            } else {d =-MATH.PI/2;    }} return (float) ((d * Delta)/MATH.PI); }/** * an exception. Used to infer if there is rotatbitmap * * @author sun.shine */Static Class NobitmaPerror extends RuntimeException {/** * */private static final long serialversionuid = 1L;        Public Nobitmaperror (String detailmessage) {super (detailmessage); }}/** * The speed calculator originally recorded the recent radians increment and point in time, and then <br> * calculated the average by dividing the total time by the increment to make the speed of its instant gesture slipping * * @author Sun.shi        NE */static class Vrecord {/** * * valid number in array */int addcount;        /** * Maximum available data space */public static final int length = 15;        /** * Two-dimensional array, 1. Save the radian increment. 2. Save the point in time that the increment was generated */double[][] record = new double[length][2]; /** * Load data for two-dimensional arrays <br> * Note: With this method, there is a feature that can record the last length of the data, the length of the outside will be lost * * @param Detad Egree * @param time */public void Add (double detadegree, double time) {for (int i = Le Ngth-1; i > 0;                i--) {record[i][0] = record[i-1][0]; RECORD[I][1] = record[i-1][1];            } record[0][0] = Detadegree;            RECORD[0][1] = time;        addcount++;        }/** * Maximum speed */public static final Double max_speed = 8; /** * Analyze the instantaneous speed <br> * Principle by the data loaded in the array: Calculates the total number of time and increment in the array. And then find the radian <br> * per millisecond * not more than {@link vrecord#max_speed} * * @return * *            Uble GetSpeed () {if (Addcount = = 0) {return 0;            } int maxindex = Math.min (addcount, length)-1;            if ((Record[0][1]-record[maxindex][1]) = = 0) {return 0;            } double detatime = record[0][1]-record[maxindex][1];            Double sumdegree = 0;                for (int i = 0; i < length-1; i++) {sumdegree + = record[i][0];            System.out.println (Record[i][0]);            }//System.out.println ("----------"); System.out.println (SumdegrEE);            System.out.println (Detatime);            Double result = Sumdegree/detatime;            if (Result > 0) {return math.min (result, max_speed);            } else {return Math.max (result,-max_speed);        }//System.out.println ("v=" + result);            }/** * Reset */public void Reset () {addcount = 0;                for (int i = length-1; i > 0; i--) {record[i][0] = 0;            RECORD[I][1] = 0;        }}} @Override protected void Ondetachedfromwindow () {Super.ondetachedfromwindow ();        if (rotatbitmap!=null) {rotatbitmap.recycle ();        Rotatbitmap=null; }    }}


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" wrap_content "    android:layout_height=" Wrap_content "    android:background= "#ffffffff" >    <com.sun.shine.myrotation.view.rotatview        android:id= "@+id/ Myrotatview "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android: layout_margin= "10dip" >    </com.sun.shine.myrotation.view.RotatView></RelativeLayout>






Some controls for Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.