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'
}
클릭 하여 링크 열기 무료원본 코드 다운로드이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
viewPager는 큰 그림 처리와 함께 동적으로 그림을 불러옵니다그림을 assets 아래에 놓으면 그림의 수량을 동적으로 읽을 수 있으며 그림의 내용과 수량이 바뀔 때 프로그램은 수정할 필요가 없다. 3 그림이 너무 크면 그림을 불러올 때 메모리가 넘치는 문제를 겪었다고 믿는다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.