如何显示GIF文件
How to display GIF file
我使用此 library 作为显示 'gif' 文件的指南。在 drawable
中使用保存的 gif
时,它会正确显示 gif
我这样称呼它
<pl.droidsonroids.gif.GifImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgView1"
android:src="@drawable/gifFile"
/>
但是当删除 src
并尝试像这样初始化 Activity 中的 GifImageView 时
GifImageView gifFromFile = (GifImageView) findViewById(R.id.imgView1);
gifFromFile.setImageBitmap(Utils.getBitmapImagefromStorage(context, imageHome));
其中 getBitmapImagefromStorage
获取路径,imageHome
获取文件名 gif 文件不会像图像一样播放它的唯一显示。据说 If given drawable is not a GIF then mentioned Views work like plain ImageView and ImageButton.
但我提供的文件是 gif
。我想知道我是否正确使用它。
文件也可以是 png
或 gif
所以我需要支持这两个。
GifTextView gifImageView;
在onCreate
gifImageView = (GifTextView) findViewById(R.id.imageView);
在onCreate中调用此方法
public void playGif(){
Animation fadeout = new AlphaAnimation(1.f, 1.f);
fadeout.setDuration(2500); // You can modify the duration here
fadeout.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
gifImageView.setBackgroundResource(R.drawable.gif_image);//your gif file
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
gifImageView.startAnimation(fadeout);
}
在你的布局文件中
<pl.droidsonroids.gif.GifTextView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="fill_parent"
/>
来自XML
最简单的方法是像普通 ImageView 一样使用 GifImageView(或 GifImageButton):
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/src_anim"
android:background="@drawable/bg_anim"
/>
如果 android:src and/or android:background 声明的可绘制对象是 GIF 文件,那么它们将被自动识别为 GifDrawables 和动画。如果给定的可绘制对象不是 GIF,则提到的视图像普通 ImageView 和 ImageButton 一样工作。
GifTextView 允许您将 GIF 用作复合绘图和背景。
<pl.droidsonroids.gif.GifTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/left_anim"
android:drawableStart="@drawable/left_anim"
android:background="@drawable/bg_anim"
/>
来自Java代码
GifImageView、GifImageButton 和 GifTextView 还实现了 setter 挂钩。因此可以通过调用 setImageResource(int resId)
和 setBackgroundResource(int resId)
来设置动画 GIF
GifDrawable 可以直接从各种来源构建:
//asset file
GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );
//resource (drawable or raw)
GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );
//byte array
byte[] rawGifBytes = ...
GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );
//FileDescriptor
FileDescriptor fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
GifDrawable gifFromFd = new GifDrawable( fd );
//file path
GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );
//file
File gifFile = new File(getFilesDir(),"anim.gif");
GifDrawable gifFromFile = new GifDrawable(gifFile);
//AssetFileDescriptor
AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
GifDrawable gifFromAfd = new GifDrawable( afd );
//InputStream (it must support marking)
InputStream sourceIs = ...
BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
GifDrawable gifFromStream = new GifDrawable( bis );
//direct ByteBuffer
ByteBuffer rawGifBytes = ...
GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );
如果不再需要 GifDrawable,InputStreams 会在终结器中自动关闭,因此您无需显式关闭它们。调用 recycle()
也将关闭基础输入源。
请注意,所有输入源都需要能够倒回开头。需要正确播放动画 GIF(其中动画是可重复的),因为后续帧是按需从源解码的。
尝试通过这种方式在您的应用中处理 GIF
public class PlayGifView extends View{
private static final int DEFAULT_MOVIEW_DURATION = 1000;
private int mMovieResourceId;
private Movie mMovie;
private long mMovieStart = 0;
private int mCurrentAnimationTime = 0;
@SuppressLint("NewApi")
public PlayGifView(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* Starting from HONEYCOMB have to turn off HardWare acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
public void setImageResource(int mvId){
this.mMovieResourceId = mvId;
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(mMovie != null){
setMeasuredDimension(mMovie.width(), mMovie.height());
}else{
setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mMovie != null){
updateAnimtionTime();
drawGif(canvas);
invalidate();
}else{
drawGif(canvas);
}
}
private void updateAnimtionTime() {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) {
mMovieStart = now;
}
int dur = mMovie.duration();
if (dur == 0) {
dur = DEFAULT_MOVIEW_DURATION;
}
mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}
private void drawGif(Canvas canvas) {
mMovie.setTime(mCurrentAnimationTime);
mMovie.draw(canvas, 0, 0);
canvas.restore();
}
}
在你的activityclass
PlayGifView pGif = (PlayGifView) findViewById(R.id.viewGif);
pGif.setImageResource(R.drawable.yourgifimage);
然后在你的 XML
<YourPackageName.PlayGifView
android:id="@+id/viewGif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
我尝试使用 GifDrawable
File gifFile = new File(context.getExternalFilesDir(null)
.getAbsolutePath(), imageHome);
GifDrawable gifFromPath = new GifDrawable(gifFile);
然后我没有使用 setImageBitmap
,而是使用 setImageDrawable
GifImageView gifImageView = (GifImageView) findViewById(R.id.textViewDealFinder);
gifImageView.setImageDrawable(gifFromPath);
我使用此 library 作为显示 'gif' 文件的指南。在 drawable
中使用保存的 gif
时,它会正确显示 gif
我这样称呼它
<pl.droidsonroids.gif.GifImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgView1"
android:src="@drawable/gifFile"
/>
但是当删除 src
并尝试像这样初始化 Activity 中的 GifImageView 时
GifImageView gifFromFile = (GifImageView) findViewById(R.id.imgView1);
gifFromFile.setImageBitmap(Utils.getBitmapImagefromStorage(context, imageHome));
其中 getBitmapImagefromStorage
获取路径,imageHome
获取文件名 gif 文件不会像图像一样播放它的唯一显示。据说 If given drawable is not a GIF then mentioned Views work like plain ImageView and ImageButton.
但我提供的文件是 gif
。我想知道我是否正确使用它。
文件也可以是 png
或 gif
所以我需要支持这两个。
GifTextView gifImageView;
在onCreate
gifImageView = (GifTextView) findViewById(R.id.imageView);
在onCreate中调用此方法
public void playGif(){
Animation fadeout = new AlphaAnimation(1.f, 1.f);
fadeout.setDuration(2500); // You can modify the duration here
fadeout.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
gifImageView.setBackgroundResource(R.drawable.gif_image);//your gif file
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
gifImageView.startAnimation(fadeout);
}
在你的布局文件中
<pl.droidsonroids.gif.GifTextView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="fill_parent"
/>
来自XML
最简单的方法是像普通 ImageView 一样使用 GifImageView(或 GifImageButton):
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/src_anim"
android:background="@drawable/bg_anim"
/>
如果 android:src and/or android:background 声明的可绘制对象是 GIF 文件,那么它们将被自动识别为 GifDrawables 和动画。如果给定的可绘制对象不是 GIF,则提到的视图像普通 ImageView 和 ImageButton 一样工作。
GifTextView 允许您将 GIF 用作复合绘图和背景。
<pl.droidsonroids.gif.GifTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/left_anim"
android:drawableStart="@drawable/left_anim"
android:background="@drawable/bg_anim"
/>
来自Java代码
GifImageView、GifImageButton 和 GifTextView 还实现了 setter 挂钩。因此可以通过调用 setImageResource(int resId)
和 setBackgroundResource(int resId)
GifDrawable 可以直接从各种来源构建:
//asset file
GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );
//resource (drawable or raw)
GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );
//byte array
byte[] rawGifBytes = ...
GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );
//FileDescriptor
FileDescriptor fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
GifDrawable gifFromFd = new GifDrawable( fd );
//file path
GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );
//file
File gifFile = new File(getFilesDir(),"anim.gif");
GifDrawable gifFromFile = new GifDrawable(gifFile);
//AssetFileDescriptor
AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
GifDrawable gifFromAfd = new GifDrawable( afd );
//InputStream (it must support marking)
InputStream sourceIs = ...
BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
GifDrawable gifFromStream = new GifDrawable( bis );
//direct ByteBuffer
ByteBuffer rawGifBytes = ...
GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );
如果不再需要 GifDrawable,InputStreams 会在终结器中自动关闭,因此您无需显式关闭它们。调用 recycle()
也将关闭基础输入源。
请注意,所有输入源都需要能够倒回开头。需要正确播放动画 GIF(其中动画是可重复的),因为后续帧是按需从源解码的。
尝试通过这种方式在您的应用中处理 GIF
public class PlayGifView extends View{
private static final int DEFAULT_MOVIEW_DURATION = 1000;
private int mMovieResourceId;
private Movie mMovie;
private long mMovieStart = 0;
private int mCurrentAnimationTime = 0;
@SuppressLint("NewApi")
public PlayGifView(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* Starting from HONEYCOMB have to turn off HardWare acceleration to draw
* Movie on Canvas.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
public void setImageResource(int mvId){
this.mMovieResourceId = mvId;
mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(mMovie != null){
setMeasuredDimension(mMovie.width(), mMovie.height());
}else{
setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mMovie != null){
updateAnimtionTime();
drawGif(canvas);
invalidate();
}else{
drawGif(canvas);
}
}
private void updateAnimtionTime() {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) {
mMovieStart = now;
}
int dur = mMovie.duration();
if (dur == 0) {
dur = DEFAULT_MOVIEW_DURATION;
}
mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}
private void drawGif(Canvas canvas) {
mMovie.setTime(mCurrentAnimationTime);
mMovie.draw(canvas, 0, 0);
canvas.restore();
}
}
在你的activityclass
PlayGifView pGif = (PlayGifView) findViewById(R.id.viewGif);
pGif.setImageResource(R.drawable.yourgifimage);
然后在你的 XML
<YourPackageName.PlayGifView
android:id="@+id/viewGif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
我尝试使用 GifDrawable
File gifFile = new File(context.getExternalFilesDir(null)
.getAbsolutePath(), imageHome);
GifDrawable gifFromPath = new GifDrawable(gifFile);
然后我没有使用 setImageBitmap
,而是使用 setImageDrawable
GifImageView gifImageView = (GifImageView) findViewById(R.id.textViewDealFinder);
gifImageView.setImageDrawable(gifFromPath);