2D Graphic Transformation

In the past posts, I already write articles about 2D graphic drawing API, and also an article about the black magic of the Bitmap operations. In this post, I try to write something about graphic transformation.
As far as I know there are only four transformation APIs, including translate, scale, rotate and matrix. Also, all the platforms have same transformation APIs, if it is a GUI framework. Those four APIs just like a formula, if you understand them in Android, then you have the ability to do programming in IOS and Html5. The translate, scale and rotate are much more usually used and understandable, the difficult is the matrix, it is the general and mathematic representation of all the transformations in a more powerful way.
What’s more, in the past post, I just used the graphic drawing API directly in coding, actually, it is not enough for making a performance customized UI graphic. The more common case is alway drawing by both the drawing API and graphic transformation, combine those two skills, you can create much more excellent graphic showing.
To combine the graphic drawing and transformation, you just need to follow those steps.

  • save the drawing context;
  • use the graphic transformation;
  • drawing the shape in unit size;
  • pop the drawing context;

I provided an example which draws stars in a night sky. You can get the code from following links.
  1. https://github.com/suzp1984/ExApiDemo/blob/master/app/src/main/java/suzp1984/github/io/exapidemo/graphics/StarsView.java
  2. https://github.com/suzp1984/IOS-ApiDemo
  3. https://github.com/suzp1984/web-dev-demos/blob/master/canvas/js/stars.js
The image at the top is the Android version screen shot. And following is the IOS version:



One thing needs to be careful is that the hardware acceleration should be shut down in android if you want include graphic transformation. One clearly way to do that is add following code int the View construction method.

setLayerType(View.LAYER_TYPE_SOFTWARE, null);





Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp