Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

Add a GIF in Android Studio Java

I need to add a GIF in my code snippet on the screen, i need it to be played continuously and without stopping I saw many blogs, QnAs on google but I didn't find the answer, here goes my MainActivity.java(Showing MainActivity.class as it was not allowing me to post the whole code) -

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView lightUp = findViewById(R.id.lightUp4);
        Button unLight = findViewById(R.id.unlight);
        ImageView AgarbattiBtn = findViewById(R.id.agarbattiBtn);
        MediaPlayer Sound_matchstick = MediaPlayer.create(this, R.raw.matchstick);
        MediaPlayer Om_Bhur = MediaPlayer.create(this, R.raw.om_bhur);

        lightUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView image1 = findViewById(R.id.diya);
                image1.setImageResource(R.drawable.lit_diya);
                lightUp.setVisibility(View.INVISIBLE);
                Sound_matchstick.start();
                if(lightUp.getVisibility() == View.INVISIBLE && AgarbattiBtn.getVisibility() == View.INVISIBLE){
                    Om_Bhur.start();
                }
            }
        });

        unLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView image2 = findViewById(R.id.diya);
                image2.setImageResource(R.drawable.not_lit_diya);
                AgarbattiBtn.setVisibility(View.VISIBLE);
                findViewById(R.id.agrabatti).setVisibility(View.INVISIBLE);
                lightUp.setVisibility(View.VISIBLE);
                Om_Bhur.stop();
            }
        });

        AgarbattiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AgarbattiBtn.setVisibility(View.INVISIBLE);
                findViewById(R.id.agrabatti).setVisibility(View.VISIBLE);
                if(lightUp.getVisibility() == View.INVISIBLE && AgarbattiBtn.getVisibility() == View.INVISIBLE){
                    Om_Bhur.start();
                }
            }
        });
    }
}

This is my activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/mandir"
        android:layout_width="508dp"
        android:layout_height="521dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.473"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.39"
        app:srcCompat="@mipmap/mandir" />

    <Button
        android:id="@+id/unlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.06"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.96" />

    <ImageView
        android:id="@+id/diya"
        android:layout_width="78dp"
        android:layout_height="62dp"
        app:layout_constraintBottom_toBottomOf="@+id/mandir"
        app:layout_constraintEnd_toEndOf="@+id/mandir"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="@+id/mandir"
        app:layout_constraintTop_toTopOf="@+id/mandir"
        app:layout_constraintVertical_bias="0.75"
        app:srcCompat="@mipmap/not_lit_diya" />

    <ImageView
        android:id="@+id/lightUp4"
        android:layout_width="142dp"
        android:layout_height="70dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/unlight"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/matchstick" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="68dp"
        android:layout_height="81dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.24"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.55"
        app:srcCompat="@drawable/krishna_ji" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="78dp"
        android:layout_height="84dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.76"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.55"
        app:srcCompat="@drawable/mata_rani" />

    <ImageView
        android:id="@+id/agarbattiBtn"
        android:layout_width="80dp"
        android:layout_height="105dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.86"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/agarbatti" />

    <ImageView
        android:id="@+id/agrabatti"
        android:layout_width="86dp"
        android:layout_height="104dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.59000003"
        app:srcCompat="@drawable/agarbatti" />
</androidx.constraintlayout.widget.ConstraintLayout>

Thanks in advance...

question from:https://stackoverflow.com/questions/66065263/add-a-gif-in-android-studio-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use 3rd party view library like this because custom solutions are too complicated to write from what I've seen.

Just import it into you project

dependencies {
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'
}

And use:

<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/src_anim"
    android:background="@drawable/bg_anim"
    />

If drawables declared by android:src and/or android:background are GIF files then they will be automatically recognized as GifDrawables and animated.

There are methods for GIF control such as:

  • stop() - stops the animation, can be called from any thread
  • start() - starts the animation, can be called from any thread
  • isRunning() - returns whether animation is currently running or not reset() - rewinds the animation, does not restart stopped one
  • setSpeed(float factor) - sets new animation speed factor, eg. passing 2.0f will double the animation speed seekTo(int position) seeks animation (within current loop) to given position (in milliseconds)
  • getDuration() - returns duration of one loop of the animation
  • getCurrentPosition() - returns elapsed time from the beginning of a
    current loop of animation

Please refer to the docs for more info :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...