Imitating the unlocking interface of android4.0

Source: Internet
Author: User

Mark forwarding from: http://blog.csdn.net/zhanwei_30/article/details/7624025

I have been working for a year now. I have been reading other people's blogs every day. I have also started to write a blog. I would like to briefly summarize some of my projects in the past with the following ideas:

1. Observe the screen lock and unlock page details for multiple times. You need to process the size of several images and images before making a static interface-customize the layout you want.

Public class circle_view extends framelayout {
Private context mcontext;
Private imageview lock_circle, lockview, anchorview, outsideview;
Private int WPS = viewgroup. layoutparams. fill_parent;
// Determine whether the circle is inside the circle when the finger leaves.
Private Boolean isoutcircle = false;
// Record the starting position of the anchorview. When the finger leaves, it returns to the original position when it is in the circle.
Private int anchorview_left;
Private int anchorview_top;
Private int anchorview_right;
Private int anchorview_bottom;
// Y coordinate of the upper critical point
Int critical_top = 0;
// Y coordinate of the lower critical point
Int critical_bottom = 0;
Private vibrator mvibrator;
Private rect mrect;
Private Boolean mtracking;
Private Boolean mtriggered = false;

Public circle_view (context ){
Super (context );
Mcontext = context;
Setup ();
}

Public circle_view (context, attributeset attrs ){
Super (context, attrs );
Mcontext = context;
Setup ();
}

Public circle_view (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
Mcontext = context;
}

@ Override
Protected void onlayout (Boolean arg0, int arg1, int arg2, int arg3, int arg4 ){
Final int COUNT = getchildcount ();
For (INT I = 0; I <count; I ++ ){
Final view child = getchildat (I );
Int left = getmeasuredwidth ()/2-Child. getmeasuredwidth ()/2;
Int right = left + child. getmeasuredwidth ();
Int Top = getmeasuredheight ()/2-Child. getmeasuredheight ()/2;
Int Bottom = Top + child. getmeasuredheight ();
Child. layout (left, top, right, bottom );
}
}

@ Override
Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
Super. onmeasure (widthmeasurespec, heightmeasurespec );
Final int COUNT = getchildcount ();
For (INT I = 0; I <count; I ++ ){
Final view child = getchildat (I );
Final int width = Child. getmeasuredwidth ();
Final int Height = Child. getmeasuredheight ();
Getchildat (I). Measure (width, height );
}
}

Private void setup (){
Mvibrator = (vibrator) mcontext. getsystemservice (context. vibrator_service );
Mrect = new rect ();

Outsideview = new imageview (mcontext );
Drawable out = getresources (). getdrawable (R. drawable. Outside );
Outsideview. setimagebitmap (getsmallbitmap (out ));
Addview (outsideview, 0 );

Imageview insideview = new imageview (mcontext );
Drawable in = getresources (). getdrawable (R. drawable. Inside );
Insideview. setimagebitmap (getsmallbitmap (in ));
Addview (insideview, 1 );

Anchorview = new imageview (mcontext );
Int width = 50;
Int Height = 50;
Bitmap anchor = bitmap. createscaledbitmap (bitmapdrawable) Out). getbitmap (), width, height, true );
Anchorview. setimagebitmap (Anchor );
Addview (anchorview, 2 );

Lockview = new imageview (mcontext );
Drawable lock = getresources (). getdrawable (R. drawable. Lock );
Lockview. setimagedrawable (Lock );
Addview (lockview, 3 );

Lock_circle = new imageview (mcontext );
Drawable Circle = getresources (). getdrawable (R. drawable. lock_cricle );
Lock_circle.setimagebitmap (getsmallbitmap (circle ));
Lock_circle.setlayoutparams (New layoutparams (WPS, WPS ));
Addview (lock_circle, 4 );
}

Bitmap getsmallbitmap (drawable ){
Bitmap bitmap = (bitmapdrawable) drawable). getbitmap ();
Bitmap newbitmap = bitmap. createscaledbitmap (bitmap, bitmap. getwidth ()/2, bitmap. getheight ()/2, true );
Return newbitmap;
}

2. Determine the animation position during the drag process-calculate the intersection of the straight line and the circle (note that attention must be paid to the accuracy of the processing of points with no slope)

@ Override
Public Boolean onintercepttouchevent (motionevent eV ){
Final int action = eV. getaction ();
Final int x = (INT) eV. getx ();
Final int y = (INT) eV. Gety ();
// Determine whether the position of the finger is in the unlocked circle
Lock_circle.gethitrect (mrect );
Boolean isvalid = mrect. Contains (x, y );

// When you click it again, if the lock ring is hidden, the lock ring will be immediately displayed at the center of the circle.
If (lock_circle.getvisibility () = 4 ){
Lockview. setvisibility (0 );
Lock_circle.layout (anchorview_left, anchorview_top, anchorview_right, anchorview_bottom );
Lock_circle.setvisibility (0 );
}
// Determine the interception before finger movement
If (! Mtracking &&! Isvalid)
Return false;
Switch (Action ){
Case motionevent. action_down:
Mtracking = true;
Final view v = lock_circle;
Lockview. setvisibility (view. Invisible );
Scale_big (action );
Anchorview_left = V. getleft ();
Anchorview_top = V. gettop ();
Anchorview_right = V. getright ();
Anchorview_bottom = V. getbottom ();
Mvibrator. vibrate (50 );
Break;

}

Return true;
}

