Android image operation (scaling and moving) instance code

Source: Internet
Author: User
Tags gety

View_show.xml

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<FrameLayout
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent">
<ImageView
Android: id = "@ + id/view_img"
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"
Android: background = "@ drawable/camera_gray"/>
<ImageButton
Android: id = "@ + id/view_close"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_gravity = "top | left"
Android: layout_margin = "5dip"/>
<ImageButton
Android: id = "@ + id/view_del"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_gravity = "top | right"
Android: layout_margin = "5dip"/>
<ImageButton
Android: id = "@ + id/view_narrow"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_gravity = "bottom | center_horizontal"
Android: layout_marginRight = "10dip"/>
<ImageButton
Android: id = "@ + id/view_amplification"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_gravity = "bottom | center_horizontal"
Android: layout_marginLeft = "10dip"/>
</FrameLayout>
</LinearLayout>

GalleryViewTouch. java:

Copy codeThe Code is as follows: package com. kotei. lbs. Anthurium. LawCases;

Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;
Import android. graphics. PointF;
Import android. graphics. RectF;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. util. FloatMath;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. Window;
Import android. view. View. OnTouchListener;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;

/**
* Image viewing operations
* @ Author hongj
*/
Public class GalleryViewTouch extends Activity {
Private ImageView iv;
Private Bitmap bitmap = null;
Matrix matrix = new Matrix ();
Matrix savedMatrix = new Matrix ();
DisplayMetrics dm;
Float minScaleR; // minimum scaling ratio
Static final float MAX_SCALE = 4f; // maximum scaling ratio
Static final int NONE = 0; // initial state
Static final int DRAG = 1; // DRAG
Static final int ZOOM = 2; // ZOOM
Int mode = NONE;
PointF prev = new PointF ();
PointF mid = new PointF ();
Float dist = 1f;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. view_show );
Dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm); // get the resolution
Bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. bottom_layout_background );
Iv = (ImageView) findViewById (R. id. view_img );
Iv. setImageBitmap (bitmap );
Iv. setOnTouchListener (new ImageTouch (bitmap, dm. widthPixels, dm. heightPixels, iv ));

}

Class ImageTouch implements OnTouchListener {
Private ImageView iv;
Private Bitmap bitmap = null;
Matrix matrix = new Matrix ();
Matrix savedMatrix = new Matrix ();
Int dmwidth, dmheight;
Float minScaleR; // minimum scaling ratio
Static final float MAX_SCALE = 4f; // maximum scaling ratio
Static final int NONE = 0; // initial state
Static final int DRAG = 1; // DRAG
Static final int ZOOM = 2; // ZOOM
Int mode = NONE;
PointF prev = new PointF ();
PointF mid = new PointF ();
Float dist = 1f;
ImageTouch (Bitmap bitmap, int width, int height, ImageView iv ){
This. bitmap = bitmap;
This. dmwidth = width;
This. dmheight = height;
This. iv = iv;
InitImage ();
}
Public void initImage (){
If (dmwidth <bitmap. getWidth () | dmheight <bitmap. getHeight ()){

MinZoom ();
Center ();
Iv. setImageMatrix (matrix );
} Else {
Iv. setScaleType (ScaleType. CENTER );
}
}
@ Override

Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction () & MotionEvent. ACTION_MASK ){
// Press the master node
Case MotionEvent. ACTION_DOWN:
Log. d ("System. out", "ACTION_DOWN ");
SavedMatrix. set (matrix );
Prev. set (event. getX (), event. getY ());
Mode = DRAG;
Break; // press the secondary Node
Case MotionEvent. ACTION_POINTER_DOWN:
Log. d ("System. out", "ACTION_POINTER_DOWN ");
Dist = spacing (event); // if the distance between two consecutive points is greater than 10, the multi-point mode is determined.
If (spacing (event)> 10f ){
SavedMatrix. set (matrix );
MidPoint (mid, event );
Mode = ZOOM;
}
Break;
Case MotionEvent. ACTION_UP:
Case MotionEvent. ACTION_POINTER_UP:
Mode = NONE;
Break;
Case MotionEvent. ACTION_MOVE:
If (mode = DRAG ){
Matrix. set (savedMatrix );
Matrix. postTranslate (event. getX ()-prev. x, event. getY ()
-Prev. y );

} Else if (mode = ZOOM ){
Float newDist = spacing (event );
If (newDist> 10f ){
Matrix. set (savedMatrix );
Float tScale = newDist/dist;
Matrix. postScale (tScale, tScale, mid. x, mid. y );
}
}
Break;
}

Iv. setImageMatrix (matrix );
If (mode! = NONE)
CheckView ();

Return true;
}
Private void CheckView (){
Float p [] = new float [9];
Matrix. getValues (p );
If (mode = ZOOM ){
If (p [0] <minScaleR ){
Matrix. setScale (minScaleR, minScaleR );
}
If (p [0]> MAX_SCALE ){
Matrix. set (savedMatrix );
}
}
Center ();
}

Public void minZoom (){
Log. I ("test", bitmap. getWidth () + "");
Log. I ("test", bitmap. getHeight () + "");
MinScaleR = Math. min (
(Float) dmwidth/(float) bitmap. getWidth (),
(Float) dmheight/(float) bitmap. getHeight ());
If (minScaleR <1.0 ){
Matrix. postScale (minScaleR, minScaleR );
}
}
Protected void center (boolean horizontal, boolean vertical ){
Matrix m = new Matrix ();
M. set (matrix );
RectF rect = new RectF (0, 0, bitmap. getWidth (), bitmap. getHeight ());
M. mapRect (rect );
Float height = rect. height ();
Float width = rect. width ();
Log. d ("System. out", rect. top + "*************" + rect. bottom );
Float deltaX = 0, deltaY = 0;
If (vertical) {// if the image size is smaller than the screen size, it is displayed in the center. Greater than the screen, move up if left empty above, move down if left empty below

Int screenHeight = dmheight;
If (height <screenHeight ){
DeltaY = (screenHeight-height)/2-rect. top;
} Else if (rect. top> 0 ){
DeltaY =-rect. top;
} Else if (rect. bottom <screenHeight ){
DeltaY = iv. getHeight ()-rect. bottom;
}
// DeltaY-= 50;
}
If (horizontal ){
Int screenWidth = dmwidth;
If (width <screenWidth ){
DeltaX = (screenWidth-width)/2-rect. left;
} Else if (rect. left> 0 ){
DeltaX =-rect. left;
} Else if (rect. right <screenWidth ){
DeltaX = screenWidth-rect. right;
}
}
Matrix. postTranslate (deltaX, deltaY );
}
Public void center (){
Center (true, true );
}
Private float spacing (MotionEvent event ){
Float x = event. getX (0)-event. getX (1 );
Float y = event. getY (0)-event. getY (1 );
Return FloatMath. sqrt (x * x + y * y );
}

/*** Midpoint of two points */
Private void midPoint (PointF point, MotionEvent event ){
Float x = event. getX (0) + event. getX (1 );
Float y = event. getY (0) + event. getY (1 );
Point. set (x/2, y/2 );
}
}
}

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.