Android image scaling and rotation of the implementation of the detailed _android

Source: Internet
Author: User
This paper uses the matrix to realize the image scaling and rotation of Android. The sample code is as follows:
Copy Code code as follows:

Package Com.android.matrix;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Matrix;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.ImageView.ScaleType;
/**
* Android enables image scaling and rotation.
* @author Administrator
*
*/
public class Matixactivity extends activity {
public void OnCreate (Bundle icicle) {

Super.oncreate (Icicle);

Settitle ("Android" enables picture scaling and rotation.) ");
LinearLayout linlayout = new LinearLayout (this);

Load the picture that needs to be manipulated, here is a picture
Bitmap bitmaporg = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.R);

Gets the width and height of this picture
int width = bitmaporg.getwidth ();
int height = bitmaporg.getheight ();

Defines the width and height of a pre-converted picture
int newwidth = 200;
int newheight = 200;

Calculate the zoom rate, the new size in addition to the original size
float ScaleWidth = ((float) newwidth)/width;
float ScaleHeight = ((float) newheight)/height;

Create a Matrix object for manipulating pictures
Matrix matrix = new Matrix ();

Zoom picture Action
Matrix.postscale (ScaleWidth, ScaleHeight);

Rotate picture Action
Matrix.postrotate (45);

Create a new picture
Bitmap Resizedbitmap = Bitmap.createbitmap (bitmaporg, 0, 0,
width, height, matrix, true);

Convert the bitmap created above into a drawable object so that it can be used in ImageView, ImageButton
Bitmapdrawable BMD = new bitmapdrawable (RESIZEDBITMAP);

Create a ImageView
ImageView ImageView = new ImageView (this);

Set ImageView picture for the above converted Picture
Imageview.setimagedrawable (BMD);

Center a picture display
Imageview.setscaletype (Scaletype.center);

Add ImageView to a layout template
Linlayout.addview (ImageView,
New Linearlayout.layoutparams (
Layoutparams.fill_parent, Layoutparams.fill_parent
)
);

Set as template for this activity
Setcontentview (linlayout);
}
}

The above example is the static implementation of picture scaling, in the following example, the mouse pulley and the direction of the key to achieve the picture dynamic amplification and reduction.
The program structure is shown below:



Zoom.java code in file:
Copy Code code as follows:

Package com.android.zooming;
Import Android.view.View;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import android.graphics.drawable.Drawable;
Import android.view.KeyEvent;
public class Zoom extends View {
Private drawable image;
private int zoomcontroler=20;

Public Zoom {
Super (context);
Image=context.getresources (). getdrawable (r.drawable.x);
Setfocusable (TRUE);
}

@Override
protected void OnDraw (Canvas Canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Control the width and height of the image
Image.setbounds (GetWidth ()/2)-zoomcontroler, (GetHeight ()/2)-zoomcontroler, (GetWidth ()/2) +zoomcontroler, ( GetHeight ()/2) +zoomcontroler);
Image.draw (canvas);
}

@Override
public boolean onKeyDown (int keycode, keyevent event) {

if (KEYCODE==KEYEVENT.KEYCODE_DPAD_UP)//Zoom In
zoomcontroler+=10;

if (keycode==keyevent.keycode_dpad_down)//Shrink
zoomcontroler-=10;

if (zoomcontroler<10)
zoomcontroler=10;

Invalidate ();
return true;
}
}

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.