public class testview extends view { private bitmap mbitmap = null; private bitmap nbitmap = null; private float scalex = 1.0f; private float scaleY = 1.0f; private float step = 0.0001f; public testview (context context, attributeset Attrs) { super (context, attrs); mbitmap = bitmapfactory.decoderesource (Context.getResources (), R.drawable.ic_pet_spirit); nbitmap = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.ic_pet_spirit_lighting); } @Override protected void ondraw (Canvas canvas) { super.ondraw (Canvas); scalecanvas (canvas); invalidate (); } private void scalecanvas (Canvas canvas) { scaleX += step; Scaley += step; matrix matrix = new matrix (); paint paint = new paint (); canvas.scale (Scalex, scaley); canvas.drawbitmap (Mbitmap, matrix, paint); canvas.drawbitmap (NBItmap, matrix, paint); }}
First on the code, the Code entry level! Main Share (1) Canvas animation principle (2) Application scenario (3) Hardware acceleration compatible bug
The initial size of the canvas must be changed by scale,rotate,translate the canvas size, angle, displacement. draw the desired content on the changed canvas and the content changes accordingly . At first, I thought it was to draw one side on canvas, and in the beginning, the misunderstanding of canvas was confusing a lot of people. In fact, the draw method of canvas is to draw the content on the transformed canvas. The principle is this: each call to OnDraw will be a new canvas, the canvas is a full screen area , if you just get this canvas after the painting, the content is immediately displayed, but then after the deformation, Canvas does not draw on its own, and if you draw once after morphing, the image repeats.
Why use canvas to deform, if we use a bunch of pictures to achieve the corresponding animation, then there will be a lot of memory for the problem to consider. The deformation of the canvas is used native method, so natural fast, convenient. And the call OnDraw animation effect drawing is now applied to a variety of view plug-ins above is more. Various click animation effects.
Clippath ()
Clipregion ()
Drawpicture ()
Drawtextonpath ()
DrawVertices ()
These methods must be determined before calling to make sure that you have turned off hardware acceleration in the invoked activity, modifying the manifest
Android:hardwareaccelerated= "false". Otherwise you will find that the RGB in the screenshot is colored, but the screen is pale (ghost-drawn)
This article is from the Geek Development blog, so be sure to keep this source http://yy030913.blog.51cto.com/3599514/1694320
Android canvas morph, move, rotate