日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

加载中动画(gif)

發布時間:2023/12/9 编程问答 148 豆豆
生活随笔 收集整理的這篇文章主要介紹了 加载中动画(gif) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家都知道,Android開發模擬器為了節省內存,一般不支持直接顯示gif圖片,即使你強制設置了,也只會顯示圖片的第一幀。看到網上也有許多的方法,來實現此功能,可都比較的繁瑣,需要修改android源代碼來實現或者用gif解析器來實現。在此文章中,這里教大家一種比較簡潔的一個方法,你可以把這個類當做是一種工具類。用的時候,直接搬到程序里面,更改下圖片的資源,就可以非常輕松的顯示gif圖片了。

?


步驟1看一下這個工具類的實例代碼:MyGifView.java

import?android.content.Context;

import?android.graphics.Canvas;

import?android.graphics.Movie;

import?android.util.AttributeSet;

import?android.view.View;

?

public?class?MyGifView?extends?View{

private?long?movieStart;

private?Movie?movie;

????//此處必須重寫該構造方法

public?MyGifView(Context?context,AttributeSet?attributeSet)?{

super(context,attributeSet);

//以文件流(InputStream)讀取進gif圖片資源

movie=Movie.decodeStream(getResources().openRawResource(R.drawable.keyboard));

}

?

@Override

protected?void?onDraw(Canvas?canvas)?{

long?curTime=android.os.SystemClock.uptimeMillis();

//第一次播放

if?(movieStart?==?0)?{

movieStart?=?curTime;

}

if?(movie?!=?null)?{

int?duraction?=?movie.duration();

int?relTime?=?(int)?((curTime-movieStart)%duraction);

movie.setTime(relTime);

movie.draw(canvas,?0,?0);

//強制重繪

invalidate();

}

super.onDraw(canvas);

}

}

此工具類中,只做了2件事情。1,構造方法;2,重寫了onDraw()方法。大家以后用的話,只需拷貝此類到你的工程下即可起作用。


步驟2布局文件代碼?activity_main.xml

<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"

????xmlns:tools="http://schemas.android.com/tools"

????android:layout_width="match_parent"

????android:layout_height="match_parent"?>

?

????<TextView?

???????android:text="====Gif圖片測試布局===="

????????android:layout_height="wrap_content"

???????android:layout_width="wrap_content"

???????/>

????

?????<com.example.showgifimage.MyGifView?

???????android:id="@+id/iv"

???????android:layout_height="wrap_content"

???????android:layout_width="wrap_content"

???????android:layout_margin="20dp"

???????/>?

???

</LinearLayout>

布局文件中,注意的是:標簽的設置?<com.example.showgifimage.MyGifView/>.

格式:包名+.類名

?


步驟3:activity中調用

public?class?MainActivity?extends?Activity?{

????@Override

????public?void?onCreate(Bundle?savedInstanceState)?{

????????super.onCreate(savedInstanceState);

????????setContentView(R.layout.activity_main);

????}

}

????就這么輕松的把Gif圖片顯示了~~

總結

以上是生活随笔為你收集整理的加载中动画(gif)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。