How to realize and summarize the effect of Android image _android

Source: Internet
Author: User

Android Graphics effects

Recently, the company project, there is a demand is to do the task of the picture special effects, write their own code to achieve special effects, but not very good, the Internet search relevant information, sorting out a more comprehensive Android image effects of the information, we can see,

A Graphic effects (i) the way the effect is implemented

In Android, there are 3 ways to implement special effects, setxxx methods, Postxxx and Prexxx () methods.

The 1.setXXX method is used to set the value of the matrix directly, and the entire matrix will be changed each time the Setxxx () method is used.

The 2.postXXX method is used to set values for the matrix in a back-multiply way, and multiple transformations can be done in successive times using post

The 3.preXXX method is used to set values for the matrix using a forward multiplication, and the setting operation of the Prexxx method occurs first.

(ii) Manifestation of special effects

1. Rotate: setrotate (float dgrees,float px,float py)

px、py为旋转的轴心

2. Zoom: Setscale (float sx.float sy)

SX and SY for specifying the scaling of the x-axis and axis y axes
Android provides the Android.graphics.Matrix class of Seetscale (), Postscale (), and Prescale () methods to scale the image. These three methods have the same syntax format except for different method names

3. Tilt Setskew (float kx.float ky)

KY and KY To specify the amount of tilt for the x-axis and axis y axes
Android provides the Android.graphics.Matrix class of Setskew () (), Postskew (), and Preskew () methods to skew the image. These three methods have the same syntax format except for different method names
Setskew (float sx,float sy,float px,float py)
PX and py are tilted by their axis

4. Translational Settranslate (float dx.float dy)

Dy and dy are used to specify the coordinates of x and Y for the position to move to
Android provides the Android.graphics.Matrix class of Settranslate (), Posttranslate () and Pretranslate () methods to translate images. These three methods, except for the different method names, Other syntax formats are the same

Two Bitmapshader Rendering Image

In Android, the Bitmapshader class is primarily used to render images, and if you need to crop a picture to an ellipse or a shape that is rounded to the screen, you can use the Bitmapshader class to use the Bitmapshader class to render the image
The basic steps are as follows

Creates an object of the Bitmapshader class and sets the render object through the paint Setshader () method. When drawing an image, use the brush that has the Setshader () method set.

Three Here is an example of a graphical effect control

The program controls the magnification, rotation, and tilt of the image by pressing the key.

(i) code for a custom view

Package lesson10_fragmentfordata.myapplication;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.util.AttributeSet;
Import android.view.KeyEvent;

Import Android.view.View;
  /** * Image Effects demo/public class MyView extends View {//define Bitmap object Bitmap Bitmap;

  Create a Matrix object matrices = the new Matrix ();

  width and height int width, height;

  Zoom ratio float scale = 1f;
  Whether the state of the image is scaled or rotated Boolean Isscale = false;

  Determines whether to rotate Boolean istrotate = false;
  X-axis tilt angle private float sx;

  Angle of rotation private float degress;
    Rewrite two constructor methods public MyView (context) {super (context);
  Initview ();
    Public MyView (context, AttributeSet attrs) {Super (context, attrs);
  Initview (); /** * Initialization data/private void Initview () {//Get bitmap Bitmap=getcontext (). Getresources (). getdrawable (R.mipmap.
    IMG01); Bitmap = BitmapfactorY.decoderesource (Getresources (), r.mipmap.a3);
    Gain width and Height width = bitmap.getwidth ();
    Height = bitmap.getheight ();
  Causes the current view to get focus this.setfocusable (TRUE);
    /** * Rewrite the OnDraw method */@Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
    Matrix.reset (),//Reset if (Isscale) {//Flex state Matrix.setscale (scale, scale);
    else {//Italic state Matrix.setskew (SX, 0);
    } if (istrotate) {//clockwise rotate 30 degrees matrix.setrotate (degress,0.5f*width,0.5f*height);
    ///Create new picture based on original bitmap and matrix Bitmap bit = Bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, true);

  New bitmap Canvas.drawbitmap (bit, matrix, NULL);
      /** * Keyboard Control logic */@Override public boolean onKeyDown (int keycode, keyevent event) {switch (keycode) {
        Case keyevent.keycode_a://Click A Left Tilt Isscale = false;
        Istrotate=false;
        SX-= 0.1;
      Invalidate ()//Redraw break; Case Keyevent.keycode_d://Click D to Tilt to the right Isscale = false;
        Istrotate=false;
        SX = 0.1;
      Invalidate ()//Redraw break;
        Case keyevent.keycode_w://Click on the W image to become larger Isscale = true;
        Istrotate=false;
        Scale + 0.1;
      Invalidate ()//Redraw break;
        Case keyevent.keycode_s://Click S image to change small Isscale = true;
        Istrotate=false;
        Scale-= 0.1;
      Invalidate ()//Redraw break;
        Case keyevent.keycode_x://Click X image clockwise rotate 30 degrees istrotate = true;
        Degress + 30;

    Invalidate ()//Redraw break;
  Return Super.onkeydown (KeyCode, event); }
}

(ii) Calling class

public class Mainactivity extends activity {


  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (New MyView (this));
  }

The program through the W key to control the effect of amplification:

The program uses the D key to control the skew effect:


The program uses the X key to control the effect of the rotation:

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.