5
回答
自定义 Gallery 与 AsyncTask
终于搞明白,存储TCO原来是这样算的>>>   

@邓凡平 你好,想跟你请教个问题:自定义了一个可以缩放图片的Gallery , Gallery 中显示的每个图片先要从网络用 AsyncTask 下载。问题是:用AsyncTask下载到图片后,setImageBitmap给Gallery 中的View,手势滑动下一个View 的时候却不能显示图片.

GalleryAdapter.java  // 适配器

/**  
 * GalleryAdapter.java
 * @version 1.0
 */
package com.techrare.android.utils;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;

import com.techrare.android.R;
import com.techrare.android.activitys.DownloadShowActivity;
import com.techrare.android.network.HttpHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;

public class GalleryAdapter extends BaseAdapter {

	public String picPath = Environment.getExternalStorageDirectory()
			.toString() + File.separator;
	private Activity context;
	private ArrayList<String> image_files;
	private ArrayList<String> image_urls;
	ProgressDialog pd;
	private int getview_count;

	public GalleryAdapter(Activity context, ArrayList<String> image_files, ArrayList<String> image_urls) {
		this.context = context;
		this.image_files = image_files;
		this.image_urls = image_urls;
	}


	public int getCount() {
		int count = 0;
		if(image_files!=null)count = image_files.size();
		if(image_urls!=null)count =image_urls.size();
		return count;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	/**
	 * 設置翻頁效果
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("getView count", (getview_count++) + ":" + position);
		GalleryImageView view =  new GalleryImageView(context);
		view.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		return view;
	}
	
	public String set_image_url(String pic_file) {
		if (pic_file != null && !"".equals(pic_file)) {
			pic_file = context.getResources().getString(R.string.base_path)
					+ context.getResources().getString(R.string.cloth_path)
					+ pic_file;
			return pic_file;
		}
		return null;
	}
	
}

LoadGalleryImageTask.java  // AsyncTask 

/**
	 * 加載原圖
	 * 
	 * @author  yangjiannan 2012-8-6 TODO
	 */
	class LoadGalleryImageTask extends AsyncTask<String, Integer, Bitmap> {

		private Bitmap bitmap;
		private int position;
		private GalleryImageView view;
		private ProgressDialog pd;

		public LoadGalleryImageTask() {
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			position = gallery.getSelectedItemPosition();
			view = (GalleryImageView) gallery.getSelectedView();
			pd = new ProgressDialog(DownloadShowActivity.this);
			pd.setMessage("正在加載...");
			pd.setCancelable(true);
			pd.show();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			if (values[0] == 100&&view!=null&&bitmap!=null) {
				view.setScale(bitmap.getWidth(), bitmap.getHeight());
				view.setImageBitmap(bitmap);
				galleryAdapter.notifyDataSetChanged();
				setImageIndex(position + 1);
			}
			try{
				
				pd.dismiss();
			}catch(IllegalArgumentException e){
				e.printStackTrace();
			}
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			//从网路下载
			InputStream is = HttpHelper
					.getFileStreamFromUrl((images_urls
							.get(position)));
			//写入sd 卡缓存
			fileUtils.write2SDFromInput(cache_folder,
					images_urls.get(position), is);
			//从sd 卡中读取图片
			bitmap = fileUtils.decodeFromFile(cache_folder,
					images_urls.get(position));
			//通知更新UI
			publishProgress(100);
			return null;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}

	

	}

自定义可以缩放的 ImageView

package com.techrare.android.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.techrare.android.activitys.DownloadShowActivity;

public class GalleryImageView extends SuperImageView {
	@SuppressWarnings("unused")
	private static final String TAG = "ImageViewTouchBase";

	// This is the base transformation which is used to show the image
	// initially. The current computation for this shows the image in
	// it's entirety, letterboxing as needed. One could choose to
	// show the image as cropped instead.
	//
	// This matrix is recomputed when we go from the thumbnail image to
	// the full size image.
	protected Matrix mBaseMatrix = new Matrix();

	// This is the supplementary transformation which reflects what
	// the user has done in terms of zooming and panning.
	//
	// This matrix remains the same when we go from the thumbnail image
	// to the full size image.
	protected Matrix mSuppMatrix = new Matrix();

	// This is the final matrix which is computed as the concatentation
	// of the base matrix and the supplementary matrix.
	private final Matrix mDisplayMatrix = new Matrix();

	// Temporary buffer used for getting the values out of a matrix.
	private final float[] mMatrixValues = new float[9];

	// The current bitmap being displayed.
	// protected final RotateBitmap mBitmapDisplayed = new RotateBitmap(null);
	protected Bitmap image = null;

	int mThisWidth = -1, mThisHeight = -1;
	float mMaxZoom = 2.0f;// 最大缩放比例
	float mMinZoom;// 最小缩放比例

	private int imageWidth;// 图片的原始宽度
	private int imageHeight;// 图片的原始高度
	private float scaleRate;// 图片适应屏幕的缩放比例

	public GalleryImageView(Context context) {
		super(context);
		init();
		Log.i("method[GalleryImageView(Context context)]", "exed");
	}

	public GalleryImageView(Context context, int imageWidth, int imageHeight) {
		super(context);
		Log.i("method[GalleryImageView(Context context, int imageWidth, int imageHeight)]","exed");
		this.imageHeight = imageHeight;
		this.imageWidth = imageWidth;
		init();
	}

	public GalleryImageView(Context context, AttributeSet attrs,
			int imageWidth, int imageHeight) {
		super(context, attrs);
		this.imageHeight = imageHeight;
		this.imageWidth = imageWidth;
		init();
		Log.i("method[GalleryImageView(Context context, AttributeSet attrs, int imageWidth, int imageHeight)]","exed");
	}

	/**
	 * 计算图片要适应屏幕需要缩放的比例
	 */
	private void arithScaleRate() {
		Log.i("method[arithScaleRate]", "exed");
		float scaleWidth = 0;
		float scaleHeight = 0;
		scaleWidth = DownloadShowActivity.screenWidth / (float) imageWidth;
		scaleHeight = DownloadShowActivity.screenHeight / (float) imageHeight;
		scaleRate = Math.min(scaleWidth, scaleHeight);
	}

	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i("method[onKeyDown]", "exed");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			event.startTracking();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		Log.i("method[onKeyUp]", "exed");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
				&& !event.isCanceled()) {
			if (getScale() > 1.0f) {
				// If we're zoomed in, pressing Back jumps out to show the
				// entire image, otherwise Back returns the user to the gallery.
				zoomTo(1.0f);
				return true;
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	protected Handler mHandler = new Handler();

	@Override
	public void setImageBitmap(Bitmap bitmap) {
		Log.i("method[setImageBitmap]", "exed");
		super.setImageBitmap(bitmap);
		image = bitmap;
		
		// 计算适应屏幕的比例
		arithScaleRate();
		// 缩放到屏幕大小
		if (imageWidth < imageHeight) {
			zoomTo_(DownloadShowActivity.screenWidth / imageWidth,
					DownloadShowActivity.screenHeight / imageHeight,
					DownloadShowActivity.screenWidth / 2f,
					DownloadShowActivity.screenHeight / 2f);
		} else {
			zoomTo(scaleRate, DownloadShowActivity.screenWidth / 2f,
					DownloadShowActivity.screenHeight / 2f);
		}

		// 居中
		layoutToCenter();

		// imageView.zoomTo(scaleRate, Main.screenWidth / 2, Main.screenHeight /
		// 2
		// center(true, true);
	}

	// Center as much as possible in one or both axis. Centering is
	// defined as follows: if the image is scaled down below the
	// view's dimensions then center it (literally). If the image
	// is scaled larger than the view and is translated out of view
	// then translate it back into view (i.e. eliminate black bars).
	protected void center(boolean horizontal, boolean vertical) {
		Log.i("method[center]", "exed");
		// if (mBitmapDisplayed.getBitmap() == null) {
		// return;
		// }
		if (image == null) {
			return;
		}
		Log.i("------image width--------", image.getWidth() + "");
		Log.i("------image height--------", image.getHeight() + "");
		Matrix m = getImageViewMatrix();
		RectF rect = new RectF(0, 0, image.getWidth(), image.getHeight());
		// RectF rect = new RectF(0, 0, imageWidth*getScale(),
		// imageHeight*getScale());
		m.mapRect(rect);
		float height = rect.height();
		float width = rect.width();
		float deltaX = 0, deltaY = 0;
		Log.i("------width--------", getWidth() + "");
		Log.i("------height--------", getHeight() + "");
		if (vertical) {
			int viewHeight = getHeight();
			if (height < viewHeight) {
				deltaY = (viewHeight - height) / 2 - rect.top;
			} else if (rect.top > 0) {
				deltaY = -rect.top;
			} else if (rect.bottom < viewHeight) {
				deltaY = getHeight() - rect.bottom;
			}
		}
		if (horizontal) {
			int viewWidth = getWidth();
			if (width < viewWidth) {
				deltaX = (viewWidth - width) / 2 - rect.left;
			} else if (rect.left > 0) {
				deltaX = -rect.left;
			} else if (rect.right < viewWidth) {
				deltaX = viewWidth - rect.right;
			}
		}
		postTranslate(deltaX, deltaY);
		setImageMatrix(getImageViewMatrix());
	}

	private void init() {
		Log.i("method[init]", "exed");
		setScaleType(ImageView.ScaleType.MATRIX);
	}

	/**
	 * 设置图片居中显示
	 */
	public void layoutToCenter() {
		Log.i("method[layoutToCenter]", "exed");
		// 正在显示的图片实际宽高
		float width = imageWidth * getScale();
		float height = imageHeight * getScale();
		// 空白区域宽高
		float fill_width = DownloadShowActivity.screenWidth - width;
		float fill_height = DownloadShowActivity.screenHeight - height;
		// 需要移动的距离
		float tran_width = 0f;
		float tran_height = 0f;

		if (fill_width > 0) {
			tran_width = fill_width / 2;
		}
		if (fill_height > 0) {
			tran_height = fill_height / 2;
		}

		postTranslate(tran_width, tran_height);
		// setImageMatrix(getImageViewMatrix());
	}

	protected float getValue(Matrix matrix, int whichValue) {
		Log.i("method[getValue]", "exed");
		matrix.getValues(mMatrixValues);
		mMinZoom = (DownloadShowActivity.screenWidth / 2f) / imageWidth;
		return mMatrixValues[whichValue];
	}

	// Get the scale factor out of the matrix.
	protected float getScale(Matrix matrix) {
		float f = getValue(matrix, Matrix.MSCALE_X);
		return f;
	}

	protected float getScale() {
		Log.i("method[getScale]", "exed");
		return getScale(mSuppMatrix);
	}

	// Combine the base matrix and the supp matrix to make the final matrix.
	protected Matrix getImageViewMatrix() {
		Log.i("method[getImageViewMatrix]", "exed");
		// The final matrix is computed as the concatentation of the base matrix
		// and the supplementary matrix.
		mDisplayMatrix.set(mBaseMatrix);
		mDisplayMatrix.postConcat(mSuppMatrix);
		return mDisplayMatrix;
	}

	static final float SCALE_RATE = 1.25F;

	// Sets the maximum zoom, which is a scale relative to the base matrix. It
	// is calculated to show the image at 400% zoom regardless of screen or
	// image orientation. If in the future we decode the full 3 megapixel image,
	// rather than the current 1024x768, this should be changed down to 200%.
	protected float maxZoom() {
		Log.i("method[maxZoom]", "exed");
		if (image == null) {
			return 1F;
		}

		float fw = (float) image.getWidth() / (float) mThisWidth;
		float fh = (float) image.getHeight() / (float) mThisHeight;
		float max = Math.max(fw, fh) * 4;
		return max;
	}

	protected void zoomTo(float scale, float centerX, float centerY) {
		Log.i("method[zoomTo(float scale, float centerX, float centerY)]",
				"exed");
		if (scale > mMaxZoom) {
			scale = mMaxZoom;
		} else if (scale < mMinZoom) {
			scale = mMinZoom;
		}

		float oldScale = getScale();
		float deltaScale = scale / oldScale;

		mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
		setImageMatrix(getImageViewMatrix());
		center(true, true);
	}

	protected void zoomTo_(float scale_w, float scale_h, float centerX,
			float centerY) {

		Log.i("method[zoomTo_(float scale_w, float scale_h, float centerX, float centerY)]",
				"exed");
		/*
		 * if (scale > mMaxZoom) { scale = mMaxZoom; } else if (scale <
		 * mMinZoom) { scale = mMinZoom; }
		 */

		// float oldScale = getScale();
		// float deltaScale = scale / oldScale;

		mSuppMatrix.postScale(scale_w, scale_h, centerX, centerY);
		setImageMatrix(getImageViewMatrix());
		center(true, true);
	}

	protected void zoomTo(final float scale, final float centerX,
			final float centerY, final float durationMs) {
		Log.i("method[zoomTo(final float scale, final float centerX, final float centerY, final float durationMs)]",
				"exed");
		final float incrementPerMs = (scale - getScale()) / durationMs;
		final float oldScale = getScale();
		final long startTime = System.currentTimeMillis();

		mHandler.post(new Runnable() {
			public void run() {
				long now = System.currentTimeMillis();
				float currentMs = Math.min(durationMs, now - startTime);
				float target = oldScale + (incrementPerMs * currentMs);
				zoomTo(target, centerX, centerY);
				if (currentMs < durationMs) {
					mHandler.post(this);
				}
			}
		});
	}

	protected void zoomTo(float scale) {
		Log.i("method[zoomTo(float scale)]", "exed");
		float cx = getWidth() / 2F;
		float cy = getHeight() / 2F;

		zoomTo(scale, cx, cy);
	}

	protected void zoomToPoint(float scale, float pointX, float pointY) {
		Log.i("method[zoomToPoint]", "exed");
		float cx = getWidth() / 2F;
		float cy = getHeight() / 2F;

		panBy(cx - pointX, cy - pointY);
		zoomTo(scale, cx, cy);
	}

	protected void zoomIn() {
		Log.i("method[zoomIn() ]", "exed");
		zoomIn(SCALE_RATE);
	}

	protected void zoomOut() {
		Log.i("method[zoomOut()]", "exed");
		zoomOut(SCALE_RATE);
	}

	protected void zoomIn(float rate) {
		Log.i("method[zoomIn(float rate)]", "exed");
		if (getScale() >= mMaxZoom) {
			return; // Don't let the user zoom into the molecular level.
		} else if (getScale() <= mMinZoom) {
			return;
		}
		if (image == null) {
			return;
		}

		float cx = getWidth() / 2F;
		float cy = getHeight() / 2F;

		mSuppMatrix.postScale(rate, rate, cx, cy);
		setImageMatrix(getImageViewMatrix());
	}

	protected void zoomOut(float rate) {
		Log.i("method[zoomOut(float rate)]", "exed");
		if (image == null) {
			return;
		}

		float cx = getWidth() / 2F;
		float cy = getHeight() / 2F;

		// Zoom out to at most 1x.
		Matrix tmp = new Matrix(mSuppMatrix);
		tmp.postScale(1F / rate, 1F / rate, cx, cy);

		if (getScale(tmp) < 1F) {
			mSuppMatrix.setScale(1F, 1F, cx, cy);
		} else {
			mSuppMatrix.postScale(1F / rate, 1F / rate, cx, cy);
		}
		setImageMatrix(getImageViewMatrix());
		center(true, true);
	}

	public void postTranslate(float dx, float dy) {
		Log.i("method[postTranslate]", "exed");
		mSuppMatrix.postTranslate(dx, dy);
		setImageMatrix(getImageViewMatrix());
	}

	float _dy = 0.0f;

	protected void postTranslateDur(final float dy, final float durationMs) {
		Log.i("method[postTranslateDur]", "exed");
		_dy = 0.0f;
		final float incrementPerMs = dy / durationMs;
		final long startTime = System.currentTimeMillis();
		mHandler.post(new Runnable() {
			public void run() {
				long now = System.currentTimeMillis();
				float currentMs = Math.min(durationMs, now - startTime);

				postTranslate(0, incrementPerMs * currentMs - _dy);
				_dy = incrementPerMs * currentMs;

				if (currentMs < durationMs) {
					mHandler.post(this);
				}
			}
		});
	}

	protected void panBy(float dx, float dy) {
		Log.i("method[panBy]", "exed");
		postTranslate(dx, dy);
		setImageMatrix(getImageViewMatrix());
	}

	@Override
	public void setScale(int width, int height) {
		Log.i("method[setScale]", "exed");
		Log.i("--- view width------", getWidth() +"");
		Log.i("--- view height------", getHeight() +"");
		this.imageWidth = width;
		this.imageHeight = height;
	}
	
	public float getScaleRate() {
		Log.i("method[getScaleRate]", "exed");
		return scaleRate;
	}

	public int getImageWidth() {
		Log.i("method[getImageWidth]", "exed");
		return imageWidth;

	}

	public void setImageWidth(int imageWidth) {
		Log.i("method[setImageWidth]", "exed");
		this.imageWidth = imageWidth;
	}

	public int getImageHeight() {
		Log.i("method[getImageHeight]", "exed");
		return imageHeight;
	}

	public void setImageHeight(int imageHeight) {
		Log.i("method[getImageHeight]", "exed");
		this.imageHeight = imageHeight;
	}
}

自定义Gallery 
/**  

 * MyGallery.java

 * @version 1.0

 * @createTime 2011-12-9 下午03:42:53

 */

package com.techrare.android.utils;

 

import android.content.Context;

import android.graphics.Matrix;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.util.Log;

import android.view.GestureDetector;

import android.view.GestureDetector.SimpleOnGestureListener;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Gallery;

 

import com.techrare.android.activitys.DownloadShowActivity;

 

public class MyGallery extends Gallery {

 private GestureDetector gestureScanner;

 private GalleryImageView imageView;

 

 public MyGallery(Context context) {

 super(context);

 

 }

 

 public MyGallery(Context context, AttributeSet attrs, int defStyle) {

 super(context, attrs, defStyle);

 }

 

 public MyGallery(Context context, AttributeSet attrs) {

 super(context, attrs);

 

 gestureScanner = new GestureDetector(new MySimpleGesture());

 this.setOnTouchListener(new OnTouchListener() {

 

 float baseValue;

 float originalScale;

 

 public boolean onTouch(View v, MotionEvent event) {

 View view = MyGallery.this.getSelectedView();

 if (view instanceof GalleryImageView) {

 imageView = (GalleryImageView) view;

 

 if (event.getAction() == MotionEvent.ACTION_DOWN) {

 baseValue = 0;

 Log.i("originalScale", originalScale + "");

 originalScale = imageView.getScale();

 //if(originalScale==0)originalScale

 }

 if (event.getAction() == MotionEvent.ACTION_MOVE) {

 if (event.getPointerCount() == 2) {

 float x = event.getX(0) - event.getX(1);

 float y = event.getY(0) - event.getY(1);

 float value = (float) Math.sqrt(x * x + y * y);// 计算两点的距离

 // System.out.println("value:" + value);

 if (baseValue == 0) {

 baseValue = value;

 } else {

 float scale = value / baseValue;// 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。

 // scale the image

 imageView.zoomTo(originalScale * scale, x

 + event.getX(1), y + event.getY(1));

 

 }

 }

 }

 }

 return false;

 }

 

 });

 }

 

 @Override

 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

 float distanceY) {

 Log.i("onScroll", "yes");

 View view = MyGallery.this.getSelectedView();

 if (view instanceof GalleryImageView) {

 imageView = (GalleryImageView) view;

 

 float v[] = new float[9];

 Matrix m = imageView.getImageMatrix();

 m.getValues(v);

 // 图片实时的上下左右坐标

 float left, right;

 // 图片的实时宽,高

 float width, height;

 width = imageView.getScale() * imageView.getImageWidth();

 height = imageView.getScale() * imageView.getImageHeight();

 Log.i("onScroll width", width + "");

 Log.i("onScroll height", height + "");

 

 if ((int) width <= DownloadShowActivity.screenWidth

 && (int) height <= DownloadShowActivity.screenHeight)// 如果图片当前大小<屏幕大小,直接处理滑屏事件

 {

 super.onScroll(e1, e2, distanceX, distanceY);

 } else {

 left = v[Matrix.MTRANS_X];

 right = left + width;

 Rect r = new Rect();

 imageView.getGlobalVisibleRect(r);

 

 if (distanceX > 0)// 向左滑动

 {

 if (r.left > 0) {// 判断当前ImageView是否显示完全

 super.onScroll(e1, e2, distanceX, distanceY);

 } else if (right < DownloadShowActivity.screenWidth) {

 super.onScroll(e1, e2, distanceX, distanceY);

 } else {

 imageView.postTranslate(-distanceX, -distanceY);

 }

 } else if (distanceX < 0)// 向右滑动

 {

 if (r.right < DownloadShowActivity.screenWidth) {

 super.onScroll(e1, e2, distanceX, distanceY);

 } else if (left > 0) {

 super.onScroll(e1, e2, distanceX, distanceY);

 } else {

 imageView.postTranslate(-distanceX, -distanceY);

 }

 }

 

 }

 

 } else {

 super.onScroll(e1, e2, distanceX, distanceY);

 }

 return false;

 }

 

 @Override

 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

 float velocityY) {

 return false;

 }

 

 @Override

 public boolean onTouchEvent(MotionEvent event) {

 gestureScanner.onTouchEvent(event);

 switch (event.getAction()) {

 case MotionEvent.ACTION_UP:

 // 判断上下边界是否越界

 View view = MyGallery.this.getSelectedView();

 if (view instanceof GalleryImageView) {

 imageView = (GalleryImageView) view;

 float width = imageView.getScale() * imageView.getImageWidth();

 float height = imageView.getScale()

 * imageView.getImageHeight();

 if ((int) width <= DownloadShowActivity.screenWidth

 && (int) height <= DownloadShowActivity.screenHeight)// 如果图片当前大小<屏幕大小,判断边界

 {

 break;

 }

 float v[] = new float[9];

 Matrix m = imageView.getImageMatrix();

 m.getValues(v);

 float top = v[Matrix.MTRANS_Y];

 float bottom = top + height;

 if (top > 0) {

 imageView.postTranslateDur(-top/2, 200f);

 }

 Log.i("manga", "bottom:" + bottom);

 if (bottom < DownloadShowActivity.screenHeight) {

 imageView.postTranslateDur(

 (DownloadShowActivity.screenHeight - bottom)/2, 200f);

 }

 

 }

 break;

 }

 return super.onTouchEvent(event);

 }

 

 private class MySimpleGesture extends SimpleOnGestureListener {

 // 按两下的第二下Touch down时触发

 public boolean onDoubleTap(MotionEvent e) {

 View view = MyGallery.this.getSelectedView();

 if (view instanceof GalleryImageView) {

 imageView = (GalleryImageView) view;

 if (imageView.getScale() > imageView.getScaleRate()) {

 imageView.zoomTo(imageView.getScaleRate(),

 DownloadShowActivity.screenWidth / 2,

 DownloadShowActivity.screenHeight / 2, 200f);

 // imageView.layoutToCenter();

 } else {

 imageView.zoomTo(1.0f,

 DownloadShowActivity.screenWidth / 2,

 DownloadShowActivity.screenHeight / 2, 200f);

 }

 

 } else {

 

 }

 // return super.onDoubleTap(e);

 return true;

 }

 }

}


举报
Jnyang
发帖于5年前 5回/1K+阅
共有5个答案 最后回答: 5年前
Gallery我做过,也是图片异步加载,但没做得这么强大。刚开始我也遇到过拖动下一个图片不显示的问题,后来我试了下设置默认图片,结果就可以了,原因不知道。你可以试下,希望对你有帮助。
--- 共有 2 条评论 ---
郝炯淞不知道,如果拖动过猛的话,就会加载下下个view,而且刚开始加载gallery时,第一个view会加载两次。得看源代码吧,这个得请教牛人。如果你知道了,希望告诉我下。 5年前 回复
Jnyang楼上的是否知道, 为何 Gallery 有时会多次调用 BaseAdapter 中的 getView 方法呢? 5年前 回复

这里返回的是null回调的时候,拿不到你获取的Bitmap.


@Override

47         protected Bitmap doInBackground(String... params) {
48             //从网路下载
49             InputStream is = HttpHelper
50                     .getFileStreamFromUrl((images_urls
51                             .get(position)));
52             //写入sd 卡缓存
53             fileUtils.write2SDFromInput(cache_folder,
54                     images_urls.get(position), is);
55             //从sd 卡中读取图片
56             bitmap = fileUtils.decodeFromFile(cache_folder,
57                     images_urls.get(position));
58             //通知更新UI
59             publishProgress(100);
60             return null;
61         }
--- 共有 1 条评论 ---
Jnyang bitmap 全局變量已經在 doInBackground 中賦值了 5年前 回复
你好楼主  你不能把可放大缩小imageview的代码给我发一份学习下  我的邮箱chenguandong@163.com    非常感谢
请问一下你在实例化 GalleryAdapter的时候是怎么传入image_files的.也就是是怎么在主线程中获取onPostExecute传回的数据
顶部