Created
April 13, 2011 08:53
-
-
Save amay077/917218 to your computer and use it in GitHub Desktop.
[Android]LazyImageView:画像の読み込みが終わるまでクルクルを表示する ImageView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.amay077.android.view; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.util.Random; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import jp.co.cosmoroot.gequu.R; | |
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.os.Handler; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.animation.Animation; | |
import android.view.animation.RotateAnimation; | |
import android.widget.ImageView; | |
/** | |
* 画像を読み込み終わるまでクルクルを表示する ImageView | |
* | |
* @author amay077 | |
*/ | |
public class LazyImageView extends ImageView { | |
/** 読み込み中に表示するクルクル回る画像 */ | |
private int loadingResID = R.drawable.loading; | |
/** 読み込み失敗時に画像 */ | |
private int noimageResID = R.drawable.no_image; | |
/** 読み込み中のクルクル回るアニメーション */ | |
private int startDegree = new Random().nextInt(300); | |
private RotateAnimation rotateAnim = new RotateAnimation(startDegree, startDegree + 300, Animation.RELATIVE_TO_SELF, 0.5f, | |
Animation.RELATIVE_TO_SELF, 0.5f); | |
private ScaleType orignalScaleType = ScaleType.CENTER; | |
private String imageUrl = ""; | |
private boolean isLoadCompleted = false; | |
private boolean nowLoading = true; | |
private Handler handler = new Handler(); | |
static private ExecutorService executor; | |
public LazyImageView(Context context) { | |
super(context); | |
initialize(context); | |
} | |
public LazyImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initialize(context); | |
} | |
public LazyImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
initialize(context); | |
} | |
/** 各コンストラクタから呼び出される共通初期化処理 */ | |
private void initialize(Context context) { | |
if (executor == null) executor = Executors.newCachedThreadPool(); | |
} | |
/** 読込中のクルクルを表示開始する */ | |
private void startLoadingAnimation() { | |
this.post(new Runnable() { | |
public void run() { | |
if (nowLoading == false) return; | |
setScaleType(ScaleType.CENTER); | |
setImageResource(loadingResID); | |
startAnimation(rotateAnim); | |
} | |
}); | |
} | |
/** 読込中のクルクルを表示終了、画像を空にする */ | |
private void stopLoadingAnimation() { | |
rotateAnim.reset(); | |
setAnimation(null); | |
setImageDrawable(null); | |
setScaleType(orignalScaleType); | |
} | |
/** 読み込まれていなかったら、タップして再読込する */ | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (!isLoadCompleted) setImageURL(imageUrl); | |
return super.onTouchEvent(event); | |
} | |
/** 画像の URL を設定する(Web-URL のみ対応) */ | |
public void setImageURL(final String url) { | |
if (imageUrl.equalsIgnoreCase(url)) return; | |
orignalScaleType = getScaleType(); | |
rotateAnim.setRepeatCount(Animation.INFINITE); | |
rotateAnim.setDuration(200); | |
startLoadingAnimation(); | |
isLoadCompleted = false; | |
nowLoading = true; | |
executor.submit(new Runnable() { | |
@Override | |
public void run() { | |
final Drawable d = getDrawable(url); | |
nowLoading = false; | |
imageUrl = url; | |
isLoadCompleted = true; | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
onLoadComplete(d); | |
} | |
}); | |
} | |
}); | |
} | |
/** 画像が読み込み終わったら呼び出されるので、ImageView を更新する */ | |
protected void onLoadComplete(Drawable drawable) { | |
stopLoadingAnimation(); | |
if (drawable != null) | |
this.setImageDrawable(drawable); | |
else this.setImageResource(noimageResID); | |
} | |
/** URL から画像をダウンロードし、 Drawable にして返す */ | |
private Drawable getDrawable(String url) { | |
try { | |
URL objUrl = new URL(url); | |
InputStream is = (InputStream) objUrl.getContent(); | |
Drawable d = Drawable.createFromStream(is, "src"); | |
return d; | |
} catch (Exception e) { | |
Log.w("LazyimageView", "getDrawable() failed.", e); | |
return null; | |
} | |
} | |
public boolean isLoadCompleted() { | |
return isLoadCompleted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment