package org.carelife.creader.ui.view; import java.io.BufferedInputStream; import java.io.IOException; import org.carelife.creader.ui.activity.ImageDetail; import org.carelife.creader.bean.FlowTagBean; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.Toast; public class FlowView extends ImageView implements View.OnClickListener, View.OnLongClickListener { private FlowTagBean flowTag; private Context context; public Bitmap bitmap; private int columnIndex; private int rowIndex; private Handler viewHandler; public FlowView(Context c, AttributeSet attrs, int defStyle) { super(c, attrs, defStyle); this.context = c; Init(); } public FlowView(Context c, AttributeSet attrs) { super(c, attrs); this.context = c; Init(); } public FlowView(Context c) { super(c); this.context = c; Init(); } private void Init() { setOnClickListener(this); this.setOnLongClickListener(this); setAdjustViewBounds(true); } public boolean onLongClick(View v) { Toast.makeText(context, ""+ this.flowTag.getFlowId(), Toast.LENGTH_SHORT).show(); return true; } public void onClick(View v) { Toast.makeText(context, "" + this.flowTag.getFlowId(), Toast.LENGTH_SHORT).show(); Intent intent = new Intent(context, ImageDetail.class); intent.putExtra("pic_url", flowTag.getFileName()); context.startActivity(intent); } public void LoadImage() { if (getFlowTag() != null) { new LoadImageThread().start(); } } public void Reload() { if (this.bitmap == null && getFlowTag() != null) { new ReloadImageThread().start(); } } public void recycle() { setImageBitmap(null); if ((this.bitmap == null) || (this.bitmap.isRecycled())) return; this.bitmap.recycle(); this.bitmap = null; } public FlowTagBean getFlowTag() { return flowTag; } public void setFlowTag(FlowTagBean flowTag) { this.flowTag = flowTag; } public int getColumnIndex() { return columnIndex; } public void setColumnIndex(int columnIndex) { this.columnIndex = columnIndex; } public int getRowIndex() { return rowIndex; } public void setRowIndex(int rowIndex) { this.rowIndex = rowIndex; } public Handler getViewHandler() { return viewHandler; } public FlowView setViewHandler(Handler viewHandler) { this.viewHandler = viewHandler; return this; } class ReloadImageThread extends Thread { @Override public void run() { if (flowTag != null) { BufferedInputStream buf; try { buf = new BufferedInputStream(flowTag.getAssetManager() .open(flowTag.getFileName())); bitmap = BitmapFactory.decodeStream(buf); } catch (IOException e) { e.printStackTrace(); } ((Activity) context).runOnUiThread(new Runnable() { public void run() { if (bitmap != null) { setImageBitmap(bitmap); } } }); } } } class LoadImageThread extends Thread { LoadImageThread() { } public void run() { if (flowTag != null) { BufferedInputStream buf; try { buf = new BufferedInputStream(flowTag.getAssetManager() .open(flowTag.getFileName())); bitmap = BitmapFactory.decodeStream(buf); } catch (IOException e) { e.printStackTrace(); } ((Activity) context).runOnUiThread(new Runnable() { public void run() { if (bitmap != null) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); LayoutParams lp = getLayoutParams(); int layoutHeight = (height * flowTag.getItemWidth()) / width; if (lp == null) { lp = new LayoutParams(flowTag.getItemWidth(), layoutHeight); } setLayoutParams(lp); setImageBitmap(bitmap); Handler h = getViewHandler(); Message m = h.obtainMessage(flowTag.what, width, layoutHeight, FlowView.this); h.sendMessage(m); } } }); // } } } } }