How to drag and scale large images in android

Source: Internet
Author: User

Due to the recent busy project, the blog has never had time to update. Today, I have to update two articles consecutively.

The drag-and-drop scaling of this image is also used in my project. I will sort out the source code and give it to you. I hope it will help you solve similar problems in the future.

I will not introduce this article too much, just go to the source code:

Copy codeThe Code is 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 the 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 successful
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 occupied by the image
Bitmap = null;
System. gc (); // remind the System to recycle it 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 occupied by the image
Bitmap = null;
System. gc (); // remind the System to recycle it in time
}
}
}
}

Because it is a big image, you need to recycle it during onDestroy so that the System will recycle it regularly, System. gc () is to remind the system to recycle. Although java-based garbage collection mechanism does not need to be concerned about object recycling, it also brings a bad side to android Developers, if the system recycle is not timely, memory overflow may occur. When will the phone no longer worry about memory usage.

Run the following command to check the effect:

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.