Android / 移动互联网 · 2014年6月28日 0

Android使用PhotoView实现图片缩放功能

使用第三方开源项目,地址:https://github.com/chrisbanes/PhotoView

引入library 项目.

简单的加载一张图片:

  1. public class MainActivity extends Activity {  
  2.   
  3.     ImageView imgView;  
  4.     PhotoViewAttacher attacher;   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         imgView=(ImageView) findViewById(R.id.img);  
  10.         imgView.setImageResource(R.drawable.wallpaper);  
  11.         attacher=new PhotoViewAttacher(imgView);  
  12.     }  
  13. }  

布局文件:

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     tools:context=“.MainActivity” >  
  6.   
  7.     <ImageView  
  8.         android:layout_width=“wrap_content”  
  9.         android:layout_height=“wrap_content”  
  10.         android:id=“@+id/img” />  
  11.   
  12. </RelativeLayout>  

Share this: