Splash screen with fade in animation of ImageView - java

So i am trying to implement splash screen with image view fade in animation which i want to start simultaneously with the start of the splash screen activity. I also want splash screen activity to end after short delay (on touch event is optional), after animation of image view is finished.
My SplashScreen.java:
package hillbillys.delivery;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
public class SplashScreen extends AppCompatActivity implements Animation.AnimationListener {
protected Animation fadeIn;
protected ImageView img1;
protected ImageView img2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_splash);
fadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in);
/*
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.VISIBLE);
img1.startAnimation(fadeIn);
img2.startAnimation(fadeIn);
*/
Thread timerThread = new Thread(){
public void run(){
try{
sleep (2000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
timerThread.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(getBaseContext(), "Animation Stopped!", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
Application crashes every time i try to add the block of code in comment, no matter where i put it. Without fade in animation works everything just fine. Is there any way how to synchronize these two in easy way? Im quite new with coding so there may be some fatal mistake in what im trying to achieve.
My screeen_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff0d9"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo1"
android:layout_marginTop="250dp"
android:visibility="gone" />
<ImageView
android:layout_width="260dp"
android:layout_height="41dp"
android:id="#+id/imageView2"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo2"
android:visibility="gone" />
</LinearLayout>
My fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0"
/>
</set>

you need to initialize the ImageView before trying to access their properties. E.g.
img1 = (ImageView) findViewById(R.id.imageView);
img2 = (ImageView) findViewById(R.id.imageView2);
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.VISIBLE);
img1.startAnimation(fadeIn);
img2.startAnimation(fadeIn);

Like Blackbelt said you need to initialize,
final img1 = (ImageView) findViewById(R.id.imageView);
final img2 = (ImageView) findViewById(R.id.imageView2);
and if you want to the animation to start correctly after the view is created you need to do this
img1.post(new Runnable(){
img1.startAnimation(fadeIn);
});
img2.post(new Runnable(){
img2.startAnimation(fadeIn);
});

Animation animation= AnimationUtils.loadAnimation(this,R.anim.fadeIn);
img1.startAnimation(animation);

Related

Android w/ Java: Using Seekbar to adjust volume

I'm new to Android App development (on android studio) and to start off I'm creating a simple 'bonk' soundboard app, and testing it using the Pixel 2 android emulator. The goal of the app is to play the bonk noise whenever someone presses the BONK button on my main screen. I also wanted to implement a seekbar that adjusts the volume when you slide it. To do this I followed a simple implementation I found here on StackOverflow (shown below).
Even though the rest of my app works fine, for some reason my volume won't adjust whenever I slide the seekbar. Here is my code:
MainActivity.java
package com.example.bonk;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
Button bonkButton;
SeekBar volumeSeekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bonkButton = (Button)findViewById(R.id.bonkButton);
volumeSeekBar = (SeekBar)findViewById(R.id.volumeSeekBar);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bonksound);
bonkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
}
}
Volumizer.java
package com.example.bonk;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Volumizer extends Activity {
//Called when the activity is first created.
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
try {
volumeSeekbar = findViewById(R.id.volumeSeekBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
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">
<Button
android:id="#+id/bonkButton"
android:layout_width="296dp"
android:layout_height="232dp"
android:text="#string/bonk"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="#+id/volumeSeekBar"
android:layout_width="276dp"
android:layout_height="26dp"
android:layout_marginBottom="116dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please let me know if you have any ideas as of what I may be doing wrong. Thank you!
I was able to solve the problem by moving my initControls() function definition into my MainActivity.java class and calling it onCreate.

Android app uses way more memory than it should

I'm currently building an app but keep having memory issues. Our start screen, which only has a background and a button uses 160 MB ram usually. This must be way too much for what it is doing.
This trend continues throughout my app.
I have included the XML and java code. I also looked at the allocation of the memory. Almost all of the memory is occupied by three things. Three dispatches to be precise. I have no clue what this means.
package com.example.tonymurchison.illuminandus;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView startButton;
private ImageView fadeView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
startButton = (ImageView) findViewById(R.id.start_button);
}
public void startButtonClick(View v){
//TODO create animation
Intent intent = new Intent(MainActivity.this,LevelSelect.class);
startActivity(intent);
finish();
}
private void animateIn(ImageView image) {
Animation fadein = new AlphaAnimation(0.f, 1.f);
fadein.setDuration(500);
final View viewToAnimate = image;
fadein.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation){}
#Override
public void onAnimationRepeat(Animation animation){}
#Override
public void onAnimationEnd(Animation animation){
Intent intent = new Intent(MainActivity.this,LevelSelect.class);
startActivity(intent);
}
});
image.startAnimation(fadein);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.tonymurchison.illuminandus.MainActivity"
android:background="#drawable/titlescreen_background">
<ImageView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="#+id/start_button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/start_button"
android:onClick="startButtonClick"/>
</RelativeLayout>

how to display random image in splashscreen in android?

i'm creating an android application with, of course, a loading/splash screen everytime the application is opened. i've created a splash screen xml and class. but with only one image.
what i want to happen is to load different images and random texts everytime the application is launched.
here's my code on my splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/exit1" />
</RelativeLayout>
and here's my code in splashscreen.java
package travelph.project;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
public class SplashScreen extends Activity {
// Randomise a background
int[] yourListOfImages= {R.drawable.about1, R.drawable.background1
, R.drawable.bakya1, R.drawable.coconut1, R.drawable.exit1hdpi};
private static final int TIME = 5 * 1000;// 5 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Random random = new Random(System.currentTimeMillis());
int posOfImage = random.nextInt(yourListOfImages.length + 1);
ImageView imageView= (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(yourListOfImages[posOfImage]);
Intent intent = new Intent(SplashScreen.this, MainMenu.class);
startActivity(intent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, TIME);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, TIME);
}
#Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
code snippet would really be a big help. thank you.
below piece of your code should be strictly in onCreate (below under setContentView for example and above new Handler creation), not inside postDelayed -> run()
Random random = new Random(System.currentTimeMillis());
int posOfImage = random.nextInt(yourListOfImages.length + 1);
ImageView imageView= (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(yourListOfImages[posOfImage]);
you are setting image after TIME and immidaitelly after that you are calling intent to another activity and current is finishing
You need to change
imageView.setBackgroundResource(yourListOfImages[posOfImage]);
to
imageView.setImageResource(yourListOfImages[posOfImage]);

Animation: Scaling Button to extend to top of screen

I have a simple layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="My TextView" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button" />
</LinearLayout>
and on the click of this button, I am animating the TextView out of the top of the screen. I do so with this code:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView tv;
Animation mSlideOutTop;
Button button;
boolean in = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
button = (Button) findViewById(R.id.button);
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
in = true;
tv.startAnimation(mSlideOutTop);
tv.setVisibility(View.INVISIBLE);
}
});
}
}
where my animation is slide_out_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromYDelta="0%"
android:toYDelta="-100%" />
</set>
So my problem is: as the TextView animates out of the screen, I need the button to expand to the top of the screen. Right now, the TextView animates away, and the space where it was, remains empty, like this:
Any ideas on how to do this? Thanks!
To accomplish what you're trying to do, set the TextView's visibility to GONE at the conclusion of the slide-out animation. Something like:
tv.setVisibility(View.GONE);
Using GONE instead of INVISIBLE will have Android perform the layout operation as if the view didn't exist, and thankfully is the most easily reversible way to do what you're looking for - in other words, you can set it back to VISIBLE at some later point, and the button will return to its original size.
Then you just need to figure out a way to time that method call. One way is to put it inside a Runnable and call Handler.postDelayed(theRunnable, 600);, though personaly I prefer using ViewPropertyAnimators because you can assign them a Runnable to perform at their conclusion (call ViewPropertyAnimator.withEndAction(theRunnable); to do this) and it will figure it out for you.
Done this way, your animation code would look something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
button = (Button) findViewById(R.id.button);
// mSlideOutTop not used
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animateRemoval(v);
in = true;
// Do NOT set visibility to ANYTHING in here
}
});
}
private void animateRemoval(final View toRemove) {
// The method .animate() creates a ViewPropertyAnimator.
toRemove.animate()
.setDuration(600) // Previously defined in slide_out_top.xml
.translationY(1000) // Tweak this number to fit the direction/amplitude, I'm talking a wild guess
.withEndAction(new Runnable() { // Stuff in here runs AFTER the animation.
#Override
public void run() {
// It is critical that this method be executed AFTER the animation, because it
// will cause a layout to occur, and executing layouts during animation is bad news bears.
toRemove.setVisibility(View.GONE);
}
});
}

