package com.smartdot.cgt.activity; import java.util.ArrayList; import java.util.List; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PointF; import android.os.Bundle; import android.os.Message; import android.view.MotionEvent; import android.widget.ImageView; import com.smartdot.cgt.R; import com.smartdot.cgt.model.CaseAttach; import com.smartdot.cgt.model.ResponseResult; import com.smartdot.cgt.request.Request; import com.smartdot.cgt.util.BaseThread; import com.smartdot.cgt.util.HandlerStatus; import com.smartdot.cgt.util.Msg; import com.smartdot.cgt.view.FlingView; import com.smartdot.cgt.view.TitleBar; public class FrmViewImage extends BaseActivity { public static final String CASEIDKEY = "CASEID"; public static final String BITMAPKEY = "BITMAPKEY"; public static final String FIRSTSHOWINDEX = "FIRSTSHOWINDEX"; private FlingView flingView; private TitleBar titlebar; private ImageView imageForIndex; private BaseThread threadGetAttach; private List<Bitmap> imageList; private int nowViewingIndex = -1; private PointF centerPoint; private float firstdistance; private Matrix matrix; public int getNowViewingIndex() { return nowViewingIndex; } public void setNowViewingIndex(int nowViewingIndex) { if (getImageList() != null && getImageList().size() > 0) { if (nowViewingIndex >= 0 && nowViewingIndex < getImageList().size()) { this.nowViewingIndex = nowViewingIndex; titlebar.setTitleText("图片" + (this.nowViewingIndex + 1)); } final int oneStep = 16; final int size = getImageList().size(); Bitmap bitmap = Bitmap.createBitmap(oneStep * size, oneStep, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); Paint paint = new Paint(); paint.setARGB(255, 122, 122, 122); for (int i = 0; i < size; i++) { if (i != nowViewingIndex) { c.drawCircle((float) (oneStep * (i + .5)), oneStep / 2, 4, paint); } else { Paint paintOn = new Paint(); paintOn.setARGB(255, 255, 255, 255); c.drawCircle((float) (oneStep * (i + .5)), oneStep / 2, 4, paintOn); } } imageForIndex.setImageBitmap(bitmap); } else { this.nowViewingIndex = nowViewingIndex; } flingView.setPosition(nowViewingIndex); } public List<Bitmap> getImageList() { return imageList; } public void setImageList(List<Bitmap> imageList) { this.imageList = imageList; flingView.setBitmaps(imageList.toArray(new Bitmap[] {})); } @Override protected void addEventListener() { flingView.setOnIndexChange(flingView.new OnIndexChange() { @Override public void onIndexChanged(int nowIndex) { setNowViewingIndex(nowIndex); } }); } @SuppressWarnings("unchecked") @Override protected void handleMessage(Message msg) { if (msg.what == HandlerStatus.REQUEST_CASEATTACH) { if (msg.arg1 == HandlerStatus.HANDLE_OK) { List<CaseAttach> list = (List<CaseAttach>) msg.obj; List<Bitmap> bitmapList = new ArrayList<Bitmap>(); for (CaseAttach caseAttach : list) { byte[] bytes = caseAttach.getAttachContent(); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); bitmapList.add(bitmap); } if (bitmapList.size() > 0) { setImageList(bitmapList); setNowViewingIndex(getNowViewingIndex()); } } else if (msg.arg1 == HandlerStatus.HANDLE_FAIL) { Msg.showInfo(FrmViewImage.this, "获取图片失败!"); } else if (msg.arg1 == HandlerStatus.HANDLE_ERROR) { Msg.showInfo(FrmViewImage.this, "获取图片出错!"); } closeProgress(); } } @Override protected void onCancelProgress(DialogInterface arg0) { if (threadGetAttach != null) { threadGetAttach.interrupt(); this.finish(); } } @Override protected void setLayout() { initActivity(); setContentView(R.layout.viewimage); titlebar = (TitleBar) findViewById(R.id.titlebar); titlebar.setHelpBtnVisible(false); flingView = (FlingView) findViewById(R.id.viewFlipperContainer); imageForIndex = (ImageView) findViewById(R.id.imageForIndex); } @Override protected void doInit() { setDataFromParent(); // imageForIndex.setImageResource(R.drawable.testimage); } @Override protected void doDestroy() { if (getImageList() != null && getImageList().size() > 0) { for (Bitmap bitmap : getImageList()) { bitmap.recycle(); System.gc(); } } } @SuppressWarnings("unchecked") private void setDataFromParent() { Bundle bundle = this.getIntent().getExtras(); if (bundle.containsKey(BITMAPKEY)) { List<String> pathList = (ArrayList<String>) bundle .getSerializable(BITMAPKEY); List<Bitmap> bitmapList = new ArrayList<Bitmap>(); for (String filePath : pathList) { bitmapList.add(BitmapFactory.decodeFile(filePath)); } setImageList(bitmapList); } else if (bundle.containsKey(CASEIDKEY)) { String caseId = bundle.getString(CASEIDKEY); List<String> cacheImagePathList = new ArrayList<String>(); try { cacheImagePathList = Request.getRequest().getCacheImage(caseId); } catch (Exception e) { e.printStackTrace(); } if (cacheImagePathList.size() > 0) { List<Bitmap> bitmapList = new ArrayList<Bitmap>(); for (String cacheImagePath : cacheImagePathList) { bitmapList.add(BitmapFactory.decodeFile(cacheImagePath)); } setImageList(bitmapList); } else { getCaseAttach(caseId); } } if (bundle.containsKey(FIRSTSHOWINDEX)) { setNowViewingIndex(bundle.getInt(FIRSTSHOWINDEX)); } else { setNowViewingIndex(0); } } private void getCaseAttach(final String caseId) { threadGetAttach = new BaseThread(baseHandler) { @Override public void runThread() { Message msg = threadGetAttach.obtainMessage(); if (msg != null) { msg.what = HandlerStatus.REQUEST_CASEATTACH; try { ResponseResult<List<CaseAttach>> result = Request .getRequest().getCaseAttach(caseId); if (result.isSuccess()) { msg.arg1 = HandlerStatus.HANDLE_OK; msg.obj = result.getResultObj(); } else if (result.isHadError()) { msg.arg1 = HandlerStatus.HANDLE_ERROR; } else { msg.arg1 = HandlerStatus.HANDLE_FAIL; } } catch (Exception e) { msg.arg1 = HandlerStatus.HANDLE_ERROR; } threadGetAttach.sendMessage(msg); } } }; showProgress("获取图片中。。。", "图片查看"); threadGetAttach.start(); } }