viewpager+photoview 이미지 뷰 어 구현

본 논문 의 사례 는 안 드 로 이 드 가 이미지 뷰 어 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

효 과 는 두 손가락 으로 크기 조정 을 금지 해 야 하기 때문에 커서 가 없어 서 핸드폰 으로 컴퓨터 에 동적 사진 을 찍 을 수 밖 에 없습니다.
demo 에서 제3자 포 토 뷰 를 실 용적 으로 사용 하여 매우 간단 하고 실 용적 입 니 다.그림 의 더 블 클릭 확대,제스처 확대 축 소 를 실현 할 수 있 습 니 다.손가락 이 화면 을 떠 날 때 그림 이 원래 그림 보다 작 으 면 자동 으로 원래 그림 의 크기 를 회복 할 수 있 습 니 다.감청 을 클릭 하고 그림 을 길 게 누 르 면 감청 을 할 수 있 습 니 다.
전체 demo 는 매우 간단 합 니 다.전체적으로 activity 입 니 다.페이지 레이아웃 은 view pager 와 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 을 초기 화하 고 어댑터 Fragment PagerAdapter 에 집합 을 전달 하면 됩 니 다.
어댑터 마다 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' 
} 
클릭 하여 링크 열기 무료원본 코드 다운로드
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기