使用glide用ImageView显示动画gif文件(android)

时间:2017-06-02 02:40:24

标签: android

enter image description here

然后我将gif文件设置为动画,我把它放在应用程序的imageview中。

GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(character6);
            Glide.with(this).load(R.raw.company_normal8).into(imageViewTarget);

这里是代码,我应用这个代码没有问题。

但是我有严重的问题,图像有残像。

我用photoshop制作了这个gif文件,它有2帧

有A帧和B帧。

但是在Android应用程序中,

A - > B - > A - > B(这是正常的)

但我的应用

A - > A + B - > A - > A + b(像这个残像)

1 个答案:

答案 0 :(得分:3)

您可以使用asGif

Glide.with(context)
    .load(imgUrl)
    .asGif()
    .placeholder(R.drawable.img)
    .crossFade()
    .into(imageView);

如果Glide不适合你,请为我工作

public class GIFView extends View{

    Movie movie;
    long moviestart;

    public GIFView(Context context) throws IOException { 
        super(context);
    }
    public GIFView(Context context, AttributeSet attrs) throws IOException{
        super(context, attrs);
    }
    public GIFView(Context context, AttributeSet attrs, int defStyle) throws IOException {
        super(context, attrs, defStyle);
    }

    public void loadGIFResource(Context context, int id)
    {
        //turn off hardware acceleration
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        InputStream is=context.getResources().openRawResource(id);
        movie = Movie.decodeStream(is);
    }

    public void loadGIFAsset(Context context, String filename)
    {
        InputStream is;
        try {
            is = context.getResources().getAssets().open(filename);
            movie = Movie.decodeStream(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (movie == null) {
            return;
        }

        long now=android.os.SystemClock.uptimeMillis();

        if (moviestart == 0) moviestart = now;

        int relTime;
        relTime = (int)((now - moviestart) % movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas,10,10);
        this.invalidate();
    }
}

XML

<yourpackagename.GIFView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="110dp"
        android:src="@drawable/yourgifimage"/>

设置图像

imagevw.loadGIFResource(this, R.drawable.yourgifimage);