package com.gh.gamecenter; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.drawable.Animatable; import android.os.Bundle; import android.text.Html; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; 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.MessageShareUtils; import com.gh.common.util.QRCodeUtils; import com.gh.gamecenter.manager.SystemBarTintManager; import com.gh.gamecenter.retrofit.ObservableUtil; import com.tencent.tauth.Tencent; 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; import butterknife.BindView; import butterknife.OnClick; import rx.functions.Action1; /** * 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; private String gameName; private String gameIconUrl; private String shareContent; private String picName; int currentImgPosition; private List shareArrImg; private Bitmap shareBm; private CountDownLatch latch; private String newsId; //接收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 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 arrImg = extras.getStringArrayList("shareArrImg"); picName = "shareImgPic.jpg"; currentImgPosition = 0; View contentView = View.inflate(this, R.layout.activity_sharecard_pic, null); init(contentView, "分享卡片"); shareArrImg = new Vector<>();; shareArrImg.addAll(arrImg); mActionbar.setBackgroundColor(getResources().getColor(R.color.back)); SystemBarTintManager tintManager = getTintManager(); if (tintManager != null) { tintManager.setStatusBarTintResource(R.color.back); } latch = ObservableUtil.latch(shareArrImg.size(), new Action1() { @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); 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(getResources().getColor(R.color.hint)); } if (!TextUtils.isEmpty(newsId)) { createQrCode(); } } private void createQrCode() { new Thread(new Runnable() { @Override public void run() { final 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) { runOnUiThread(new Runnable() { @Override public void run() { mShareQrCodeDv.setImageBitmap(BitmapFactory.decodeFile(filePath)); } }); } } }).start(); } private void setContentImage(String url){ ControllerListener listener = 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; 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); } @Override public void onIntermediateImageSet(String id, Object imageInfo) { super.onIntermediateImageSet(id, imageInfo); } @Override public void onIntermediateImageFailed(String id, Throwable throwable) { super.onIntermediateImageFailed(id, throwable); } @Override public void onFailure(String id, Throwable throwable) { super.onFailure(id, throwable); } @Override public void onRelease(String id) { super.onRelease(id); } }; 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(new View(ShareCardPicActivity.this), 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 static Bitmap getBitmapByView(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; // 获取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; } 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); } }