@ Override
Public Boolean ontouchevent (motionevent event ){
If (mtracking ){
Final int action = event. getaction ();
Final view v = lock_circle;
Final int width = V. getright ()-V. getleft ();
Final int Height = V. getbottom ()-V. gettop ();
Final int radius_big = math. Round (outsideview. getmeasuredwidth ()/2.0f );
Critical_top = outsideview. gettop ();
Critical_bottom = outsideview. getbottom ();
// System. Out. println ("the upper critical point is" + critical_top + "<---> the lower critical point is" +
// Critical_bottom );
Final int radius_x = outsideview. getleft () + radius_big;
Final int radius_y = outsideview. gettop () + radius_big;
Final int radius_small = math. Round (width/2.0f );
Switch (Action ){

Case motionevent. action_move:
Int SpaceX = 0;
Int spacey = 0;
// Final int A = (0-(new random (). nextint (10) + 5 );
Final int x = (INT) event. getx ();
Final int y = (INT) event. Gety ();
Final Boolean flag = mathex_circle.iscrosspoint (radius_x, radius_y, radius_big, x, y );
// Within the circle
If (! Flag ){
SpaceX = X;
Spacey = y;
} Else {
// Determine whether the touch is valid
/*
* Boolean validetouch_x_y =
* Mathex_circle.isvalidetouchxy (radius_x, radius_y,
* Radius_big, x, y); If (! Validetouch_x_y) return true;
*/
// Here we need to take a certain range. All the values here are int type. When float is strongly converted to int type, we will remove the decimal number. When the calculation rate is not accurate enough, the Java precision is not enough. it may not be too accurate. The value around the critical point will always have a large error in the calculation, including whether or not it is in the circle. After repeated experiments, I feel that the critical range is just the diameter of the small circle.
If (x> radius_x-radius_small & x <radius_x + radius_small ){
SpaceX = radius_x;
If (Y <= critical_top ){
Spacey = critical_top;
}
Else if (Y> = critical_bottom ){
Spacey = critical_bottom;
}
If (spacey = 0) spacey = critical_bottom ;;
} Else {
SpaceX = mathex_circle.calxbothonlineandcircle (radius_x, radius_y, radius_big, x, y );
Spacey = mathex_circle.calyfromxonline (radius_x, radius_y, X, Y, SpaceX );
}

}

System. err. println ("the center coordinate is" + "x =" + radius_x + "\ t y =" + radius_y + "the finger position is X =" + x + "\ t y = "+ Y +" the diameter of the small circle is width = "+ width +" \ t Height = "+ height +" ---- measurewidth = "+ v. getmeasuredwidth () + "\ t getleft =" + v. getleft () + "\ t getright =" + v. getright ()
+ "\ T gettop =" + v. gettop ()
+ "\ Tgetbottom =" + v. getbottom () + "the radius of the circle is" + radius_big );

System. Err. println ("SpaceX =" + SpaceX + "\ t spacey =" + Spacey );

If (spacey <critical_top ){
Spacey = critical_top;
}

If (spacey> critical_bottom ){
Spacey = critical_bottom;
}

Int L = SpaceX-radius_small;
Int T = spacey-radius_small;
Int r = SpaceX + radius_small;
Int B = spacey + radius_small;

V. layout (L, t, R, B );
Break;
Case motionevent. action_up:
// Processing after finger exit
Final int rawx = (INT) event. getx ();
Final int rawy = (INT) event. Gety ();
Isoutcircle = mathex_circle.iscrosspoint (radius_x, radius_y, radius_big, rawx, rawy );
Scale_big (action );
Break;
}
}
Return mtracking | super. ontouchevent (event );

}

Private void scale_big (INT action ){
Final int anchor_width = anchorview. getdrawable (). getintrinsicwidth ();
Final int outcircle_width = outsideview. getdrawable (). getintrinsicwidth ();
Final float scale = (outcircle_width * 1.00f)/(anchor_width * 1.00f );
Scaleanimation animation = new scaleanimation (1.00f, scale, 1.00f, scale, animation. relative_to_self, 0.50f, animation. relative_to_self, 0.50f );
Animation. setfillafter (true );
Animation. setduration (1000 );
Animation. setanimationlistener (New myanimationlistener (Action ));
Anchorview. startanimation (animation );
}

