314 lines
12 KiB
Java
314 lines
12 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.content.Intent;
|
|
import android.content.res.Configuration;
|
|
import android.graphics.drawable.Animatable;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
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.widget.RelativeLayout;
|
|
|
|
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.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
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 java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
* 查看游戏截图页面
|
|
*
|
|
* @author 黄壮华
|
|
*/
|
|
public class ViewImageActivity extends BaseActivity implements
|
|
OnPageChangeListener {
|
|
|
|
private Gh_ViewPager viewimage_vp_show;
|
|
private ViewImageAdapter adapter;
|
|
private View viewimage_slide_line;
|
|
private RelativeLayout.LayoutParams rparams;
|
|
|
|
private ArrayList<String> urls;
|
|
private Map<Integer, String> newUrls;
|
|
|
|
private String scaleType;
|
|
|
|
private int width;
|
|
|
|
private boolean isOrientation;
|
|
|
|
private ImagePipeline imagePipeline;
|
|
|
|
private Handler handler = new Handler() {
|
|
@Override
|
|
public void handleMessage(Message msg) {
|
|
notifyItemChanged(msg.arg1);
|
|
}
|
|
};
|
|
|
|
private void notifyItemChanged(int position) {
|
|
Object object = viewimage_vp_show.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.getInstance(getApplicationContext()).display(newUrls.get(position), urls.get(position), imageView
|
|
, new BaseControllerListener(){
|
|
@Override
|
|
public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) {
|
|
super.onFinalImageSet(id, imageInfo, 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 void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = getIntent();
|
|
urls = intent.getStringArrayListExtra("urls");
|
|
int current = intent.getIntExtra("current", 0);
|
|
scaleType = intent.getStringExtra("ScaleType");
|
|
|
|
if (savedInstanceState != null) {
|
|
current = savedInstanceState.getInt("currentItem", 0);
|
|
isOrientation = savedInstanceState.getBoolean("isOrientation");
|
|
}
|
|
|
|
setContentView(R.layout.activity_viewimage);
|
|
|
|
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;
|
|
viewimage_slide_line = findViewById(R.id.viewimage_slide_line);
|
|
viewimage_slide_line.setLayoutParams(rparams);
|
|
|
|
viewimage_vp_show = (Gh_ViewPager) findViewById(R.id.viewimage_vp_show);
|
|
adapter = new ViewImageAdapter();
|
|
viewimage_vp_show.setAdapter(adapter);
|
|
viewimage_vp_show.setCurrentItem(current);
|
|
viewimage_vp_show.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("currentItem", viewimage_vp_show.getCurrentItem());
|
|
outState.putBoolean("isOrientation", isOrientation);
|
|
}
|
|
|
|
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.ghzhushou.com/pic/hq/") && url.startsWith("http://image.ghzhushou.com/pic/")) {
|
|
String hqUrl = "http://image.ghzhushou.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();
|
|
}
|
|
});
|
|
|
|
view.setTag(position);
|
|
container.addView(view);
|
|
return view;
|
|
}
|
|
|
|
@Override
|
|
public boolean isViewFromObject(View view, Object object) {
|
|
return view == object;
|
|
}
|
|
|
|
@Override
|
|
public void destroyItem(ViewGroup container, int position, Object object) {
|
|
container.removeView((View) object);
|
|
object = null;
|
|
}
|
|
|
|
@Override
|
|
public void onSingleTap() {
|
|
finish();
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrollStateChanged(int newState) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrolled(int position, float positionOffset,
|
|
int positionOffsetPixels) {
|
|
if (positionOffset != 0) {
|
|
rparams.leftMargin = (int) (width * (positionOffset + position));
|
|
viewimage_slide_line.setLayoutParams(rparams);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPageSelected(int position) {
|
|
Gh_RelativeLayout ghRelativeLayout;
|
|
for (int i = 0; i < viewimage_vp_show.getChildCount(); i++){
|
|
if (viewimage_vp_show.getChildAt(i).getTag() != null){
|
|
ghRelativeLayout = (Gh_RelativeLayout) viewimage_vp_show.getChildAt(i);
|
|
if (ghRelativeLayout == null){
|
|
return;
|
|
}
|
|
ZoomSimpleDraweeView zoomDraweeView = (ZoomSimpleDraweeView) ghRelativeLayout.findViewById(R.id.viewimage_iv_show);
|
|
zoomDraweeView.reset(); // 重置矩阵,还原图片
|
|
}
|
|
}
|
|
}
|
|
|
|
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.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 void checkUrl(final String url, final ZoomSimpleDraweeView imageView) {
|
|
newUrls = new HashMap<>();
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
String newUrl = "http://image.ghzhushou.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();
|
|
}
|
|
}
|
|
}.start();
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
viewimage_vp_show.onDestory(); // 注销EventBus
|
|
}
|
|
}
|