編輯:關於 Android 程式設計
Android 原生的 ImageView 不會播放 GIF,但 android.graphics.Movie 提供了最輕量的解法:解碼成 Movie 物件後,在 onDraw() 中依時間軸畫出對應影格即可。
public class CommonGifView extends View { private Resources mResources; private Movie mMovie; private long startTime = 0; private float widthRatio; private float heightRatio; public CommonGifView(Context context) { this(context, null); } public CommonGifView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CommonGifView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mResources = context.getResources(); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_gif_view); int src_id = ta.getResourceId(R.styleable.custom_gif_view_gif_src, -1); setGifViewBg(src_id); ta.recycle(); } /** * 為View設置gif格式圖片背景 * @description: * @author ldm * @date 2016-2-18 上午9:21:16 */ private void setGifViewBg(int src_id) { if (src_id == -1) { return; } // 獲取對應資源文件的輸入流 InputStream is = mResources.openRawResource(src_id); mMovie = Movie.decodeStream(is);// 解碼輸入流為Movie對象 requestLayout(); } /* * 這個方法供Activity中使用 */ public void setGifStream(InputStream is) { mMovie = Movie.decodeStream(is); requestLayout(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); long now = SystemClock.uptimeMillis(); if (startTime == 0) { // 如果第一幀,記錄起始時間 startTime = now; } if (mMovie != null) {// 如果返回值不等於null,就說明這是一個GIF圖片 int duration = mMovie.duration();// 取出動畫的時長 if (duration == 0) { duration = 1000; } int currentTime = (int) ((now - startTime) % duration);// 算出需要顯示第幾幀 mMovie.setTime(currentTime); // mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height()); float scale = Math.min(widthRatio, heightRatio); canvas.scale(scale, scale); mMovie.draw(canvas, 0, 0); invalidate(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mMovie != null) {// 如果返回值不等於null,就說明這是一個GIF圖片 int w = mMovie.width();//寬度 int h = mMovie.height();//高度 if (w <= 0) { w = 1; } if (h <= 0) { h = 1; } int left = getPaddingLeft(); int right = getPaddingRight(); int top = getPaddingTop(); int bottom = getPaddingBottom(); int widthSize, heightSize; w += left + right; h += top + bottom; w = Math.max(w, getSuggestedMinimumWidth()); h = Math.max(h, getSuggestedMinimumHeight()); widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);//根據你提供的大小和MeasureSpec,返回你想要的大小值 heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); widthRatio = (float) widthSize / w; heightRatio = (float) heightSize / h; setMeasuredDimension(widthSize, heightSize); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } }
自訂屬性宣告於 res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_gif_view"> <attr name="gif_src" format="reference"></attr> </declare-styleable> </resources>
在 Activity 中使用:
public class MainActivity extends Activity { private CommonGifView view; private InputStream is; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = (CommonGifView) findViewById(R.id.gif_test); try { is = getAssets().open("test01.gif"); view.setGifStream(is); } catch (IOException e) { e.printStackTrace(); } } }
幾處設計細節值得說明。setGifViewBg() 負責從 res/raw 讀取資源並解碼,解碼後呼叫 requestLayout() 是必要的——因為 onMeasure() 需要知道 Movie 的尺寸才能量出正確大小。onMeasure() 裡以 resolveSizeAndState() 把內容尺寸與父容器約束調和,同時記下 widthRatio 與 heightRatio;onDraw() 取兩者最小值做等比縮放,避免 GIF 變形。
動畫推進的關鍵是那個取餘運算:(now - startTime) % duration 讓影格在動畫長度內不斷循環;而 duration 為 0 時給 1000 毫秒的預設值,是為了避免除零。最後 invalidate() 觸發下一輪繪製,動畫才會持續。若你只需要播放一次而非循環,把取餘改成 Math.min() 限制上限,並在播完時停止 invalidate() 即可。
Android 接入支付寶支付的完整實戰指南
提到支付寶,幾乎沒有人會感到陌生——日常購物付款、朋友之間轉賬,都離不開它。而對 Android 開發者來說,把這套成熟的支付能力接進自己的 App,幾乎是產品走向商業化
EventBus 3.0.0 使用總結:依賴引入、線程模型與黏性事件
前言:為什麼選 greenrobot 的 EventBus 「EventBus」其實是個通用叫法。Google 的 Guava 也附帶了一個 EventBus 功能,但
用自定義屬性為 Android 應用程式設定全域背景
為什麼使用自定義屬性 自定義屬性常用於自定義 View,但其實也可以用來統一管理應用程式的視覺設定。例如,若所有頁面都需要相同背景,就可以把背景值定義成屬性,再於主題中指
《Android 動畫高手成長記》:屬性動畫打造跳跳球效果
在動手實現本文的動畫效果之前,先來補幾個屬性動畫的關鍵知識點:ValueAnimator 與 ObjectAnimator,以及 Interpolator 插值器與 Ty