我想在Android应用程序中展示一个动态GIF图片,就像下面这张图片。我尝试过使用WebView但没有成功。如何在应用程序中展示动态gif?
我想在Android应用程序中展示一个动态GIF图片,就像下面这张图片。我尝试过使用WebView但没有成功。如何在应用程序中展示动态gif?
<pl.droidsonroids.gif.GifImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_anim"/>
找到你的gif文件并将其放在src属性下面。就这样!
你可以使用Glide:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
Glide
。<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#62b849"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/splace_image_view"
android:layout_centerInParent="true"/>
</RelativeLayout>
在你的Java
文件中
ImageView imageView = findViewById(R.id.splace_image_view);
Glide
.with(this)
.load(R.drawable.football)
.into(imageView);
在Android中显示GIF图像的最佳和最简单的解决方案,它可以完美地工作:
Open layout folder and put this code where you want to display GIF image: e-g activity_main.xml
<pl.droidsonroids.gif.GifImageView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:src="@drawable/your_gif_file_name"/>
android:src="@drawable/your_gif_file_name", Replace 'your_gif_file_name' with your desired gif image file
您不需要使用任何库,只需使用以下代码:
步骤1: 创建一个名为GIFView.java的文件
package com.thigale.testproject;
/**
* Created by Thigale Sameer on 11-12-16.
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import java.io.InputStream;
public class GifView extends View {
public Movie mMovie;
public long movieStart;
private int gifId;
public GifView(Context context) {
super(context);
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeView(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "src", 0));
}
public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeView(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "src", 0));
}
private void initializeView(final int id) {
InputStream is = getContext().getResources().openRawResource(id);
mMovie = Movie.decodeStream(is);
this.gifId = id;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
super.onDraw(canvas);
long now = android.os.SystemClock.uptimeMillis();
if (movieStart == 0) {
movieStart = now;
}
if (mMovie != null) {
int relTime = (int) ((now - movieStart) % mMovie.duration());
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
this.invalidate();
}
}
public void setGIFResource(int resId) {
this.gifId = resId;
initializeView(this.gifId);
}
public int getGIFResource() {
return this.gifId;
}
}
步骤 2: 在 res/attrs.xml 文件中添加以下行
<declare-styleable name="GIFView">
<attr name="src" format="reference" />
</declare-styleable>
步骤3: 在特定的活动中,在 AndroidManifest.xml 中添加此行
android:hardwareAccelerated="false"
步骤4:在您的XML中创建此视图:
<com.thigale.testproject.GifView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
components:src="@drawable/loading" />
步骤5 在创建视图的父活动中添加以下行:
xmlns:components="http://schemas.android.com/apk/res-auto"
我也试过同样的事情,但 Android 无法显示具有动画效果的 gif 图像。如果您想实现相同的效果,则需要提取几个帧并使用逐帧动画。
您可以在以下链接中参考。 http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_(large).gif/200px-Rotating_earth_(large).gif");
这将在您的应用程序中显示gif图像。
希望能帮到您!祝好运!
build.gradle(:app)
dependencies {
implementation 'com.facebook.fresco:fresco:3.0.0'
implementation 'com.facebook.fresco:animated-gif:3.0.0'
implementation 'com.facebook.fresco:animated-webp:3.0.0'
implementation 'com.facebook.fresco:webpsupport:3.0.0'
}
activity_main.xml
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/displaygif"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
app:srcCompat="@drawable/anyimage"
/>
MainActivity.java
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
...
//convert string to Uri
Uri myUri = Uri.parse("..anygifUri..");
SimpleDraweeView displaygif = (SimpleDraweeView) findViewById(R.id.displaygif);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(myUri)
.setAutoPlayAnimations(true)
.build();
displaygif.setController(controller);
尝试这种方式:
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
long moviestart1;
public GIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.hxps);
is1=context.getResources().openRawResource(R.drawable.cartoon);
movie=Movie.decodeStream(is);
movie1=Movie.decodeStream(is1);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
if(moviestart1==0)
{
moviestart1=now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
int relTime1=(int)((now - moviestart1)% movie1.duration());
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie1.setTime(relTime1);
movie.draw(canvas,0,0);
movie1.draw(canvas,10,300);
this.invalidate();
}