466 lines
18 KiB
Java
466 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.graphics.drawable.Animatable;
|
||
import android.net.Uri;
|
||
import android.os.Bundle;
|
||
import android.os.Environment;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
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.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.controller.BaseControllerListener;
|
||
import com.facebook.drawee.drawable.ScalingUtils;
|
||
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.image.ImageInfo;
|
||
import com.facebook.imagepipeline.request.ImageRequest;
|
||
import com.facebook.imagepipeline.request.ImageRequestBuilder;
|
||
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
||
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.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.halo.assistant.HaloApp;
|
||
import com.lightgame.utils.Utils;
|
||
|
||
import java.io.File;
|
||
import java.io.FileOutputStream;
|
||
import java.io.IOException;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
import butterknife.ButterKnife;
|
||
|
||
/**
|
||
* 查看游戏截图页面
|
||
*
|
||
* @author 黄壮华
|
||
*/
|
||
//TODO 处理这里activity到统一主题
|
||
public class ViewImageActivity extends Activity implements OnPageChangeListener {
|
||
|
||
private Gh_ViewPager mViewimageVp;
|
||
private View mViewimageLine;
|
||
|
||
private RelativeLayout.LayoutParams rparams;
|
||
|
||
private ViewImageAdapter adapter;
|
||
|
||
private ImagePipeline imagePipeline;
|
||
|
||
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 Map<Integer, String> newUrls;
|
||
|
||
private String scaleType;
|
||
|
||
private int width;
|
||
|
||
private boolean isOrientation;
|
||
|
||
private Handler handler = new Handler() {
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
notifyItemChanged(msg.arg1);
|
||
}
|
||
};
|
||
|
||
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(KEY_SCALETYPE, "FIT_CENTER");
|
||
checkIntent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
||
return checkIntent;
|
||
}
|
||
|
||
private void notifyItemChanged(int position) {
|
||
Object object = mViewimageVp.findViewWithTag(position);
|
||
if (object != null) {
|
||
RelativeLayout view = (RelativeLayout) object;
|
||
final ZoomSimpleDraweeView imageView = (ZoomSimpleDraweeView) view.findViewById(R.id.viewimage_iv_show);
|
||
final ProgressBarCircularIndeterminate progressBar = (ProgressBarCircularIndeterminate) view.findViewById(R.id.viewimage_pb_loading);
|
||
progressBar.setVisibility(View.VISIBLE);
|
||
ImageUtils.Companion.getInstance().display(imageView, newUrls.get(position), urls.get(position),
|
||
new BaseControllerListener<ImageInfo>() {
|
||
@Override
|
||
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
|
||
progressBar.setVisibility(View.GONE);
|
||
// if (imageInfo == null){
|
||
// return;
|
||
// }
|
||
// ImageInfo imageInfo1 = (ImageInfo) imageInfo;
|
||
// int height = imageInfo1.getHeight();
|
||
// int width = imageInfo1.getWidth();
|
||
// float index = (float) height / (float) width;
|
||
// imageView.setImagePro(index);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
// @Override
|
||
// protected int getLayoutId() {
|
||
// return R.layout.activity_viewimage;
|
||
// }
|
||
//
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
|
||
Bundle extras = getIntent().getExtras();
|
||
urls = extras.getStringArrayList(KEY_URLS);
|
||
int current = extras.getInt(KEY_CURRENT, 0);
|
||
scaleType = extras.getString(KEY_SCALETYPE);
|
||
Bundle data = getIntent().getBundleExtra(EntranceUtils.KEY_DATA);
|
||
if (data != null) {
|
||
urls = data.getStringArrayList(KEY_URLS);
|
||
current = data.getInt(KEY_CURRENT, 0);
|
||
scaleType = data.getString(KEY_SCALETYPE);
|
||
}
|
||
|
||
if (savedInstanceState != null) {
|
||
current = savedInstanceState.getInt(EntranceUtils.KEY_CURRENTITEM, 0);
|
||
isOrientation = savedInstanceState.getBoolean("isOrientation");
|
||
}
|
||
|
||
setContentView(R.layout.activity_viewimage);
|
||
//TODO activity 没继承base之前,可是木有ButterKnife的。。。。
|
||
ButterKnife.bind(this);
|
||
|
||
mViewimageVp = (Gh_ViewPager) findViewById(R.id.viewimage_vp_show);
|
||
mViewimageLine = findViewById(R.id.viewimage_slide_line);
|
||
|
||
imagePipeline = Fresco.getImagePipeline();
|
||
|
||
DisplayMetrics outMetrics = new DisplayMetrics();
|
||
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
|
||
width = outMetrics.widthPixels / urls.size();
|
||
|
||
rparams = new RelativeLayout.LayoutParams(width, DisplayUtils.dip2px(getApplicationContext(), 1));
|
||
rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
||
rparams.bottomMargin = DisplayUtils.dip2px(getApplicationContext(), 10);
|
||
rparams.leftMargin = width * current;
|
||
mViewimageLine.setLayoutParams(rparams);
|
||
|
||
adapter = new ViewImageAdapter();
|
||
mViewimageVp.setAdapter(adapter);
|
||
mViewimageVp.setCurrentItem(current);
|
||
mViewimageVp.addOnPageChangeListener(this);
|
||
|
||
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
||
isOrientation = true; // 横屏
|
||
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
|
||
isOrientation = false;// 竖屏
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected void onSaveInstanceState(Bundle outState) {
|
||
super.onSaveInstanceState(outState);
|
||
outState.putInt(EntranceUtils.KEY_CURRENTITEM, mViewimageVp.getCurrentItem());
|
||
outState.putBoolean("isOrientation", isOrientation);
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
mViewimageVp.onDestory(); // 注销EventBus
|
||
handler.removeCallbacksAndMessages(null);
|
||
}
|
||
|
||
@Override
|
||
public void onPageScrolled(int position, float positionOffset,
|
||
int positionOffsetPixels) {
|
||
if (positionOffset != 0) {
|
||
rparams.leftMargin = (int) (width * (positionOffset + position));
|
||
mViewimageLine.setLayoutParams(rparams);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onPageSelected(int position) {
|
||
Gh_RelativeLayout ghRelativeLayout;
|
||
for (int i = 0; i < mViewimageVp.getChildCount(); i++) {
|
||
if (mViewimageVp.getChildAt(i).getTag() != null) {
|
||
ghRelativeLayout = (Gh_RelativeLayout) mViewimageVp.getChildAt(i);
|
||
if (ghRelativeLayout == null) {
|
||
return;
|
||
}
|
||
ZoomSimpleDraweeView zoomDraweeView = (ZoomSimpleDraweeView) ghRelativeLayout.findViewById(R.id.viewimage_iv_show);
|
||
zoomDraweeView.reset(); // 重置矩阵,还原图片
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onPageScrollStateChanged(int newState) {
|
||
|
||
}
|
||
|
||
private void checkUrl(final String url, final ZoomSimpleDraweeView imageView) {
|
||
newUrls = new HashMap<>();
|
||
HaloApp.MAIN_EXECUTOR.execute(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
String newUrl = "http://image.ghzs666.com/pic/hq" + url.substring(url.lastIndexOf("/"));
|
||
HttpURLConnection connection = (HttpURLConnection) new URL(newUrl).openConnection();
|
||
connection.setRequestMethod("GET");
|
||
connection.setConnectTimeout(5 * 1000);
|
||
connection.setReadTimeout(5 * 1000);
|
||
connection.connect();
|
||
int code = connection.getResponseCode();
|
||
if (code == 200 && urls != null) {
|
||
for (int i = 0, size = urls.size(); i < size; i++) {
|
||
if (urls.get(i).equals(url)) {
|
||
newUrls.put(i, newUrl);
|
||
Message msg = new Message();
|
||
msg.arg1 = i;
|
||
handler.sendMessage(msg);
|
||
break;
|
||
}
|
||
}
|
||
} else {
|
||
//没有高清图时
|
||
handler.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
loadImage(url, imageView);
|
||
}
|
||
});
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
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);
|
||
ImageUtils.Companion.display(imageView, url);
|
||
// ImageUtils.getInstance(getApplicationContext()).display(url, imageView, new BaseControllerListener(){
|
||
// @Override
|
||
// public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) {
|
||
// super.onFinalImageSet(id, imageInfo, animatable);
|
||
// if (imageInfo == null){
|
||
// return;
|
||
// }
|
||
// ImageInfo imageInfo1 = (ImageInfo) imageInfo;
|
||
// int height = imageInfo1.getHeight();
|
||
// int width = imageInfo1.getWidth();
|
||
// float index = (float) height / (float) width;
|
||
// imageView.setImagePro(index);
|
||
// }
|
||
// });
|
||
}
|
||
|
||
}
|
||
|
||
private class ViewImageAdapter extends PagerAdapter implements OnSingleTapListener {
|
||
|
||
@Override
|
||
public int getCount() {
|
||
if (urls == null) {
|
||
return 0;
|
||
}
|
||
return urls.size();
|
||
}
|
||
|
||
@Override
|
||
public Object instantiateItem(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 = (ZoomSimpleDraweeView) view.findViewById(R.id.viewimage_iv_show);
|
||
|
||
if (scaleType != null || isOrientation) {
|
||
imageView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
|
||
}
|
||
if (!url.startsWith("http://image.ghzs666.com/pic/hq/") && url.startsWith("http://image.ghzs666.com/pic/")) {
|
||
String hqUrl = "http://image.ghzs666.com/pic/hq" + url.substring(url.lastIndexOf("/"));
|
||
if (imagePipeline.isInBitmapMemoryCache(Uri.parse(hqUrl))) { // 检查高清图是否被缓存
|
||
loadImage(hqUrl, imageView);
|
||
} else {
|
||
checkUrl(url, imageView);
|
||
}
|
||
} else {
|
||
loadImage(url, imageView);
|
||
}
|
||
|
||
//单点退出
|
||
imageView.setOnSingleClickListener(new ZoomSimpleDraweeView.setOnSingleClickListener() {
|
||
@Override
|
||
public void onClick() {
|
||
finish();
|
||
}
|
||
});
|
||
|
||
//长按
|
||
imageView.setOnLongClickListener(new ZoomSimpleDraweeView.setOnLongClickListener() {
|
||
@Override
|
||
public void onLongClick() {
|
||
final Dialog dialog = new Dialog(ViewImageActivity.this);
|
||
|
||
LinearLayout container = new LinearLayout(ViewImageActivity.this);
|
||
container.setOrientation(LinearLayout.VERTICAL);
|
||
container.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("保存图片");
|
||
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));
|
||
container.addView(reportTv);
|
||
|
||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
dialog.setContentView(container);
|
||
dialog.show();
|
||
|
||
reportTv.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
findImageBM();
|
||
dialog.cancel();
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
view.setTag(position);
|
||
container.addView(view);
|
||
return view;
|
||
}
|
||
|
||
private void findImageBM() {
|
||
String key = urls.get(mViewimageVp.getCurrentItem());
|
||
String hdKey = "http://image.ghzs666.com/pic/hq" + key.substring(key.lastIndexOf("/"));
|
||
ImagePipeline imagePipeline = Fresco.getImagePipeline();
|
||
String curUrl;
|
||
|
||
if (imagePipeline.isInBitmapMemoryCache(Uri.parse(hdKey))) {
|
||
curUrl = hdKey;
|
||
} else if (imagePipeline.isInBitmapMemoryCache(Uri.parse(key))) {
|
||
curUrl = key;
|
||
} else {
|
||
return;
|
||
}
|
||
|
||
ImageRequest imageRequest = ImageRequestBuilder
|
||
.newBuilderWithSource(Uri.parse(key))
|
||
.setProgressiveRenderingEnabled(true)
|
||
.build();
|
||
|
||
DataSource<CloseableReference<CloseableImage>>
|
||
dataSource = imagePipeline.fetchDecodedImage(imageRequest, ViewImageActivity.this);
|
||
final String finalCurUrl = curUrl;
|
||
dataSource.subscribe(new BaseBitmapDataSubscriber() {
|
||
@Override
|
||
protected void onNewResultImpl(Bitmap bitmap) {
|
||
saveImage(bitmap, finalCurUrl);
|
||
}
|
||
|
||
@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());
|
||
System.out.println(e);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void destroyItem(ViewGroup container, int position, Object object) {
|
||
container.removeView((View) object);
|
||
object = null;
|
||
}
|
||
|
||
@Override
|
||
public boolean isViewFromObject(View view, Object object) {
|
||
return view == object;
|
||
}
|
||
|
||
@Override
|
||
public void onSingleTap() {
|
||
finish();
|
||
}
|
||
|
||
}
|
||
}
|