你的位置:首页 > 操作系统

[操作系统]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 // hovertree.com 69  70   public class MulitPointTouchListener implements OnTouchListener {    71        72     Matrix matrix = new Matrix();    73     Matrix savedMatrix = new Matrix();    74      75     public ImageView image;    76     static final int NONE = 0;    77     static final int DRAG = 1;    78     static final int ZOOM = 2;    79     int mode = NONE;    80      81     PointF start = new PointF();    82     PointF mid = new PointF();    83     float oldDist = 1f;    84      85      86     public MulitPointTouchListener(ImageView image) {    87       super();    88       this.image = image;    89     }    90      91     @Override    92     public boolean onTouch(View v, MotionEvent event) {    93       this.image.setScaleType(ScaleType.MATRIX);    94      95       ImageView view = (ImageView) v;    96 //    dumpEvent(event);    97      98       switch (event.getAction() & MotionEvent.ACTION_MASK) {   99         100       case MotionEvent.ACTION_DOWN:   101     102         Log.w("FLAG", "ACTION_DOWN");  103         matrix.set(view.getImageMatrix());   104         savedMatrix.set(matrix);   105         start.set(event.getX(), event.getY());   106         mode = DRAG;   107         break;   108       case MotionEvent.ACTION_POINTER_DOWN:   109         Log.w("FLAG", "ACTION_POINTER_DOWN");  110         oldDist = spacing(event);   111         if (oldDist > 10f) {   112           savedMatrix.set(matrix);   113           midPoint(mid, event);   114           mode = ZOOM;   115         }   116         break;   117       case MotionEvent.ACTION_UP:   118         Log.w("FLAG", "ACTION_UP");  119       case MotionEvent.ACTION_POINTER_UP:   120         Log.w("FLAG", "ACTION_POINTER_UP");  121         mode = NONE;   122         break;   123       case MotionEvent.ACTION_MOVE:   124         Log.w("FLAG", "ACTION_MOVE");  125         if (mode == DRAG) {   126           matrix.set(savedMatrix);   127           matrix.postTranslate(event.getX() - start.x, event.getY()   128               - start.y);   129         } else if (mode == ZOOM) {   130           float newDist = spacing(event);   131           if (newDist > 10f) {   132             matrix.set(savedMatrix);   133             float scale = newDist / oldDist;   134             matrix.postScale(scale, scale, mid.x, mid.y);   135           }   136         }   137         break;   138       }   139     140       view.setImageMatrix(matrix);   141       return true;  142     }   143     144       145     private float spacing(MotionEvent event) {   146       float x = event.getX(0) - event.getX(1);   147       float y = event.getY(0) - event.getY(1);   148       return FloatMath.sqrt(x * x + y * y);   149     }   150     151     private void midPoint(PointF point, MotionEvent event) {   152       float x = event.getX(0) + event.getX(1);   153       float y = event.getY(0) + event.getY(1);   154       point.set(x / 2, y / 2);   155     }   156   }   157   private void initHead() { 158     linnerLayout_spacepage = (LinearLayout) findViewById(R.id.linnerLayout_spacepage); 159     relativeLayout_spacepage=(RelativeLayout) findViewById(R.id.relativeLayout_spacepage); 160     btn_spacepage_back = (Button) findViewById(R.id.btn_spacepage_back); 161     btn_spacepage_back.setOnClickListener(new OnClickListener() { 162        163       @Override 164       public void onClick(View v) { 165         // TODO Auto-generated method stub 166          167         finish(); 168       } 169     }); 170     btn_spacepage_save = (Button) findViewById(R.id.btn_spacepage_save); 171      172   } 173   protected boolean isNetworkAvailable() { 174     ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 175     if (connectivity == null) { 176       Log.i("NetWorkState", "Unavailabel"); 177       return false; 178     } else { 179       NetworkInfo[] info = connectivity.getAllNetworkInfo(); 180       if (info != null) { 181         for (int i = 0; i < info.length; i++) { 182           if (info[i].getState() == NetworkInfo.State.CONNECTED) { 183             Log.i("NetWorkState", "Availabel"); 184             return true; 185           } 186         } 187       } 188     } 189     return false; 190   } 191   @Override 192 protected void onStop() { 193   // TODO Auto-generated method stub 194    if(bitmap!=null){ 195       if(!bitmap.isRecycled()){ 196         bitmap.recycle();  //回收图片所占的内存 197         bitmap=null; 198         System.gc(); //提醒系统及时回收 199      } 200     } 201   super.onStop(); 202 } 203   @Override 204   protected void onDestroy() { 205     // TODO Auto-generated method stub 206     super.onDestroy(); 207     if(bitmap!=null){ 208     if(!bitmap.isRecycled()){ 209       bitmap.recycle();  //回收图片所占的内存 210       bitmap=null; 211       System.gc(); //提醒系统及时回收 212    } 213     } 214   } 215 } 

因为是大图片,在onDestroy的时候要recycle掉,这样系统才会定时回收掉,System.gc();是提醒系统回收,虽然java这种垃圾回收机制不用我们自己关心对象的回收,但是这也给android开发人员带来了不好的一面,如果系统回收不及时很可能就造成了内存溢出了。什么时候手机不担心内存使用了就好了。

 

   运行一下看看效果: