Android PhotoView 加ViewPager打造图片
2019-10-09 本文已影响0人
不用98K
效果
嗯,大概就是这样
Screenrecorder-2019-10-09-14-00-15-584.gif
这里用到的框架 Glide + PhotoView
弹出dialog使用的FragmentDialog
Glide:https://github.com/bumptech/glide
PhotoView:https://github.com/chrisbanes/PhotoView
1.配置文件:
最外层gradle配置:一定要加上这句话,如果有请忽略。
maven { url "https://jitpack.io" }
app gradle配置:
// photoView
api 'com.github.chrisbanes:PhotoView:2.0.0'
// Glide
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
清单文件配置:
...
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
...
</application>
</manifest>
注意: android:usesCleartextTraffic="true" 当在targetSdkVersion=29的时候非https的图片链接无法加载显示,因为Android P http默认都是被阻止的。
完成配置
2.关键代码:
Dialog 代码:
package com.my.dialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.viewpager.widget.ViewPager;
import com.my.R;
import com.my.adapter.MyPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class PhotoDialog extends DialogFragment {
private int currentPostion = -1;
private List<String> imageData = new ArrayList<>();
private ViewPager viewPager;
private TextView textView;
private MyPagerAdapter pagerAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
currentPostion = bundle.getInt("currentPostion");
imageData = bundle.getStringArrayList("imageData");
}
View view = inflater.inflate(R.layout.dialog_photo, null);
initView(view);
initListener();
return view;
}
private void initView(View view) {
viewPager = view.findViewById(R.id.dialog_photo_vp);
textView = view.findViewById(R.id.dialog_photo_tv);
pagerAdapter = new MyPagerAdapter(getActivity(), imageData);
viewPager.setAdapter(pagerAdapter);
textView.setText(currentPostion + 1 + "/" + imageData.size());
viewPager.setCurrentItem(currentPostion, false);
}
private void initListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
textView.setText(position + 1 + "/" + imageData.size());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
pagerAdapter.setCallBack(new MyPagerAdapter.onCallBack() {
@Override
public void onItemClick() {
close();
}
});
}
private void close() {
this.dismiss();
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.PhotoDialog);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// dialog进入和消失的动画
getDialog().getWindow().getAttributes().windowAnimations = R.style.FragmentDialogAnimation;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
window.setLayout(width, height); // 使dialog充满真个屏幕
}
}
}
}
style:
<style name="PhotoDialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:windowFrame">@null</item><!-- 边框 -->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@color/black</item><!--背景颜色-->
<item name="android:backgroundDimEnabled">false</item><!--模糊-->
</style>
<style name="FragmentDialogAnimation">
<item name="android:windowEnterAnimation">@anim/anim_photo_in</item>
<item name="android:windowExitAnimation">@anim/anim_photo_out</item>
</style>
动画资源
anim_photo_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="200"
android:fillBefore="true"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" />
<alpha
android:duration="200"
android:fillBefore="true"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
anim_photo_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="200"
android:fillBefore="true"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" />
<alpha
android:duration="200"
android:fillBefore="true"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
MyPagerAdapter:
package com.my.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import java.util.List;
public class MyPagerAdapter extends PagerAdapter {
private Context context;
private List<String> data;
private onCallBack callBack;
public MyPagerAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
}
public void setCallBack(onCallBack callBack) {
this.callBack = callBack;
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView photoView = new PhotoView(context);
Glide.with(context).load(data.get(position)).into(photoView);
container.addView(photoView);
photoView.setOnClickListener(new View.OnClickListener() { // 给每个ViewPager加载页添加点击事件,点击消失
@Override
public void onClick(View view) {
if (callBack != null) {
callBack.onItemClick();
}
}
});
return photoView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
public interface onCallBack {
void onItemClick();
}
}
使用方法:
private void initListener() {
adapter.setCallBack(new MyAdapter.OnMyAdapterCallBack() {
@Override
public void onItemClick(int position, ArrayList<String> itemImgs) {
Bundle bundle = new Bundle();
ArrayList<String> data = new ArrayList<>();
data.addAll(itemImgs);
bundle.putInt("currentPostion", position);
bundle.putStringArrayList("imageData", itemImgs);
PhotoDialog photoDialog = new PhotoDialog();
photoDialog.setArguments(bundle);
photoDialog.show(getSupportFragmentManager(), "");
}
});
}
}
至此关键代码均已给出,直接复制就可使用。如果不符合你的使用习惯可随意修改代码,没什么难得地方。
代码地址:https://github.com/No98K/VpPhotoView