在按钮的背景上设置GIF [英] Set GIF on Background of button

查看:160
本文介绍了在按钮的背景上设置GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在按钮的背景上应用GIF.我尝试了以下代码,方法是引用

is it possible to apply GIF on background of button.I have tried by the following code by taking the reference from this. And my code is below.

   public class GIFView extends View {
    Movie movie, movie1;
    InputStream is = null, is1 = null;
    long moviestart;

    public GIFView(Context context) {
        super(context);
        is = context.getResources().openRawResource(+R.drawable.cloudinary_animation);// avoid error ( "Expected resource of type raw")by +
        movie = Movie.decodeStream(is);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        System.out.println("now=" + now);
        if (moviestart == 0) { // first time
            moviestart = now;

        }
        System.out.println("\tmoviestart=" + moviestart);
        int relTime = (int) ((now - moviestart) % movie.duration());
        System.out.println("time=" + relTime + "\treltime=" + movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas, this.getWidth() / 2 - 20, this.getHeight() / 2 - 40);
        this.invalidate();
    }
}

现在,我想将此动画应用到按钮的背景上.

Now I want to apply this animation on the background of button.

推荐答案

一天前,我已经在我的应用程序中实现了此功能.尽管本教程是关于ImageView的,但是您可以轻松地将代码转换为您的优势.

首先,这取决于您的愿望.

First of all, it depends on your desire.

1:反复继续播放Gif(很简单)

2:类似于Facebook应用程序的赞"按钮,只能播放一次(需要做一些工作)


将此库包含到您的gradle.build

编译'pl.droidsonroids.gif:android-gif-drawable:1.2.8'

GifImageView gifImageView;

GifDrawable gifDrawable;


定义此变量:


Define this variable:

gifImageView = findViewById(R.id.splashGif);

<pl.droidsonroids.gif.GifImageView
        android:id="@+id/splashGif"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFDFF"
        />

播放GIF

 public void playGif() {


        try {
            gifDrawable = new GifDrawable(getActivity().getResources(), R.drawable.SplashGifId);
        } catch (IOException e) {
            e.printStackTrace();
        }


        gifDrawable.setLoopCount(1);

        gifDrawable.addAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationCompleted(int loopNumber) {

                gifImageView.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.SplashLastFrame));

            }
        });

        gifImageView.setImageDrawable(gifDrawable);



}


此方法将在GIF完成处理后播放gif R.drawable.SplashGifId,您会将GIF的最后一帧(R.drawable.SplashLastFrame)设置为按钮或ImageView.

This method will play the gif R.drawable.SplashGifId after finishing process of GIF you will set the last frame (R.drawable.SplashLastFrame) of GIF to your button or ImageView.

要拆分gif并获取gif的最后一帧,您可以使用以下在线工具:

https://ezgif.com/split

使用此资源,并通知我...干杯!

Use this resources and inform me... Cheers!

这篇关于在按钮的背景上设置GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