Android Graphics Image Usage Summary

Source: Internet
Author: User

A Graphic effects (a) How to implement the effects

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

The 1.setXXX method is used to directly set the matrix value, and the entire matrix will be changed every time the Setxxx () method is used. The 2.postXXX method is used to set values for the matrix in a post-multiply manner, and multiple transformations 3.preXXX methods can be used in successive times using post to set values for the matrix in a forward-multiply manner. The setup operation that uses the Prexxx method occurs first. (ii) The manifestation of special effects 1. Rotation: setRotate (float dgrees,float px,float py)
px、py为旋转的轴心
2. Scaling: Setscale (float sx.float sy)

SX and SY are used to specify the scale of the x-axis and axis y-axis
Android provides the Seetscale (), Postscale (), and Prescale () methods of the Android.graphics.Matrix class to scale the image. These three methods have the same syntax format except for the method name

3. Tilt Setskew (float kx.float ky)

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

4. Pan settranslate (float dx.float dy)

Dy and dy Specify the coordinates of x and Y for the position to be moved to
Android provides the settranslate (), Posttranslate (), and Pretranslate () methods of the Android.graphics.Matrix class to translate the image. These three methods, in addition to the method name, Other syntax formats are the same

Two Bitmapshader Rendering Images

In Android, the Bitmapshader class is primarily used to render images, and if you need to crop a picture to a shape such as an ellipse or a circle, you can use the Bitmapshader class to render the image using the Bitmapshader class.
The basic steps are as follows
Creates an object of the Bitmapshader class and sets the render object through the paint's Setshader () method. When drawing an image, use a brush that has the Setshader () method already 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.

(a) The code of the Custom view
 PackageLesson10_fragmentfordata.myapplication;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Matrix;ImportAndroid.util.AttributeSet;ImportAndroid.view.KeyEvent;ImportAndroid.view.View;/** * Image Effects Demo * / Public  class MyView extends View {    //define Bitmap objectBitmap Bitmap;//Create matrix ObjectMatrix Matrix =NewMatrix ();//Width and height    intwidth, height;//Zoom ratio    floatScale =1F//Whether the state of the image is scaled or rotatedBoolean Isscale =false;//Determine if rotationBoolean istrotate =false;angle of inclination of//x axis direction    Private floatSx//Angle of rotation    Private floatdegress;//Rewrite two construction methods     Public MyView(Context context) {Super(context);    Initview (); } Public MyView(context context, AttributeSet attrs) {Super(context, attrs);    Initview (); }/** * Initialize data * /    Private void Initview() {//Get bitmap Bitmap=getcontext (). Getresources (). getdrawable (R.MIPMAP.IMG01);Bitmap = Bitmapfactory.decoderesource (Getresources (), r.mipmap.a3);//gain width and heightwidth = Bitmap.getwidth (); Height = bitmap.getheight ();//Make the current view get focus         This. setfocusable (true); }/** * Rewrite the method of OnDraw redraw * /    @Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Matrix.reset ();//Reset        if(Isscale) {//Telescopic stateMatrix.setscale (scale, scale); }Else{//Tilt StateMatrix.setskew (SX,0); }if(istrotate) {//Rotate 30 degrees clockwiseMatrix.setrotate (degress,0.5F*width,0.5F*height); }//Create new picture based on original bitmap and matrixBitmap bit = Bitmap.createbitmap (Bitmap,0,0, width, height, matrix,true);//Draw a new bitmapCanvas.drawbitmap (bit, matrix,NULL); }/** * Keyboard-controlled logic */    @Override     Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {Switch(KeyCode) { CaseKeyevent.keycode_a://Click a to tilt leftIsscale =false; Istrotate=false; SX-=0.1; Invalidate ();// Redraw                 Break; CaseKeyevent.keycode_d://Click D to Tilt rightIsscale =false; Istrotate=false; SX + =0.1; Invalidate ();// Redraw                 Break; CaseKeyevent.keycode_w://Click the W image to become largerIsscale =true; Istrotate=false; Scale + =0.1; Invalidate ();// Redraw                 Break; Casekeyevent.keycode_s://Click the s image to get smallerIsscale =true; Istrotate=false; Scale-=0.1; Invalidate ();// Redraw                 Break; CaseKeyevent.keycode_x://Click x image to rotate clockwise 30 degreesIstrotate =true; Degress + = -; Invalidate ();// Redraw                 Break; }return Super. OnKeyDown (KeyCode, event); }}
(ii) Call class
publicclass MainActivity extends Activity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new MyView(this));    }}

The program uses the W key to control the magnified effect:

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


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

Android Graphics Image Usage Summary

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.