0

I am trying to get a Image to fade at the start of an application without having to press a button. I have been looking around but it seems as if for some reason that people only know how to make it so that you need a button to make the image or object to perform the animation. I do have a animation file ready. The problem is getting the image to fade without a button.

package android.example.verification;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    MediaPlayer mysong;
    private View image;
    ImageView imageView;
    Animation animation;
    private Object TitleScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,"onCreate: started.");
        mysong = MediaPlayer.create(MainActivity.this, R.raw.bc);
        mysong.start();
        mysong.setLooping(true);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Logo
        int imageResource = getResources().getIdentifier("@drawable/logo", null, this.getPackageName());
        ImageView firstImage = (ImageView) findViewById(R.id.firstImage);
        firstImage.setImageResource(imageResource);
    }

    //Logo Fade Animation
    public void tapToFade(View view) {
        Button button = (Button)findViewById(R.id.firstImage);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade);
        button.startAnimation(animation);
    }

    //helps Music
    @Override
    protected void onPause() {
        super.onPause();
        mysong.release();
        finish();
    }

}
  • is your code even working? you are casting `R.id.firstImage` twice in your code to two different classes... can you describe more clearly what do you want to achievie? – snachmsm Sep 03 '21 at 06:31

2 Answers2

0

Firstly you can use Handler.postDelayed(Runnable, long) to run a code after a time delay (if you don't want to use a click event to start the animation). this code invokes the Runnable.run() method after 1000 milliseconds which starts the animation.

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
     @Override
     public void run() {
          Button button = (Button)findViewById(R.id.firstImage);
          Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade);
          button.startAnimation(animation);
     }
}, 1000);

Secondly you don't need to cast R.id.firstImage to specifically to Button to use the startAnimation, all classes extending the View class have this method.

Erfan Yeganegi
  • 311
  • 1
  • 9
0

what you want to do is called SplashScreen

Splash Screen will show an image when you open your App for a period of time(ex. 1000 ms or one second) then after that will display MainActivity .

You should create new activity called SpalshScreen then add this code in onCreate Method in SplashScreen.java

setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            /* Create an Intent that will start the Main-Activity. */

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();

        }
    }, 1000); //show this screen for 1 second
}

And then go to manifest you need to set spalshScreen as LAUNCHER .

<activity
android:name=".SplashActivity">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

that will show SplashScreen for 1 second and then will open MainActivity page.

also see this question

How do I make a splash screen?

Dharman
  • 30,962
  • 25
  • 85
  • 135