Copy codeThe Code is as follows: public class PageWidget extends View {
Private Bitmap foreImage;
Private Bitmap bgImage;
Private PointF touchPt;
Private int screenWidth;
Private int screenHeight;
Private GradientDrawable shadowDrawableRL;
Private GradientDrawable shadowDrawableLR;
Private ColorMatrixColorFilter mColorMatrixFilter;
Private Scroller mScroller;
Private int lastTouchX;
Public PageWidget (Context context ){
Super (context );
// TODO Auto-generated constructor stub
TouchPt = new PointF (-1,-1 );
// Argb a (0-transparent, 255-opaque)
Int [] color = {0xb0333333, 0x00333333 };
ShadowDrawableRL = new GradientDrawable (GradientDrawable. Orientation. RIGHT_LEFT, color );
ShadowDrawableRL. setGradientType (GradientDrawable. LINEAR_GRADIENT );
ShadowDrawableLR = new GradientDrawable (GradientDrawable. Orientation. LEFT_RIGHT, color );
ShadowDrawableLR. setGradientType (GradientDrawable. LINEAR_GRADIENT );
Float array [] = {0.55f, 0, 0, 0, 80366f,
, 0.55f, 0, 0, 80366f,
, 0, 0.55f, 0, 80366f,
, 0, 0, 0.2f, 0 };
ColorMatrix cm = new ColorMatrix ();
Cm. set (array );
/*
* | A * 0.55 + 80 |
* | R * 0.55 + 80 |
* | G * 0.55 + 80 |
* | B x 0.2 |
*/
// Cm. setSaturation (0 );
MColorMatrixFilter = new ColorMatrixColorFilter (cm );
// Use the scroll bar to achieve the animation effect after the touch point is opened
MScroller = new Scroller (context );
}
@ Override
Public void computeScroll (){
// TODO Auto-generated method stub
If (mScroller. computescroloffset ()){
TouchPt. x = mScroller. getCurrX ();
TouchPt. y = mScroller. getCurrY ();
PostInvalidate ();
}
Else {
// TouchPt. x =-1;
// TouchPt. y =-1;
}
Super. computeScroll ();
}
Public void SetScreen (int screenWidth, int screenHeight ){
This. screenWidth = screenWidth;
This. screenHeight = screenHeight;
}
Public Bitmap getForeImage (){
Return foreImage;
}
Public void setForeImage (Bitmap foreImage ){
This. foreImage = foreImage;
}
Public Bitmap getBgImage (){
Return bgImage;
}
Public void setBgImage (Bitmap bgImage ){
This. bgImage = bgImage;
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
DrawPageEffect (canvas );
Super. onDraw (canvas );
}
/**
* Draw foreground images
* @ Param canvas
*/
Private void drawForceImage (Canvas canvas ){
// TODO Auto-generated method stub
Paint mPaint = new Paint ();
If (foreImage! = Null ){
Canvas. drawBitmap (foreImage, 0, 0, mPaint );
}
}
/**
* Draw background images
* @ Param canvas
*/
Private void drawBgImage (Canvas canvas, Path path ){
// TODO Auto-generated method stub
Paint mPaint = new Paint ();
If (bgImage! = Null ){
Canvas. save ();
// Draw a picture only at the intersection of the path
Canvas. clipPath (path, Op. INTERSECT );
Canvas. drawBitmap (bgImage, 0, 0, mPaint );
Canvas. restore ();
}
}
/**
* Page flip Effect
* @ Param canvas
*/
Private void drawPageEffect (Canvas canvas ){
// TODO Auto-generated method stub
DrawForceImage (canvas );
Paint mPaint = new Paint ();
If (touchPt. x! =-1 & touchPt. y! =-1 ){
// Flip the left side of the book
Canvas. drawLine (touchPt. x, 0, touchPt. x, screenHeight, mPaint );
// Draw a shadow on the left side of the book
ShadowDrawableRL. setBounds (int) touchPt. x-20, 0, (int) touchPt. x, screenHeight );
ShadowDrawableRL. draw (canvas );
// Fold the page
Float halfCut = touchPt. x + (screenWidth-touchPt. x)/2;
Canvas. drawLine (halfCut, 0, halfCut, screenHeight, mPaint );
// Flip the page on the left of the fold to the back of the picture
Rect backArea = new Rect (int) touchPt. x, 0, (int) halfCut, screenHeight );
Paint backPaint = new Paint ();
BackPaint. setColor (0xffdacab0 );
Canvas. drawRect (backArea, backPaint );
// Flip the front of the flip image horizontally and pan it to touchPt. x.
Paint fbPaint = new Paint ();
FbPaint. setColorFilter (mColorMatrixFilter );
Matrix matrix = new Matrix ();
Matrix. preScale (-1, 1 );
Matrix. postTranslate (foreImage. getWidth () + touchPt. x, 0 );
Canvas. save ();
Canvas. clipRect (backArea );
Canvas. drawBitmap (foreImage, matrix, fbPaint );
Canvas. restore ();
// Draw the left shadow at the fold.
ShadowDrawableRL. setBounds (int) halfCut-50, 0, (int) halfCut, screenHeight );
ShadowDrawableRL. draw (canvas );
Path bgPath = new Path ();
// Display the area of the background image
BgPath. addRect (new RectF (halfCut, 0, screenWidth, screenHeight), Direction. CW );
// Fold out the background on the right
DrawBgImage (canvas, bgPath );
// Draw the right shadow at the fold
ShadowDrawableLR. setBounds (int) halfCut, 0, (int) halfCut + 50, screenHeight );
ShadowDrawableLR. draw (canvas );
}
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
// TODO Auto-generated method stub
If (event. getAction () = MotionEvent. ACTION_DOWN ){
TouchPt. x = event. getX ();
TouchPt. y = event. getY ();
}
Else if (event. getAction () = MotionEvent. ACTION_MOVE ){
LastTouchX = (int) touchPt. x;
TouchPt. x = event. getX ();
TouchPt. y = event. getY ();
PostInvalidate ();
}
Else if (event. getAction () = MotionEvent. ACTION_UP ){
Int dx, dy;
Dy = 0;
// Slide to the right
If (lastTouchX <touchPt. x ){
Dx = foreImage. getWidth ()-(int) touchPt. x + 30;
}
Else {
// Slide left
Dx =-(int) touchPt. x-foreImage. getWidth ();
}
MScroller. startScroll (int) touchPt. x, (int) touchPt. y, dx, dy, 1000 );
PostInvalidate ();
}
// The value must be true. Otherwise, the ACTION_MOVE and ACTION_UP events cannot be obtained.
Return true;
}
}
Public class PageActivity extends Activity {
Protected void onCreate (android. OS. Bundle savedInstanceState ){
RequestWindowFeature (Window. FEATURE_NO_TITLE );
PageWidget pageWidget = new PageWidget (this );
Display display = getWindowManager (). getDefaultDisplay ();
Int width = display. getWidth ();
Int height = display. getHeight ();
PageWidget. SetScreen (width, height );
Bitmap bm1 = BitmapFactory. decodeResource (getResources (), R. drawable. pre7 );
Bitmap bm2 = BitmapFactory. decodeResource (getResources (), R. drawable. after7 );
Bitmap foreImage = Bitmap. createScaledBitmap (bm1, width, height, false );
Bitmap bgImage = Bitmap. createScaledBitmap (bm2, width, height, false );
PageWidget. setBgImage (bgImage );
PageWidget. setForeImage (foreImage );
SetContentView (pageWidget );
Super. onCreate (savedInstanceState );
};
}