【问题标题】:using glide to show animated gif file with ImageView (android)使用 glide 显示带有 ImageView (android) 的动画 gif 文件
【发布时间】:2017-06-02 12:10:12
【问题描述】:

然后我将 gif 文件制作成动画,然后我将这个文件放在应用程序的 imageview 中。

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

这是代码,我可以应用这个代码。

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

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

有A帧和B帧。

但在安卓应用中,

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

但我的应用

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

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以使用 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);
    

    【讨论】:

    • 我让我的来源像这样 Glide.with(this).load(R.raw.employer_normal).asGif().into(imageViewTarget);但是进入方法有红线:(
    • 我删除了错误但仍然是后台堆栈..我真的想删除这个
    • 感谢您的评论.. 但是如果我使用 GIfview 方式,图像会消失,,
    • 我终于解决了这个问题,我在 Gradle 选项中有一个很大的错误。我使用滑翔 3.5.0。当我更新到 3.8.0 时错误消失了
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2017-11-10
    • 2017-04-30
    • 2023-04-04
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多