android开发获取网络图片并可拖拽移动

这一篇讲获取网络图片,然后显示出来,并对其操作,如拖拽,放大,缩小,移动。

首先,先访问网络图片,代码如下:

先定义一个线程,因为在android4.0以上的系统中,不支持有阻塞的操作在主线程里发生,所以得新定义一个线程,这个线程包括访问网络操作。

Thread xiaoli_thread=new Thread(new Runnable(){
			@Override//该线程通过网络,获取校历的数据流
			public void run() {
				 try{
					 URL url= new URL(PATH);
					 HttpURLConnection conn= (HttpURLConnection)url.openConnection();
					 conn.setConnectTimeout(6 * 1000); // 别超过6秒
					 conn.setDoInput(true);
					 conn.connect();
					 InputStream inputStream=conn.getInputStream();
					 if(inputStream==null){
						 Log.i(TAG,"picture got failed");
					 }else{
						 bmp= BitmapFactory.decodeStream(inputStream);
						 Log.i(TAG,"picture got");
						 
						 MAX_W=bmp.getWidth()*2;
						 MAX_H=bmp.getHeight()*2;
						 MIN_W=bmp.getWidth()/2;
						 MIN_H=bmp.getHeight()/2;
						 
						 Message msg=new Message();
						 msg.what=SHOW_PICTURE;
						 handler.sendMessage(msg);
					 }
				 }catch (SocketTimeoutException e){
					 Log.i(TAG,"SocketTimeoutException");
					 e.printStackTrace();
				 }
				 catch (MalformedURLException e){
					 Log.i(TAG,"MalformedURLException");
					 e.printStackTrace();
				 }catch (IOException e) {
					 Log.i(TAG,"IOException");
					 e.printStackTrace();
				 }
			 }
		 });	
再定义一个handler获取线程发送的消息

final Handler handler=new Handler(){
    		@Override//接收线程发出的信息,缓冲框消失
			public void handleMessage(Message msg){
				switch(msg.what){
					case SHOW_PICTURE:
						 img.setImageBitmap(bmp);
						 dialog.dismiss();
					break;
				}
			}
	};

然后,在oncreate()方法中

//后台启动校历线程,加载学校校历图片
		xiaoli_thread.start();
		Log.i(TAG,"new xiaoli_thread start!");
这样网络上的图片就可以显示出来了,当然还要有配置文件,这里就不说了。

android开发获取网络图片并可拖拽移动_第1张图片

接下来,就是对图片的操作。

先定义触摸、放大、缩小事件

img.setOnTouchListener(new MulitPointTouchListener(img));
zoomcon=(ZoomControls)super.findViewById(R.id.zoomcontrol);
		this.zoomcon.setOnZoomInClickListener(new onzoominclicklistener());//放大监听
		this.zoomcon.setOnZoomOutClickListener(new onzoomoutclicklistener());//缩小监听
		zoomcon.setIsZoomInEnabled(true);
	    zoomcon.setIsZoomOutEnabled(true);

然后,再写具体的方法

1、触摸即多点触控方法

	/**
	 * @继承OnTouchListener实现多点触控
	 */
	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;  
	        switch (event.getAction() & MotionEvent.ACTION_MASK) {  
		        case MotionEvent.ACTION_DOWN:
		            Log.w("FLAG", "ACTION_DOWN"); 
		            
		            //设置底部组件的可见性
		            if(IsVisible){  
			            bottom.setVisibility(View.INVISIBLE);  
			            IsVisible = false;  
			            Log.i(TAG,"onClick1");
			        }else{  
			            bottom.setVisibility(View.VISIBLE);  
			            IsVisible = true;  
			            Log.i(TAG,"onClick2");
			        }
		  
		            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);    
	    } 
	    
	}

2、放大操作

/**
	 * @放大图片
	 */
	private class onzoominclicklistener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int bmpWidth = bmp.getWidth();
		     int bmpHeight = bmp.getHeight();
		     //设置图片放大但比例
		     double scale = 1.25;
		     //计算这次要放大的比例
		     scaleWidth = (float)(scaleWidth*scale);
		     scaleHeight = (float)(scaleHeight*scale);
		     //产生新的大小但Bitmap对象
		     Matrix matrix = new Matrix();
		     matrix.postScale(scaleWidth, scaleHeight);
		     try{
			     Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
			     if(resizeBmp.getWidth()>=(MAX_W-350 )|| resizeBmp.getHeight()>=(MAX_H-350)){
			    	 zoomcon.setIsZoomInEnabled(false);
			     }else{
			    	 img.setImageBitmap(resizeBmp);
			    	 zoomcon.setIsZoomOutEnabled(true);
			     }
		     }catch(IllegalArgumentException e){
	        	Log.e(TAG,"IllegalArgumentException");
	        	e.printStackTrace();
		     }catch(Exception e){
	        	Log.e(TAG,"Exception");
	        	e.printStackTrace();
		     }
		}
	}

3、缩小操作

	/**
	 * @缩小图片
	 */
	private class onzoomoutclicklistener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int bmpWidth=bmp.getWidth();    
	        int bmpHeight=bmp.getHeight();   
	        
	        /* 设置图片缩小的比例 */  
	        double scale=0.8;   
	        /* 计算出这次要缩小的比例 */    
	        scaleWidth=(float) (scaleWidth*scale);    
	        scaleHeight=(float) (scaleHeight*scale);    
	        /* 产生reSize后的Bitmap对象 */  
	        Matrix matrix = new Matrix();   
	        matrix.postScale(scaleWidth, scaleHeight);  
	        try{
		        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, bmpHeight,matrix,true); 
		        if(resizeBmp.getWidth()<=MIN_W || resizeBmp.getHeight()<=MIN_H){
			    	 zoomcon.setIsZoomOutEnabled(false);
			     }else{
			    	 img.setImageBitmap(resizeBmp);
			    	 zoomcon.setIsZoomInEnabled(true);
			     } 
	        }catch(IllegalArgumentException e){
	        	Log.e(TAG,"IllegalArgumentException");
	        	e.printStackTrace();
	        }catch(Exception e){
	        	Log.e(TAG,"Exception");
	        	e.printStackTrace();
	        }
	        
		}
	}

啊,代码有点长,最好在最后还要加上回收图片,因为它占的内存比较多

 @Override
    protected void onStop() {
   	   if(bmp!=null){
   			if(!bmp.isRecycled()){
   				bmp.recycle();   //回收图片所占的内存
   				bmp=null;
   		        System.gc();  //提醒系统及时回收
   			}
   	   }
   	   super.onStop();
   }
	   
   	@Override
   	protected void onDestroy() {
   		super.onDestroy();
   		if(bmp!=null){
	   		if(!bmp.isRecycled()){
	   			bmp.recycle();   //回收图片所占的内存
	   			bmp=null;
	   	        System.gc();  //提醒系统及时回收
	   		}
   		}
   	}

效果图如下:

android开发获取网络图片并可拖拽移动_第2张图片


安卓市场:http://apk.hiapk.com/html/2013/05/1468059.html?module=256&info=IWjtVg9cqVJLYg%3D%3D
N多市场:http://www.nduoa.com/apk/detail/553415

360手机助手:http://zhushou.360.cn/search/index/?kw=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B

百度应用:http://as.baidu.com/a/item?docid=3101724&pre=web_am_se

优亿市场(eoe):http://www.eoemarket.com/search/apps/?keyword=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B



你可能感兴趣的:(网络,Android开发,图片)