Changing the position of the thumb of the SeekBar using a different Button

I'm trying to move the position of the seekbar using a button. Basically I have a seekbar from 0 to 100. and I have button presents set up at arbitrary values (40,50,60 etc). When I try to set the progress on the seekbar via button, I get a fault.. I've already initialized the seekBar in the onCreate() method.
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
currentProgress = 40;
seekBar.setMax(100);
seekBar.setProgress(currentProgress);
button40.setOnClickListener(button40Listener);
But when use the below, it crashes.
private OnClickListener button40Listener = new OnClickListener() {
public void onClick(View v) {
currentProgress = 40;
seekBar.setProgress(currentProgress);
}
}
This seems straight-forward. Any ideas?
You shouldn't need to put up another Seekbar. The initial one should be fine. Without the exception message and stack trace I'm not sure what is causing the crash. However, I just coded an example and works as you would expect. Perhaps by looking at my example you can identify your issue.
SeekbarTest.java:
package com.example.seekbartest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class SeekbarTest extends Activity {
/** Called when the activity is first created. */
private SeekBar seekBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seekBar = (SeekBar)findViewById(R.id.seekBar1);
Button fortyPctButton = (Button)findViewById(R.id.buttonFortyPct);
fortyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(40);
}
});
Button sixtyPctButton = (Button)findViewById(R.id.buttonSixtyPct);
sixtyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(60);
}
});
Button eightyPctButton = (Button)findViewById(R.id.buttonEightyPct);
eightyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(80);
}
});
}
}
And here is the main.xml it is referencing for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="#string/hello"
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar1"
android:layout_below="#+id/textView1"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"/>
<Button
android:layout_width="wrap_content"
android:text="40%"
android:id="#+id/buttonFortyPct"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBar1"
android:layout_alignLeft="#+id/seekBar1"/>
<Button
android:layout_width="wrap_content"
android:text="60%"
android:id="#+id/buttonSixtyPct"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/buttonFortyPct"
android:layout_alignTop="#+id/buttonFortyPct"
android:layout_alignBottom="#+id/buttonFortyPct"/>
<Button
android:layout_width="wrap_content"
android:text="80%"
android:id="#+id/buttonEightyPct"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/buttonSixtyPct"
android:layout_alignTop="#+id/buttonSixtyPct"
android:layout_alignBottom="#+id/buttonSixtyPct"/>
</RelativeLayout>
Just create a new android app and replace the generated code + layout with the example above. It should work for you.
Good luck,
Craig

Categories

Resources