歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 大圖片拖拽縮放

這篇Android圖片拖拽縮放也是我在項目中用到的,今天整理一下,將源碼奉獻給大家,希望對大家以後碰到相似的問題有幫助。

這篇就不做過多介紹了,直接上源碼:

  1. public class SpacePageActivity extends Activity {  
  2.   
  3.     private LinearLayout linnerLayout_spacepage;  
  4.     private RelativeLayout relativeLayout_spacepage;  
  5.     private Button btn_spacepage_back;  
  6.     private Button btn_spacepage_save;  
  7.     private static Boolean isTrue=false;  
  8.       
  9.     private static  String IMAGE_MIME_TYPE ;  
  10.   
  11.     private ImageView image_spacePage;  
  12.     private ProgressDialog dialog;  
  13.     private Bitmap bitmap;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.         requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題    
  19.         setContentView(R.layout.spacepage);  
  20.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);     
  21.         initHead();  
  22.       
  23.         if(isNetworkAvailable()){  
  24.         image_spacePage=(ImageView) findViewById(R.id.image_spacepage);  
  25.         btn_spacepage_save=(Button) findViewById(R.id.btn_spacepage_save);  
  26.         image_spacePage.setOnTouchListener(new MulitPointTouchListener(image_spacePage));  
  27.         image_spacePage.setScaleType(ScaleType.CENTER_INSIDE);  
  28.           
  29.         //加載成功  
  30.         String urls="http://epaper.yzdsb.com.cn/201201/04/yz2104_7.jpg";  
  31.         bitmap=returnBitMap(urls);  
  32.         if(null!=bitmap){  
  33.          image_spacePage.setImageBitmap(bitmap);  
  34.         }  
  35.           
  36.         }  
  37.     }   
  38.       
  39.   
  40.     public Bitmap returnBitMap(String url) {    
  41.           
  42.          if(null==url || "".equals(url)){  
  43.              return null;  
  44.          }  
  45.            URL myFileUrl = null;     
  46.            Bitmap bitmap = null;     
  47.            try {     
  48.             myFileUrl = new URL(url);     
  49.            } catch (MalformedURLException e) {     
  50.             e.printStackTrace();     
  51.            }     
  52.            try {     
  53.             HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();  
  54.             conn.setConnectTimeout(2000);  
  55.             conn.setDoInput(true);     
  56.             conn.connect();     
  57.             InputStream is = conn.getInputStream();     
  58.             bitmap = BitmapFactory.decodeStream(is);     
  59.             is.close();     
  60.            } catch (IOException e) {     
  61.             e.printStackTrace();     
  62.            }     
  63.              
  64.              
  65.              
  66.            return bitmap;     
  67.         }   
  68.     public class MulitPointTouchListener implements OnTouchListener {      
  69.             
  70.         Matrix matrix = new Matrix();      
  71.         Matrix savedMatrix = new Matrix();      
  72.         
  73.         public ImageView image;      
  74.         static final int NONE = 0;      
  75.         static final int DRAG = 1;      
  76.         static final int ZOOM = 2;      
  77.         int mode = NONE;      
  78.         
  79.         PointF start = new PointF();      
  80.         PointF mid = new PointF();      
  81.         float oldDist = 1f;      
  82.         
  83.         
  84.         public MulitPointTouchListener(ImageView image) {      
  85.             super();      
  86.             this.image = image;      
  87.         }      
  88.         
  89.         @Override      
  90.         public boolean onTouch(View v, MotionEvent event) {      
  91.             this.image.setScaleType(ScaleType.MATRIX);      
  92.         
  93.             ImageView view = (ImageView) v;      
  94. //        dumpEvent(event);      
  95.         
  96.             switch (event.getAction() & MotionEvent.ACTION_MASK) {    
  97.                 
  98.             case MotionEvent.ACTION_DOWN:      
  99.         
  100.                 Log.w("FLAG", "ACTION_DOWN");    
  101.                 matrix.set(view.getImageMatrix());      
  102.                 savedMatrix.set(matrix);      
  103.                 start.set(event.getX(), event.getY());      
  104.                 mode = DRAG;      
  105.                 break;      
  106.             case MotionEvent.ACTION_POINTER_DOWN:      
  107.                 Log.w("FLAG", "ACTION_POINTER_DOWN");    
  108.                 oldDist = spacing(event);      
  109.                 if (oldDist > 10f) {      
  110.                     savedMatrix.set(matrix);      
  111.                     midPoint(mid, event);      
  112.                     mode = ZOOM;      
  113.                 }      
  114.                 break;      
  115.             case MotionEvent.ACTION_UP:      
  116.                 Log.w("FLAG", "ACTION_UP");    
  117.             case MotionEvent.ACTION_POINTER_UP:      
  118.                 Log.w("FLAG", "ACTION_POINTER_UP");    
  119.                 mode = NONE;      
  120.                 break;      
  121.             case MotionEvent.ACTION_MOVE:      
  122.                 Log.w("FLAG", "ACTION_MOVE");    
  123.                 if (mode == DRAG) {      
  124.                     matrix.set(savedMatrix);      
  125.                     matrix.postTranslate(event.getX() - start.x, event.getY()      
  126.                             - start.y);      
  127.                 } else if (mode == ZOOM) {      
  128.                     float newDist = spacing(event);      
  129.                     if (newDist > 10f) {      
  130.                         matrix.set(savedMatrix);      
  131.                         float scale = newDist / oldDist;      
  132.                         matrix.postScale(scale, scale, mid.x, mid.y);      
  133.                     }      
  134.                 }      
  135.                 break;      
  136.             }      
  137.         
  138.             view.setImageMatrix(matrix);      
  139.             return true;    
  140.         }      
  141.         
  142.             
  143.         private float spacing(MotionEvent event) {      
  144.             float x = event.getX(0) - event.getX(1);      
  145.             float y = event.getY(0) - event.getY(1);      
  146.             return FloatMath.sqrt(x * x + y * y);      
  147.         }      
  148.         
  149.         private void midPoint(PointF point, MotionEvent event) {      
  150.             float x = event.getX(0) + event.getX(1);      
  151.             float y = event.getY(0) + event.getY(1);      
  152.             point.set(x / 2, y / 2);      
  153.         }      
  154.     }     
  155.     private void initHead() {  
  156.         linnerLayout_spacepage = (LinearLayout) findViewById(R.id.linnerLayout_spacepage);  
  157.         relativeLayout_spacepage=(RelativeLayout) findViewById(R.id.relativeLayout_spacepage);  
  158.         btn_spacepage_back = (Button) findViewById(R.id.btn_spacepage_back);  
  159.         btn_spacepage_back.setOnClickListener(new OnClickListener() {  
  160.               
  161.             @Override  
  162.             public void onClick(View v) {  
  163.                 // TODO Auto-generated method stub  
  164.                   
  165.                 finish();  
  166.             }  
  167.         });  
  168.         btn_spacepage_save = (Button) findViewById(R.id.btn_spacepage_save);  
  169.           
  170.     }  
  171.     protected boolean isNetworkAvailable() {  
  172.         ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  
  173.         if (connectivity == null) {  
  174.             Log.i("NetWorkState", "Unavailabel");  
  175.             return false;  
  176.         } else {  
  177.             NetworkInfo[] info = connectivity.getAllNetworkInfo();  
  178.             if (info != null) {  
  179.                 for (int i = 0; i < info.length; i++) {  
  180.                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
  181.                         Log.i("NetWorkState", "Availabel");  
  182.                         return true;  
  183.                     }  
  184.                 }  
  185.             }  
  186.         }  
  187.         return false;  
  188.     }  
  189.    @Override  
  190. protected void onStop() {  
  191.     // TODO Auto-generated method stub  
  192.        if(bitmap!=null){  
  193.             if(!bitmap.isRecycled()){  
  194.                 bitmap.recycle();   //回收圖片所占的內存  
  195.                 bitmap=null;  
  196.                  System.gc();  //提醒系統及時回收  
  197.          }  
  198.         }  
  199.     super.onStop();  
  200. }  
  201.     @Override  
  202.     protected void onDestroy() {  
  203.         // TODO Auto-generated method stub  
  204.         super.onDestroy();  
  205.         if(bitmap!=null){  
  206.         if(!bitmap.isRecycled()){  
  207.             bitmap.recycle();   //回收圖片所占的內存  
  208.             bitmap=null;  
  209.              System.gc();  //提醒系統及時回收  
  210.      }  
  211.         }  
  212.     }  
  213. }  

因為是大圖片,在onDestroy的時候要recycle掉,這樣系統才會定時回收掉,System.gc();是提醒系統回收,雖然java這種垃圾回收機制不用我們自己關心對象的回收,但是這也給android開發人員帶來了不好的一面,如果系統回收不及時很可能就造成了內存溢出了。什麼時候手機不擔心內存使用了就好了。

運行一下看看效果:

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved