本文實例為大家分享了Android實現圖片查看器的具體代碼,供大家參考,具體內容如下
	
效果需要兩個手指禁止縮放,所以沒有光標,只能用手機投放電腦上錄制動態圖片;
demo中實用了一個第三方的photoview,非常簡單實用;可實現圖片雙擊放大,手勢放大縮小,當手指離開屏幕時如果圖片小于原圖可自動恢復原圖大小,可實現點擊監聽,長按圖片監聽;
整個demo非常簡單,整體就是一個activity,頁面布局只有一個viewpager和textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="#ffffff" android:textSize="30sp" /> </RelativeLayout>
	在activity中初始化圖片的url,將集合傳遞到適配器FragmentPagerAdapter中即可中即可;
	每個適配器中顯示一個fragment,這里自己創建一個即可
/**  * Created by zheng on 2017/11/27.  */  public class PhotoFragment extends Fragment {   private String url;  private PhotoView mPhotoView;   /**  * 獲取這個fragment需要展示圖片的url  * @param url  * @return  */  public static PhotoFragment newInstance(String url) {  PhotoFragment fragment = new PhotoFragment();  Bundle args = new Bundle();  args.putString("url", url);  fragment.setArguments(args);  return fragment;  }   @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  url = getArguments().getString("url");  }   @Nullable  @Override  public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {  View view = inflater.inflate(R.layout.fragment_img, container, false);  mPhotoView = view.findViewById(R.id.photoview);  //設置縮放類型,默認ScaleType.CENTER(可以不設置)  mPhotoView.setScaleType(ImageView.ScaleType.CENTER);  mPhotoView.setOnLongClickListener(new View.OnLongClickListener() {   @Override   public boolean onLongClick(View view) {   ToastUtils.showToast(getContext(),"長按事件");   return true;   }  });  mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {   @Override   public void onPhotoTap(View view, float x, float y) {   ToastUtils.showToast(getContext(),"點擊事件,真實項目中可關閉activity");   }  });  Glide.with(getContext())   .load(url)   .placeholder(R.mipmap.ic_launcher)//加載過程中圖片未顯示時顯示的本地圖片   .error(R.mipmap.ic_launcher)//加載異常時顯示的圖片 //  .centerCrop()//圖片圖填充ImageView設置的大小   .fitCenter()//縮放圖像測量出來等于或小于ImageView的邊界范圍,該圖像將會完全顯示   .into(mPhotoView);  return view;  }  } fragment布局非常簡單,只有一個圖片展示的view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <uk.co.senab.photoview.PhotoView android:id="@+id/photoview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
想要實用PhotoView和Glide需要build.gradle中添加
allprojects {  repositories {  maven { url "https://jitpack.io" }  } } dependencies {  compile 'com.github.chrisbanes.photoview:library:+'  compile 'com.github.bumptech.glide:glide:3.7.0' } 點擊打開鏈接免費下載源碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
| 
 
 | 
新聞熱點
疑難解答