Android canvas morph, move, rotate

Source: Internet
Author: User

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

    • Canvas Animation principle:

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.

    • Application Scenarios:

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.

    • Hardware-accelerated compatibility bug:

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

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.