Set GIF on Background of button

孤街醉人 提交于 2019-12-23 02:28:09

问题


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.


回答1:


I have implemented this in my application a day ago.Although this tutorial is >about ImageView you can easily convert the code to your advantage.

First of all, it depends on your desire.

1: Repeatedly continue to play the Gif (It is easy)

2: Play only once similar to Facebook app "Like" button (Some work is required)


Include this library to your gradle.build

compile '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"
        />

To play the 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);



}

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.

To split your gif and get the last frame of your gif, you can use this online tool:

https://ezgif.com/split


Use this resources and inform me... Cheers!



来源:https://stackoverflow.com/questions/47547226/set-gif-on-background-of-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!