Copying Netease cloud Music Album image folding carousel and Netease album pictures
Similar to Netease cloud Music Album image folding carousel
First, briefly describe the implementation principle 1. First, let our imageview ** start **
In fact, it is a constant. invalidate (); the function allows him to continuously call the ondarw function.
(Obviously, we don't need to call the ondarw function constantly. We only need to call it when we let it change. So we need to declare a variable to record whether the change record generates an animation)
2. implement our foldable Effect
Use of the setPolyToPoly method of Matrix
Reference (http://blog.csdn.net/lmj623565791/article/details/44278417)
3. Implement a one-on-one switch between images
Paint. setXfermode (xfermode); xfermode is a total of 16 processing methods to deal with the overlapping effect of two images.
Implement custom imageview attributes
List for storing bitmap
Animation interval variable
Animation swap interval variable
Two handler for sending messages
Foreground and background Indexes
Code
Package com. cust. myview; import java. util. arrayList; import java. util. list; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graphics. xfermode; import android. OS. ha Ndler; import android. util. attributeSet; import android. widget. imageView; import com. cust. uischool. r; public class musicImageView extends ImageView {private List <Bitmap> mbitmaps; private Paint paint; private Xfermode xfermode; private boolean change; // whether to change the private int bhjg = 5000; // transformation interval private int bhsj = 50; // transformation time private int frontindex; // foreground index private int backindex; // background index private boolean setbitma P = false; // whether to set bitmap; private Matrix matrix; private int count = 0; private int henght; public musicImageView (Context context) {this (context, null );} public musicImageView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public musicImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); paint = new Paint (); paint. setAntiAlias (true); xfermode = New porterduxfermode (Mode. SRC_OVER); change = false; matrix = new Matrix (); Bitmap map1 = BitmapFactory. decodeResource (getResources (), R. drawable. im2); Bitmap map2 = BitmapFactory. decodeResource (getResources (), R. drawable. st); List <Bitmap> mbitmaps = new ArrayList <Bitmap> (); mbitmaps. add (map1); mbitmaps. add (map2); setBitmaps (mbitmaps); henght = map1.getHeight (); bhjg = (int) Math. random () * (5000-2000) + 2000;} pub Lic void setBitmaps (List <Bitmap> mbitmaps) {this. mbitmaps = mbitmaps; setbitmap = true; if (mbitmaps. size ()> = 2) {frontindex = 0; backindex = 1;} else {frontindex = 0; backindex = 0;} new Thread () {public void run () {handler. postDelayed (run, bhjg); handler1.postDelayed (run1, bhsj );};}. start ();} Runnable run1 = new Runnable () {@ Override public void run () {if (change) {musicImageView. this. invalidate ();} Handler1.postDelayed (run1, bhsj) ;}}; Runnable run = new Runnable () {@ Override public void run () {change = true; handler. postDelayed (run, bhjg) ;}}; Handler handler1 = new Handler (); Handler handler = new Handler (); @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {Bitmap bmp = mbitmaps. get (backindex); setMeasuredDimension (bmp. getWidth (), bmp. getHeight () ;}@ Overr Ide protected void onDraw (Canvas canvas) {super. onDraw (canvas); if (change) {Bitmap bmp = mbitmaps. get (frontindex); Bitmap BMP 1 = mbitmaps. get (backindex); count ++; if (BMP 1.getheight ()-5 * count> = BMP 1.getheight ()/2) {// draw the upper part of the image canvas. save (); canvas. clipRect (0, 0, BMP 1.getwidth (), BMP 1.getheight ()/2); canvas. drawBitmap (BMP 1, 0, 0, paint); canvas. restore (); // draw the canvas of the part after work. save (); canvas. clipRect (0, BMP 1.getheight ()/2, BMP 1.getwidth (), BMP 1.getheight (); canvas. drawBitmap (bmp, 0, 0, paint); canvas. restore (); // dynamically change the lower half image float [] src = {0, BMP 1.getheight ()/2, // BMP 1.getwidth (), BMP 1.getheight ()/2, // BMP 1.getwidth (), BMP 1.getheight (), // 0, BMP 1.getheight ()}; henght = BMP 1.getheight ()-5 * count; float [] dst = {0, BMP 1.getheight () /2, // BMP 1.getwidth (), BMP 1.getheight ()/2, // BMP 1.getwidth (), h Enght, // 0, henght}; System. out. println (count + ""); canvas. save (); matrix. setPolyToPoly (src, 0, dst, 0, src. length> 1); canvas. clipRect (0, BMP 1.getheight ()/2, BMP 1.getwidth (), henght); paint. setXfermode (null); paint. setXfermode (xfermode); canvas. drawBitmap (BMP 1, matrix, paint); canvas. restore (); paint. setXfermode (null);} else {// draw the upper part of the image canvas. save (); canvas. clipRect (0, 0, BMP 1.getwidth (), bm P1.getHeight ()/2); canvas. drawBitmap (BMP 1, 0, 0, paint); canvas. restore (); // draw the canvas of the part after work. save (); canvas. clipRect (0, BMP 1.getheight ()/2, BMP 1.getwidth (), BMP 1.getheight (); canvas. drawBitmap (bmp, 0, 0, paint); canvas. restore (); // dynamically change the float [] src = {0, // BMP 1.getwidth (), 0, // BMP 1.getwidth (), BMP 1.getheight ()/2, // 0, BMP 1.getheight ()/2}; henght = BMP 1.getheight ()-5 * count; float [] dst = {0, henght, // BMP 1.getwidth (), henght, // BMP 1.getwidth (), BMP 1.getheight ()/2, // 0, BMP 1.getheight ()/2}; System. out. println (count + ""); canvas. save (); matrix. setPolyToPoly (src, 0, dst, 0, src. length> 1); canvas. clipRect (0, BMP 1.getheight ()/2, BMP 1.getwidth (), henght); paint. setXfermode (null); paint. setXfermode (xfermode); canvas. drawBitmap (bmp, matrix, paint); canvas. restore (); paint. setXfermo De (null); if (henght <= 0) {// specifies the animation end resetting parameter count = 0; change = false; int temp = frontindex; frontindex = backindex; backindex = temp; System. out. println ("change false"); bhjg = (int) (Math. random () * (8000-5000) + 5000; System. out. println ("bhjg:" + bhjg + "") ;}} else {if (! Setbitmap) {canvas. drawBitmap (BitmapFactory. decodeResource (getResources (), R. drawable. im2), matrix, paint );}}}}