Android big picture drag and zoom implementation principle _android

Source: Internet
Author: User

Due to the recent project busy, blog has no time to update, today a little time on the continuous update two bar, too enjoyable.

This picture drag and drop scaling is also I used in the project, today to tidy up, will be the source of dedication to everyone, I hope that we encounter similar problems in the future to help.

This article will not do too much introduction, directly on the source code:

Copy Code code as follows:

public class Spacepageactivity extends activity {

Private LinearLayout linnerlayout_spacepage;
Private Relativelayout relativelayout_spacepage;
Private Button Btn_spacepage_back;
Private Button Btn_spacepage_save;
private static Boolean Istrue=false;

private static String Image_mime_type;

Private ImageView Image_spacepage;
Private ProgressDialog Dialog;
Private Bitmap Bitmap;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Requestwindowfeature (window.feature_no_title)//Hide Title
Setcontentview (R.layout.spacepage);
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
Inithead ();

if (isnetworkavailable ()) {
Image_spacepage= (ImageView) Findviewbyid (r.id.image_spacepage);
Btn_spacepage_save= (Button) Findviewbyid (R.id.btn_spacepage_save);
Image_spacepage.setontouchlistener (New Mulitpointtouchlistener (image_spacepage));
Image_spacepage.setscaletype (Scaletype.center_inside);

Load succeeded
String urls= "Http://epaper.yzdsb.com.cn/201201/04/yz2104_7.jpg";
Bitmap=returnbitmap (URLs);
if (Null!=bitmap) {
Image_spacepage.setimagebitmap (bitmap);
}

}
}


Public Bitmap returnbitmap (String URL) {

if (Null==url | | "". Equals (URL)) {
return null;
}
URL myfileurl = null;
Bitmap Bitmap = null;
try {
myfileurl = new URL (URL);
catch (Malformedurlexception e) {
E.printstacktrace ();
}
try {
HttpURLConnection conn = (httpurlconnection) myfileurl.openconnection ();
Conn.setconnecttimeout (2000);
Conn.setdoinput (TRUE);
Conn.connect ();
InputStream is = Conn.getinputstream ();
Bitmap = Bitmapfactory.decodestream (IS);
Is.close ();
catch (IOException e) {
E.printstacktrace ();
}



return bitmap;
}
public class Mulitpointtouchlistener implements Ontouchlistener {

Matrix matrix = new Matrix ();
Matrix Savedmatrix = new Matrix ();

public ImageView image;
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF ();
PointF mid = new PointF ();
float olddist = 1f;


Public Mulitpointtouchlistener (ImageView image) {
Super ();
This.image = image;
}

@Override
public boolean Ontouch (View V, motionevent event) {
This.image.setScaleType (Scaletype.matrix);

ImageView view = (ImageView) v;
Dumpevent (event);

Switch (event.getaction () & Motionevent.action_mask) {

Case Motionevent.action_down:

LOG.W ("FLAG", "Action_down");
Matrix.set (View.getimagematrix ());
Savedmatrix.set (matrix);
Start.set (Event.getx (), event.gety ());
mode = DRAG;
Break
Case Motionevent.action_pointer_down:
LOG.W ("FLAG", "Action_pointer_down");
olddist = Spacing (event);
if (Olddist > 10f) {
Savedmatrix.set (matrix);
Midpoint (Mid, event);
mode = ZOOM;
}
Break
Case MOTIONEVENT.ACTION_UP:
LOG.W ("FLAG", "action_up");
Case MOTIONEVENT.ACTION_POINTER_UP:
LOG.W ("FLAG", "action_pointer_up");
mode = NONE;
Break
Case Motionevent.action_move:
LOG.W ("FLAG", "Action_move");
if (mode = = DRAG) {
Matrix.set (Savedmatrix);
Matrix.posttranslate (Event.getx ()-Start.x, Event.gety ()
-START.Y);
else if (mode = = ZOOM) {
float newdist = spacing (event);
if (Newdist > 10f) {
Matrix.set (Savedmatrix);
float scale = newdist/olddist;
Matrix.postscale (scale, scale, mid.x, MID.Y);
}
}
Break
}

View.setimagematrix (matrix);
return 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);
}

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);
}
}
private void Inithead () {
Linnerlayout_spacepage = (linearlayout) Findviewbyid (r.id.linnerlayout_spacepage);
Relativelayout_spacepage= (relativelayout) Findviewbyid (r.id.relativelayout_spacepage);
Btn_spacepage_back = (Button) Findviewbyid (r.id.btn_spacepage_back);
Btn_spacepage_back.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub

Finish ();
}
});
Btn_spacepage_save = (Button) Findviewbyid (R.id.btn_spacepage_save);

}
Protected Boolean isnetworkavailable () {
Connectivitymanager connectivity = (Connectivitymanager) getsystemservice (Context.connectivity_service);
if (connectivity = = NULL) {
LOG.I ("Networkstate", "Unavailabel");
return false;
} else {
Networkinfo[] info = Connectivity.getallnetworkinfo ();
if (info!= null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {
LOG.I ("Networkstate", "Availabel");
return true;
}
}
}
}
return false;
}
@Override
protected void OnStop () {
TODO auto-generated Method Stub
if (bitmap!=null) {
if (!bitmap.isrecycled ()) {
Bitmap.recycle (); Reclaim the memory of a picture
Bitmap=null;
System.GC (); Remind the system to recycle in time
}
}
Super.onstop ();
}
@Override
protected void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
if (bitmap!=null) {
if (!bitmap.isrecycled ()) {
Bitmap.recycle (); Reclaim the memory of a picture
Bitmap=null;
System.GC (); Remind the system to recycle in time
}
}
}
}

Because it is a big picture, in the OnDestroy time to recycle off, so that the system will be recycled, System.GC (), is the reminder system recycling, although the Java garbage collection mechanism We do not care about the collection of objects, But it also brings a bad side to the Android developers, and if the system is not recycled, it could cause a memory overflow. It would be nice when the phone doesn't worry about memory usage.

Run a look at the effect:


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.