Android¥¢¥×¥ê¤ò³«È¯¤¹¤ë¤¿¤á¤Î´ðÁäò¤Þ¤È¤á¤Æ¤¢¤ê¤Þ¤¹¡£

Step1

src/jp.co.example.practice/ImageLoaderTask.java¤ò¿·µ¬ºîÀ®¤·¤Þ¤¹¡£
  1. src/jp.co.example.practice¤ò±¦¥¯¥ê¥Ã¥¯¤·¡¢¿·µ¬¢ª¥¯¥é¥¹¤òÁªÂò
  2. ¼¡¤ÎÍͤËÆþÎϤ·¡¢´°Î»¤ò²¡²¼

Step2

¼¡¤ÎÆâÍƤËÊѹ¹¤·¤Æ¤¯¤À¤µ¤¤¡£
public class ImageLoaderTask extends AsyncTask<String, Void, Bitmap> {
	private static final String TAG = "AsyncImageLoader";
	private HashMap<String, SoftReference<Bitmap>> mImageCache = new HashMap<String, SoftReference<Bitmap>>();
	private int mLoadCount = 0;
	private ImageView mImageView;
	private ImageCallBack mImageCallBack;

	public ImageLoaderTask() {
	}

	public ImageLoaderTask(ImageView imageView, ImageCallBack imageCallBack) {
		mImageView = imageView;
		mImageCallBack = imageCallBack;
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap bitmapImage = null;
		String profileImageUrl = params[0];
		// ²èÁü¤¬¥­¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤«¥Á¥§¥Ã¥¯¤·¡¢¤Ê¤±¤ì¤Ð¼èÆÀ¤·¤Ë¤¤¤¯
		if (mImageCache.containsKey(profileImageUrl)) {
			SoftReference<Bitmap> softReference = mImageCache
					.get(profileImageUrl);
			if (softReference != null) {
				bitmapImage = softReference.get();
			}
		}
		if (bitmapImage == null) {
			bitmapImage = loadProfileImage(profileImageUrl);
			mImageCache.put(profileImageUrl, new SoftReference<Bitmap>(
					bitmapImage));
		}

		return bitmapImage;
	}

	@Override
	protected void onPostExecute(Bitmap bitmap) {
		mImageCallBack.imageLoaded(bitmap, mImageView);
	}

	private Bitmap loadProfileImage(String profileImageUrl) {
		Bitmap profileImage;
		try {
			// ¥×¥í¥Õ¥£¡¼¥ë²èÁü¤Î¼èÆÀ
			// ²¿Åù¤«¤Î¸¶°ø¤Ç¥¨¥é¡¼¤Ë¤Ê¤Ã¤¿¾ì¹ç¡¢3²ó¤Þ¤Ç¥ê¥È¥é¥¤¤ò¹Ô¤¦
			profileImage = createBitmap(profileImageUrl);
			if (profileImage != null) {
				profileImage = zoomBitmap(profileImage,
						Constant.PROFILE_IMAGE_WIDTH,
						Constant.PROFILE_IMAGE_HEIGHT);
				profileImage = getRoundedCornerBitmap(profileImage,
						Constant.PROFILE_IMAGE_ROUND);
			} else {
				if (mLoadCount < Constant.MAX_LOAD_COUNT) {
					mLoadCount++;
					profileImage = loadProfileImage(profileImageUrl);
				}
			}
			mLoadCount = 0;
		} catch (IOException e) {
			profileImage = null;
			Log.d(TAG, "error", e);
		}
		return profileImage;
	}

	// ²èÁü²Ã¹©¡Ê·è¤á¤é¤ì¤¿¥µ¥¤¥º¤Ë²èÁü¤ò³È½Ì¤¹¤ë¡Ë
	public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight);
		return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
	}

	// ²èÁü²Ã¹©¡Ê³Ñ´Ý¤Ë¤¹¤ë¡Ë
	public Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(Constant.ABOUT_ROUND_COLOR);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public Bitmap createBitmap(String imageUrl) throws IOException {
		return BitmapFactory.decodeStream(new URL(imageUrl).openStream());
	}

	public interface ImageCallBack {
		void imageLoaded(Bitmap bitmapImage, ImageView view);
	}
}

¤³¤Î¥Ú¡¼¥¸¤Ø¤Î¥³¥á¥ó¥È

goU37j <a href="http://uwilwzdqqeng.com/">uwilwzdqqeng</a>, [url=http://bvwcifnebudt.com/]bvwcifnebudt[/url], [link=http://duhoyuvlenlm.com/]duhoyuvlenlm[/link], http://etmtydelguqj.com/

0
Posted by caefujhlux 2013ǯ11·î14Æü(ÌÚ) 19:22:52 ÊÖ¿®

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