Class myanimationlistener implements animationlistener {
// Sliding action
Int action;
Boolean inside_circle;

Public myanimationlistener (INT action ){
This. Action = action;
}

@ Override
Public void onanimationend (animation ){
Switch (Action ){
Case motionevent. action_down:
Case motionevent. action_move:
Animation. setrepeatmode (animation. Restart );
Animation. setrepeatcount (animation. Infinite );
Break;
Case motionevent. action_up:
Outsideview. setvisibility (0 );
Lockview. setvisibility (0 );
Lock_circle.layout (anchorview_left, anchorview_top, anchorview_right, anchorview_bottom );
Lock_circle.setvisibility (0 );
Anchorview. setvisibility (0 );
Anchorview. clearanimation ();
Animation. Cancel ();
Break;
}

}

@ Override
Public void onanimationrepeat (animation ){
}

@ Override
Public void onanimationstart (animation ){
If (Action = motionevent. action_up ){

If (! Isoutcircle ){
Outsideview. setvisibility (4 );
Lock_circle.setvisibility (4 );
Anchorview. setvisibility (4 );
} Else {
// When the finger leaves, the animation is immediately unlocked.
Anchorview. clearanimation ();
Animation. Cancel ();
Anchorview. setvisibility (4 );
Mvibrator. vibrate (100 );
Toast. maketext (mcontext, "unlock here", 3000). Show ();
}
}

3. The reference mathex_circle class in the above class is a static class that encapsulates all the methods for dealing with the relationship between a straight line and a circle.
The main code is as follows:

/**
* Slope of Two-point coordinates is known (x1 = x2 is not considered here)
* @ Param X1 X coordinate of point 1
* @ Param Y1: Y coordinate of point 2
* @ Param X2 X coordinate of point 3
* @ Param Y2 the Y coordinate of Point 4
* @ Return
* @ Throws exception
*/
Public static double calslope (INT X1, int Y1, int X2, int Y2 ){
Return (double) (Y2-Y1)/(double) (x2-X1 );
}

/**
* Obtain the intercept based on the coordinates of the slope and a point in the straight line.
* @ Param X point X coordinate
* @ Param Y: Y coordinate
* @ Param K Slope
* @ Return
*/
Public static double calintercept (int x, int y, Double K ){
Return y-K * X;
}

/**
* If the X coordinate, slope k, and intercept length are known as B, use y = kx + B to calculate y coordinate.
* @ Param x
* @ Param K
* @ Param B
* @ Return
*/
Public static int calyfromxonline (int x, Double K, double B ){
Return (INT) (K * x + B );
}

/**
* The two coordinate points (x1, Y1), (X2, Y2), and the third X coordinate are known on the straight line, and the third y coordinate is obtained.
* @ Param X1 X coordinate of point 1
* @ Param Y1 the Y coordinate of point 1
* @ Param X2 X coordinate of point 2
* @ Param Y2: Y coordinate of point 2
* @ Param x coordinate of the third point
* @ Return
*/
Public static int calyfromxonline (INT X1, int Y1, int X2, int Y2, int X ){
Double K = calslope (x1, Y1, X2, Y2 );
Double B = calintercept (x1, Y1, k );
Return calyfromxonline (x, K, B );
}

/**
* Evaluate (X-cx) ^ 2 + (Y-cy) ^ 2 = radius ^ 2 coordinate of the intersection of the Square Program and Y = kx + B linear equation x (the logical judgment of whether the two have an intersection is omitted here)
* @ Param circlecx
* @ Param circlecy
* @ Param circleradius
* @ Param K
* @ Param B
* @ Param y
* @ Return
*/
Public static int [] calxbothonlineandcircle (int cx, int cy, int radius,
Double K, double B ){
Double parama = K * k + 1;
Double paramb = 2 * K * (B-cy)-2 * CX;
Double paramc = Cx * cx + (B-cy) * (B-cy)-radius * radius;

Int [] result = new int [2];

Result [0] = (INT) (-paramb + math. SQRT (paramb * paramb-4 * parama
* Paramc)/(2 * parama ));
Result [1] = (INT) (-paramb-math. SQRT (paramb * paramb-4 * parama
* Paramc)/(2 * parama ));
Return result;
}

/**
* The coordinates and radius of the center are known. A point and the center of the circle form a ray with the endpoint of the center. The X coordinate of the intersection of the ray and the circle is calculated.
* @ Param CX
* @ Param cy
* @ Param radius
* @ Param x
* @ Param y
* @ Return
*/
Public static int calxbothonlineandcircle (int cx, int cy, int radius,
Int X, int y ){
Double K = calslope (CX, Cy, x, y );
Double B = calintercept (X, Y, k );
Int [] tempresult = calxbothonlineandcircle (CX, Cy, radius, K, B );
Return x> CX? Math. Max (tempresult [0], tempresult [1]): Math. Min (
Tempresult [0], tempresult [1]);
}

 

4. Next, let's talk about how to port it to the source code.

Place mathex_circle in the same directory as frameworks/base/CORE/Java/COM/Android/Internal/widget/and sildingtab.

Then, we can port the code in circle_view to the silding, and modify the code if there is a problem with the deletion and compilation process.

5. In frameworks/base/CORE/RES/*** layout. XML (find the layout interface suitable for your mobile phone screen), find keyguard_screen_tab_unlock _. xml keyguard_screen_tab_unlock_land.xml

Delete some unwanted items (refer to the 4.0 interface). Only adjust the height of the sliding layout after the time-based alarm clock is displayed for the emergency phone charging display.

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.