Files
assistant-android/app/src/main/java/com/gh/gamecenter/ShareCardPicActivity.java
2017-05-19 10:47:43 +08:00

297 lines
11 KiB
Java

package com.gh.gamecenter;
import android.content.Intent;
import android.graphics.*;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import butterknife.BindView;
import butterknife.OnClick;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.gh.base.BaseActivity;
import com.gh.common.util.*;
import com.gh.gamecenter.retrofit.ObservableUtil;
import com.gh.gamecenter.retrofit.Response;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.tencent.tauth.Tencent;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
/**
* Created by khy on 2016/11/7.
*/
public class ShareCardPicActivity extends BaseActivity {
@BindView(R.id.sharecard_content)
TextView mShareContentTv;
@BindView(R.id.sharecard_game_name)
TextView mShareGameNameTv;
@BindView(R.id.sharecard_game_icon)
SimpleDraweeView mShareGameIconDv;
@BindView(R.id.sharecard_qrcode)
ImageView mShareQrCodeDv;
@BindView(R.id.sharecard_game_content_img)
SimpleDraweeView mShareContentImgRv;
@BindView(R.id.sharecard_screenshot)
ScrollView mShareScreenshotRl;
@BindView(R.id.reuse_actionbar)
RelativeLayout mActionbar;
@BindView(R.id.sharecard_chang_img)
RelativeLayout mShareChangImageRl;
@BindView(R.id.sharecard_share_btn)
RelativeLayout mShareShareRl;
@BindView(R.id.sharecard_chang_img_icon)
ImageView mShareChangImageIcon;
@BindView(R.id.sharecard_chang_img_tv)
TextView mShareChangImageTv;
int currentImgPosition;
private String gameName;
private String gameIconUrl;
private String shareContent;
private String picName;
private List<String> shareArrImg;
private Bitmap shareBm;
private CountDownLatch latch;
private String newsId;
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap;
// 获取scrollview实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(
Color.parseColor("#ffffff"));
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}
//接收QQ或者QQ空间分享回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == com.tencent.connect.common.Constants.REQUEST_QQ_SHARE
|| requestCode == com.tencent.connect.common.Constants.REQUEST_QZONE_SHARE) {
Tencent.onActivityResultData(requestCode, resultCode, data, MessageShareUtils.getInstance(this).QqShareListener);
}
}
@Override
protected int getLayoutId() {
return R.layout.activity_sharecard_pic;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
gameName = extras.getString("gameName");
gameIconUrl = extras.getString("gameIconUrl");
shareContent = extras.getString("shareContent");
newsId = extras.getString("newsId");
List<String> arrImg = extras.getStringArrayList("shareArrImg");
picName = "shareImgPic.jpg";
currentImgPosition = 0;
init(getString(R.string.title_share_card_pic));
shareArrImg = new Vector<>();
shareArrImg.addAll(arrImg);
mActionbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.black));
SystemBarTintManager tintManager = getTintManager();
if (tintManager != null) {
tintManager.setStatusBarTintResource(android.R.color.black);
}
latch = ObservableUtil.latch(shareArrImg.size(), new Action1<Object>() {
@Override
public void call(Object o) {
ShareCardPicActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
setContentImage(shareArrImg.get(0));
currentImgPosition++;
}
});
}
});
for (int i = 0; i < shareArrImg.size(); i++) {
checkUrl(shareArrImg.get(i), shareArrImg.size(), i);
}
mShareGameNameTv.setText(gameName);
mShareContentTv.setText(Html.fromHtml(shareContent));
// mShareGameIconDv.setImageURI(gameIconUrl);
ImageUtils.display(mShareGameIconDv, gameIconUrl);
mShareQrCodeDv.setImageResource(R.drawable.test_qrcode);
if (shareArrImg.size() > 1) {
mShareChangImageIcon.setImageResource(R.drawable.sharecard_chang_img);
mShareChangImageTv.setTextColor(Color.WHITE);
} else {
mShareChangImageIcon.setImageResource(R.drawable.sharecard_unchang_img);
mShareChangImageTv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.hint));
}
if (!TextUtils.isEmpty(newsId)) {
QRCodeUtils.setQRCode(this, newsId, mShareQrCodeDv);
}
}
// private void createQrCode() {
// Observable
// .create(new Observable.OnSubscribe<String>() {
// @Override
// public void call(Subscriber<? super String> subscriber) {
// String filePath = getExternalCacheDir().getPath() + "/ShareImg/ShareQRCode.jpg";
// boolean success = QRCodeUtils.createQRImage("http://www.ghzhushou.com/article/" + newsId + ".html?source=appshare200"
// , 200, 200, filePath, ShareCardPicActivity.this);
// if (success) {
// subscriber.onNext(filePath);
// }
// subscriber.onCompleted();
// }
// })
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(new Response<String>(){
// @Override
// public void onResponse(String response) {
// super.onResponse(response);
// mShareQrCodeDv.setImageBitmap(BitmapFactory.decodeFile(response));
// }
// });
// }
private void setContentImage(String url) {
ControllerListener<ImageInfo> listener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
if (imageInfo == null) {
return;
}
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
float index = (float) height / (float) width;
ViewGroup.LayoutParams layoutParams = mShareContentImgRv.getLayoutParams();
int widthPixels = getResources().getDisplayMetrics().widthPixels;
if (index > 1) {
layoutParams.height = widthPixels;
} else {
layoutParams.height = (int) (index * widthPixels);
}
mShareContentImgRv.setLayoutParams(layoutParams);
}
};
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(url)
.setControllerListener(listener)
.build();
mShareContentImgRv.setController(controller);
}
@OnClick(R.id.sharecard_share_btn)
public void OnShareListener() {
if (shareBm != null && shareBm.isRecycled()) {
shareBm.isRecycled();
}
shareBm = getBitmapByView(mShareScreenshotRl);
saveBitmap(shareBm);
MessageShareUtils.getInstance(ShareCardPicActivity.this).showShareWindows(getWindow().getDecorView(), shareBm, picName, 1);
}
@OnClick(R.id.sharecard_chang_img)
public void OnChangPicListener() {
if (currentImgPosition > shareArrImg.size() - 1) {
currentImgPosition = 0;
}
setContentImage(shareArrImg.get(currentImgPosition));
currentImgPosition++;
}
private void checkUrl(final String url, final int size, final int position) {
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) {
shareArrImg.remove(position);
shareArrImg.add(position, newUrl);
latch.countDown();
} else {
latch.countDown();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void saveBitmap(Bitmap bm) {
File file = new File(getExternalCacheDir().getPath() + "/ShareImg");
if (!file.isDirectory()) {
file.delete();
file.mkdirs();
}
if (!file.exists()) {
file.mkdirs();
}
MessageShareUtils.getInstance(ShareCardPicActivity.this).writeBitmap(file.getPath(), picName, bm, false);
}
}