454 lines
18 KiB
Java
454 lines
18 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Activity;
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.res.Configuration;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Color;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.os.Environment;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.support.v4.view.PagerAdapter;
|
|
import android.support.v4.view.ViewPager.OnPageChangeListener;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.Window;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.facebook.binaryresource.BinaryResource;
|
|
import com.facebook.cache.common.CacheKey;
|
|
import com.facebook.common.executors.CallerThreadExecutor;
|
|
import com.facebook.common.references.CloseableReference;
|
|
import com.facebook.datasource.DataSource;
|
|
import com.facebook.drawee.backends.pipeline.Fresco;
|
|
import com.facebook.drawee.interfaces.DraweeController;
|
|
import com.facebook.imagepipeline.core.ImagePipeline;
|
|
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
|
|
import com.facebook.imagepipeline.image.CloseableImage;
|
|
import com.facebook.imagepipeline.request.ImageRequest;
|
|
import com.facebook.imagepipeline.request.ImageRequestBuilder;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
import com.gh.common.util.MessageShareUtils;
|
|
import com.gh.common.util.NetworkUtils;
|
|
import com.gh.common.view.EmptyDrawable;
|
|
import com.gh.common.view.Gh_RelativeLayout;
|
|
import com.gh.common.view.Gh_RelativeLayout.OnSingleTapListener;
|
|
import com.gh.common.view.Gh_ViewPager;
|
|
import com.gh.common.view.ZoomSimpleDraweeView;
|
|
import com.gh.gamecenter.entity.ImageInfoEntity;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
/**
|
|
* 查看游戏截图页面
|
|
*
|
|
* @author 黄壮华
|
|
*/
|
|
public class ViewImageActivity extends BaseActivity implements OnPageChangeListener {
|
|
|
|
public static final int REQUEST_FOR_VIEWED_IMAGE = 921;
|
|
public static final String VIEWED_IMAGE = "viewed_image";
|
|
|
|
@BindView(R.id.image_detail_page)
|
|
Gh_ViewPager mViewPager;
|
|
@BindView(R.id.image_detail_progress)
|
|
TextView mProgressHint;
|
|
@BindView(R.id.image_mask)
|
|
View mIndicatorMask;
|
|
@BindView(R.id.image_indicator_tv)
|
|
TextView mIndicatorTv;
|
|
|
|
private ViewImageAdapter adapter;
|
|
|
|
private ImagePipeline mImagePipeline;
|
|
|
|
private static final String KEY_URLS = "urls";
|
|
private static final String KEY_CURRENT = "current";
|
|
private static final String KEY_SCALETYPE = "ScaleType";
|
|
|
|
private ArrayList<String> urls;
|
|
private HashSet<Integer> mViewedSet; // 让调用者知道该图片是否被看过了
|
|
private Map<String, ImageInfoEntity> mImageInfoMap;
|
|
|
|
private int mLimitWidth;
|
|
|
|
private boolean isOrientation;
|
|
|
|
public static Intent getViewImageIntent(Context context, ArrayList<String> list, int position, String entrance) {
|
|
Intent checkIntent = new Intent(context, ViewImageActivity.class);
|
|
checkIntent.putExtra(KEY_URLS, list);
|
|
checkIntent.putExtra(KEY_CURRENT, position);
|
|
checkIntent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
|
return checkIntent;
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_viewimage;
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
mViewedSet = new HashSet<>();
|
|
mImageInfoMap = new HashMap<>();
|
|
|
|
// init data
|
|
int current = 0;
|
|
Bundle extras = getIntent().getExtras();
|
|
if (extras != null) {
|
|
urls = extras.getStringArrayList(KEY_URLS);
|
|
current = extras.getInt(KEY_CURRENT, 0);
|
|
}
|
|
|
|
if (savedInstanceState != null) {
|
|
current = savedInstanceState.getInt(EntranceUtils.KEY_CURRENTITEM, 0);
|
|
isOrientation = savedInstanceState.getBoolean("isOrientation");
|
|
}
|
|
|
|
if (urls.size() > 1) mIndicatorMask.setVisibility(View.VISIBLE);
|
|
mIndicatorTv.setText(String.format("%d/%d", current + 1, urls.size()));
|
|
|
|
mImagePipeline = Fresco.getImagePipeline();
|
|
|
|
// init slide
|
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
|
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
|
|
int widthPixels = outMetrics.widthPixels;
|
|
if (NetworkUtils.isWifiOr4GConnected(this)) {
|
|
mLimitWidth = widthPixels * 2;
|
|
} else {
|
|
mLimitWidth = widthPixels;
|
|
}
|
|
// init viewPage
|
|
adapter = new ViewImageAdapter();
|
|
mViewPager.setAdapter(adapter);
|
|
mViewPager.setCurrentItem(current);
|
|
mViewPager.addOnPageChangeListener(this);
|
|
|
|
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
isOrientation = true; // 横屏
|
|
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
|
|
isOrientation = false;// 竖屏
|
|
}
|
|
|
|
mProgressHint.setOnClickListener(v -> {
|
|
int position = mViewPager.getCurrentItem();
|
|
Object object = mViewPager.findViewWithTag(position);
|
|
if (object != null) {
|
|
mProgressHint.setWidth(mProgressHint.getWidth());
|
|
RelativeLayout view = (RelativeLayout) object;
|
|
final ZoomSimpleDraweeView draweeView = view.findViewById(R.id.viewimage_iv_show);
|
|
draweeView.getHierarchy().setProgressBarImage(new EmptyDrawable(progress -> {
|
|
if (position == mViewPager.getCurrentItem()) { // 防止下载过程中切换图片
|
|
int percent = progress / 100;
|
|
if (percent < 100) {
|
|
mProgressHint.setText((percent + "%"));
|
|
} else {
|
|
mProgressHint.setText("已完成");
|
|
mBaseHandler.postDelayed(() -> {
|
|
if (position == mViewPager.getCurrentItem()) { // 防止等待过程中切换图片
|
|
mProgressHint.setVisibility(View.GONE);
|
|
}
|
|
}, 500);
|
|
}
|
|
}
|
|
}));
|
|
draweeView.setController(Fresco.newDraweeControllerBuilder()
|
|
// 高清图片
|
|
.setImageRequest(ImageRequest.fromUri(urls.get(position)))
|
|
// 低分辨率图片
|
|
.setLowResImageRequest(ImageRequest.fromUri(ImageUtils.addLimitWidth(urls.get(position), mLimitWidth)))
|
|
.build());
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
outState.putInt(EntranceUtils.KEY_CURRENTITEM, mViewPager.getCurrentItem());
|
|
outState.putBoolean("isOrientation", isOrientation);
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
mViewPager.onDestroy(); // 注销EventBus
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrolled(int position, float positionOffset,
|
|
int positionOffsetPixels) {
|
|
if (positionOffset != 0) {
|
|
mProgressHint.setVisibility(View.GONE);
|
|
} else {
|
|
String url = urls.get(position);
|
|
ImageInfoEntity imageInfoEntity = mImageInfoMap.get(url);
|
|
if (imageInfoEntity != null && imageInfoEntity.getFileSize() != null &&
|
|
!mImagePipeline.isInBitmapMemoryCache(ImageRequest.fromUri(url)) &&
|
|
!mImagePipeline.isInDiskCacheSync(ImageRequest.fromUri(url))) {
|
|
String size = String.format(Locale.CHINA, "%.1fM",
|
|
Integer.valueOf(imageInfoEntity.getFileSize().getValue()) / 1024F / 1024F);
|
|
mProgressHint.setVisibility(View.VISIBLE);
|
|
mProgressHint.setText(("查看原图(" + size + ")"));
|
|
ViewGroup.LayoutParams layoutParams = mProgressHint.getLayoutParams();
|
|
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
|
|
mProgressHint.setLayoutParams(layoutParams);
|
|
}
|
|
}
|
|
|
|
mViewedSet.add(position);
|
|
setResult(Activity.RESULT_OK, new Intent().putExtra(VIEWED_IMAGE, mViewedSet));
|
|
}
|
|
|
|
@Override
|
|
public void onPageSelected(int position) {
|
|
Gh_RelativeLayout ghRelativeLayout;
|
|
for (int i = 0; i < mViewPager.getChildCount(); i++) {
|
|
if (mViewPager.getChildAt(i).getTag() != null) {
|
|
ghRelativeLayout = (Gh_RelativeLayout) mViewPager.getChildAt(i);
|
|
if (ghRelativeLayout == null) {
|
|
return;
|
|
}
|
|
|
|
ZoomSimpleDraweeView zoomDraweeView = ghRelativeLayout.findViewById(R.id.viewimage_iv_show);
|
|
zoomDraweeView.reset(); // 重置矩阵,还原图片
|
|
}
|
|
}
|
|
mIndicatorTv.setText(String.format("%d/%d", position + 1, urls.size()));
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrollStateChanged(int newState) {
|
|
|
|
}
|
|
|
|
private void loadImageInfo(int position, int width) {
|
|
String url = urls.get(position);
|
|
RetrofitManager.getInstance(this)
|
|
.getApi().getImageInfo(url + "?x-oss-process=image/info")
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<ImageInfoEntity>() {
|
|
@Override
|
|
public void onResponse(@Nullable ImageInfoEntity response) {
|
|
if (response != null && response.getImageWidth() != null &&
|
|
Integer.valueOf(response.getImageWidth().getValue()) > width) {
|
|
mImageInfoMap.put(url, response);
|
|
if (position == mViewPager.getCurrentItem()) {
|
|
onPageScrolled(position, 0, 0); // 刷新下载原图提示按钮
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void loadImage(String url, final ZoomSimpleDraweeView imageView) {
|
|
if (url.contains(".gif")) {
|
|
DraweeController controller = Fresco.newDraweeControllerBuilder()
|
|
.setUri(url)
|
|
.setAutoPlayAnimations(true)
|
|
.build();
|
|
imageView.setController(controller);
|
|
} else {
|
|
imageView.setImageURI(url); // 加载原图
|
|
}
|
|
}
|
|
|
|
private class ViewImageAdapter extends PagerAdapter implements OnSingleTapListener {
|
|
|
|
@Override
|
|
public int getCount() {
|
|
if (urls == null) {
|
|
return 0;
|
|
}
|
|
return urls.size();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
|
String url = urls.get(position);
|
|
|
|
Gh_RelativeLayout view = (Gh_RelativeLayout) View.inflate(container.getContext(), R.layout.viewimage_normal_item, null);
|
|
ZoomSimpleDraweeView imageView = view.findViewById(R.id.viewimage_iv_show);
|
|
|
|
if (mImagePipeline.isInBitmapMemoryCache(ImageRequest.fromUri(url)) ||
|
|
mImagePipeline.isInDiskCacheSync(ImageRequest.fromUri(url)) ||
|
|
NetworkUtils.isWifiOr4GConnected(ViewImageActivity.this) ||
|
|
url.contains(".gif")) {
|
|
loadImage(url, imageView);
|
|
} else {
|
|
ImageUtils.addLimitWidthAndLoad(imageView, url, mLimitWidth, imageInfo -> {
|
|
if (imageInfo != null && mImageInfoMap.get(url) == null) {
|
|
loadImageInfo(position, imageInfo.getWidth()); // 加载图片参数,目的是用户显示原文按钮
|
|
}
|
|
});
|
|
}
|
|
|
|
//单点退出
|
|
imageView.setOnSingleClickListener(ViewImageActivity.this::finish);
|
|
|
|
//长按
|
|
imageView.setOnLongClickListener(() -> {
|
|
final Dialog dialog = new Dialog(ViewImageActivity.this);
|
|
|
|
LinearLayout container1 = new LinearLayout(ViewImageActivity.this);
|
|
container1.setOrientation(LinearLayout.VERTICAL);
|
|
container1.setBackgroundColor(Color.WHITE);
|
|
|
|
final TextView reportTv = new TextView(ViewImageActivity.this);
|
|
reportTv.setPadding(DisplayUtils.dip2px(ViewImageActivity.this, 20), DisplayUtils.dip2px(ViewImageActivity.this, 12),
|
|
0, DisplayUtils.dip2px(ViewImageActivity.this, 12));
|
|
reportTv.setText(R.string.save_pic);
|
|
reportTv.setTextSize(17);
|
|
reportTv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.title));
|
|
reportTv.setBackgroundResource(R.drawable.textview_white_style);
|
|
int widthPixels = getResources().getDisplayMetrics().widthPixels;
|
|
reportTv.setLayoutParams(new LinearLayout.LayoutParams((widthPixels * 9) / 10,
|
|
LinearLayout.LayoutParams.WRAP_CONTENT));
|
|
container1.addView(reportTv);
|
|
|
|
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
dialog.setContentView(container1);
|
|
dialog.show();
|
|
|
|
reportTv.setOnClickListener(v -> {
|
|
findImageBM();
|
|
dialog.cancel();
|
|
});
|
|
});
|
|
|
|
view.setTag(position);
|
|
container.addView(view);
|
|
return view;
|
|
}
|
|
|
|
private void findImageBM() {
|
|
String url = urls.get(mViewPager.getCurrentItem());
|
|
String limitUrl = ImageUtils.addLimitWidth(urls.get(mViewPager.getCurrentItem()), mLimitWidth);
|
|
String targetUrl;
|
|
|
|
if (mImagePipeline.isInBitmapMemoryCache(ImageRequest.fromUri(url))) {
|
|
targetUrl = url;
|
|
} else if (mImagePipeline.isInBitmapMemoryCache(ImageRequest.fromUri(limitUrl))) {
|
|
targetUrl = limitUrl;
|
|
} else {
|
|
targetUrl = url;
|
|
}
|
|
|
|
|
|
ImageRequest imageRequest = ImageRequestBuilder
|
|
.newBuilderWithSource(Uri.parse(targetUrl))
|
|
.setProgressiveRenderingEnabled(true)
|
|
.build();
|
|
|
|
DataSource<CloseableReference<CloseableImage>>
|
|
dataSource = mImagePipeline.fetchDecodedImage(imageRequest, ViewImageActivity.this);
|
|
final String finalCurUrl = targetUrl;
|
|
dataSource.subscribe(new BaseBitmapDataSubscriber() {
|
|
@Override
|
|
protected void onNewResultImpl(Bitmap bitmap) {
|
|
if (bitmap != null) { // gif bitmap is null
|
|
saveImage(bitmap, finalCurUrl);
|
|
} else {
|
|
ImageRequest imageRequest = ImageRequest.fromUri(Uri.parse(finalCurUrl));
|
|
CacheKey encodedCacheKey = mImagePipeline.getCacheKeyFactory().getEncodedCacheKey(imageRequest, null);
|
|
BinaryResource resource = Fresco.getImagePipelineFactory().getMainFileCache().getResource(encodedCacheKey);
|
|
try {
|
|
String fileName = finalCurUrl.substring(finalCurUrl.lastIndexOf("/"));
|
|
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
|
|
|
File targetFile = new File(savePath, fileName);
|
|
OutputStream outStream = new FileOutputStream(targetFile);
|
|
outStream.write(resource.read());
|
|
Utils.toast(ViewImageActivity.this, "图片已保存到/Pictures/ghzhushou/");
|
|
} catch (Exception e) {
|
|
Utils.log("图片保存失败:" + e.toString());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
|
|
|
|
}
|
|
}, CallerThreadExecutor.getInstance());
|
|
}
|
|
|
|
private void saveImage(Bitmap bitmap, String curUrl) {
|
|
|
|
String fileName = curUrl.substring(curUrl.lastIndexOf("/"));
|
|
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
|
|
|
try {
|
|
File file = new File(savePath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
|
|
File f = new File(savePath, fileName);
|
|
if (f.exists()) {
|
|
f.delete();
|
|
}
|
|
|
|
FileOutputStream out = new FileOutputStream(f);
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
|
|
out.flush();
|
|
out.close();
|
|
Utils.log("图片保存成功");
|
|
Utils.toast(ViewImageActivity.this, "图片已保存到/Pictures/ghzhushou/");
|
|
MessageShareUtils.refreshImage(new File(savePath), ViewImageActivity.this);
|
|
} catch (IOException e) {
|
|
Utils.log("图片保存失败:" + e.toString());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
|
|
container.removeView((View) object);
|
|
object = null;
|
|
}
|
|
|
|
@Override
|
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
|
|
return view == object;
|
|
}
|
|
|
|
@Override
|
|
public void onSingleTap() {
|
|
finish();
|
|
}
|
|
|
|
}
|
|
}
|