In my code, I have implemented the sliding layouts. I have a front page which runs a video, while the user interaction is being done with layouts sliding up and down. Here's the code:
package org.udoo.androidadkdemobidirect;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.VideoView;
import java.io.DataOutputStream;
import java.io.IOException;
//import org.udoo.androidadkdemobidirect.sAdkManager;
public class MainActivity extends Activity{
// private static final String TAG = "UDOO_AndroidADKFULL";
private static String mAdkManager=null;
private VideoView myVideoView;
private MediaController mediaControls=null;
private int position = 0;
private String ppm_val=null;
private ToggleButton buttonLED;
private TextView distance;
private Button res_nextBtn;
private TextView res_tv;
private AdkReadTask mAdkReadTask;
private int myInt = -10;
private TextView tv;
private FrameLayout current=null;
private Animation slide_down;
private Animation slide_up;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
//hideNavigationBar();
buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLed);
distance = (TextView) findViewById(R.id.textViewIntro);
buttonLED.setVisibility(View.INVISIBLE);
tv = (TextView) findViewById(R.id.ppm_unique);
if (mediaControls == null) {
mediaControls = new MediaController(this);
}
slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
res_nextBtn = (Button) findViewById(R.id.result_next_btn);
res_tv = (TextView) findViewById(R.id.result_number_text);
myVideoView = (VideoView) findViewById(R.id.videoView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
myVideoView.getHolder().setFixedSize(size.x, size.y);
tv.setText(""+size.x+":"+size.y);
final Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(000);
final Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(500);
//animation1 AnimationListener
animation1.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
// start animation2 when animation1 ends (continue)
distance.startAnimation(animation2);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
//animation2 AnimationListener
animation2.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
// start animation1 when animation2 ends (repeat)
distance.startAnimation(animation1);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
distance.startAnimation(animation1);
try {
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound_758));
} catch (Exception e) {
}
// myVideoView.requestFocus();
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
//progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
mediaPlayer.setLooping(true);
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
//if we come from a resumed activity, video playback will be paused
myVideoView.pause();
}
}
});
((MyApplication) this.getApplication()).init(this);
((MyApplication) this.getApplication()).open();
if (mAdkReadTask==null) {
mAdkReadTask = new AdkReadTask();
mAdkReadTask.execute();
}
}
/************************************************************************************************
* *
* OnClickEvents *
* *
************************************************************************************************/
public void BR_onBuyClick(View view) {
next((FrameLayout) findViewById(R.id.flavours_inc_layout));
}
public void Flavours_onBackClick(View v){
next((FrameLayout) findViewById(R.id.br_inc_layout));
}
public void Flavours_onF1Click(View view) {
next((FrameLayout) findViewById(R.id.ty_inc_layout));
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
home();
}
},
5000);
}
public void Flavours_onF2Click(View view) {
next((FrameLayout) findViewById(R.id.ty_inc_layout));
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
home();
}
},
5000);
}
public void Flavours_onF3Click(View view) {
next((FrameLayout) findViewById(R.id.ty_inc_layout));
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
home();
}
},
5000);
}
private void home() {
current.startAnimation(slide_down);
current.setVisibility(View.INVISIBLE);
current=null;
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
distance.startAnimation(slide_up);
distance.setVisibility(View.VISIBLE);
final Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(000);
final Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(500);
//animation1 AnimationListener
animation1.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
// start animation2 when animation1 ends (continue)
distance.startAnimation(animation2);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
//animation2 AnimationListener
animation2.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
// start animation1 when animation2 ends (repeat)
distance.startAnimation(animation1);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
distance.startAnimation(animation1);
((MyApplication) getApplication()).write("1");
}
},
1000);
}
public void BR_onRecycleClick(View view) {
next((FrameLayout) findViewById(R.id.ready_inc_layout));
}
public void proc_onDoneClick(View v){
next((FrameLayout) findViewById(R.id.delay_inc_layout));
onDelay();
}
public void proc_onCancelClick(View v){
next((FrameLayout) findViewById(R.id.br_inc_layout));
}
private void onDelay() {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.delay_progressBar);
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
ValueAnimator animator = new ValueAnimator();
animator.setObjectValues(0, 100);
animator.setDuration(7000);
final TextView tv = (TextView) findViewById(R.id.delay_textPoints);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
tv.setText("" + animation.getAnimatedValue() +"%");
}
});
animator.addListener(new AnimatorListenerAdapter(){
#Override
public void onAnimationEnd(Animator animator) {
progressBar.setVisibility(View.INVISIBLE);
((MyApplication) getApplication()).write("4");
next((FrameLayout) findViewById(R.id.result_inc_layout));
((MyApplication) getApplication()).write("8");
res_nextBtn.setVisibility(View.INVISIBLE);
}
});
animator.start();
}
public void Res_onNextClick(View v){
next((FrameLayout) findViewById(R.id.points_inc_layout));
onPoints();
}
private void onPoints() {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.points_progressBar);
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
final ValueAnimator animator = new ValueAnimator();
if (myInt>=0) {
animator.setObjectValues(0, (myInt) * 5);
}
else{
animator.setObjectValues(0, 1);
}
animator.setDuration(2000);
final TextView tv = (TextView) findViewById(R.id.points_textPoints);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
tv.setText("" + animation.getAnimatedValue());
}
});
animator.addListener(new AnimatorListenerAdapter(){
#Override
public void onAnimationEnd(Animator animator) {
progressBar.setVisibility(View.INVISIBLE);
}
});
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
animator.start();
}
},
1500);
}
public void pts_onConfirmClick(View v){
next((FrameLayout) findViewById(R.id.end_inc_layout));
((MyApplication) getApplication()).write("3");
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
home();
}
},
5000);
}
public void Ready_onYesClick(View v){
((MyApplication) this.getApplication()).write("2");
next((FrameLayout) findViewById(R.id.processing_inc_layout));
onProcessing();
}
private void onProcessing() {
final int[] i = {0};
final int[] drawablearray = new int[]{R.drawable.processing_butts_3, R.drawable.processing_butts_2, R.drawable.processing_butts_1, R.drawable.processing_butts_0};
final ImageView backgroundImageView = (ImageView) findViewById(R.id.processing_textButts);
backgroundImageView.postDelayed(new Runnable() {
public void run() {
backgroundImageView.setImageResource(drawablearray[i[0]++ % drawablearray.length]);
backgroundImageView.postDelayed(this, 1000);
}
}, 1500);
}
public void Ready_onCancelClick(View v){
next((FrameLayout) findViewById(R.id.br_inc_layout));
}
#Override
public void onResume() {
super.onResume();
((MyApplication) this.getApplication()).write("1");
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void hideNavigationBar() {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
/*
enable = enable navigation bar
disable = disable navigation bar
*/
os.writeBytes("pm enable com.android.systemui\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
//////////////////////////////////////
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String output="";
/*
* We put the readSerial() method in an AsyncTask to run the
* continuous read task out of the UI main thread
*/
private class AdkReadTask extends AsyncTask<Void, String, String> {
private boolean running = true;
public void pause(){
running = false;
}
public void start(){
running = true;
}
#Override
protected String doInBackground(Void... params) {
while(running) {
if (this.isCancelled())
break;
String s = ((MyApplication) getApplication()).read();
publishProgress(s);
}
return output;
}
#Override
protected void onProgressUpdate(String... progress) {
if(progress[0].charAt(0)=='P') {
/*
TODO: change text+ppm_val to image+text
TODO: send ppm values to server
*/
tv.setText("Air quality: " + progress[0].substring(1, progress[0].length()));
ppm_val = progress[0].substring(1, progress[0].length());
((MyApplication) getApplication()).setPPM(ppm_val);
}
else if (progress[0].charAt(0)=='R'){
/*
TODO: add server verification of RFID
*/
distance.startAnimation(slide_down);
distance.setVisibility(View.GONE);
next((FrameLayout) findViewById(R.id.br_inc_layout));
}
else if (progress[0].charAt(0)=='C'){
/*
TODO: send data to server
*/
res_tv.setText(""+(int) progress[0].charAt(1));
myInt = (int) progress[0].charAt(1);
res_nextBtn.setVisibility(View.VISIBLE);
}
}
#Override
protected void onPostExecute(String result){
}
}
private void next(final FrameLayout fl) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fl.getLayoutParams();
if (fl == (FrameLayout) findViewById(R.id.flavours_inc_layout)){
params.height = (int) (size.y*0.6);
}
else{
params.height = (int) (size.y*0.3);
}
params.width = size.x;
int delay_delta = 1000;
// Set it back.
fl.setLayoutParams(params);
if(current!=null){
current.startAnimation(slide_down);
current.setVisibility(View.INVISIBLE);
delay_delta=0;
}
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
fl.startAnimation(slide_up);
fl.setVisibility(View.VISIBLE);
current = fl;
}
},
1200-delay_delta);
}
}
And everything worked fine before I tried changing the slide-up height. After I set up a special case for a layout 0.6*y instead of 0.3*y. The layout is being not displayed above 0.3 mark. It does function, and when I click the supposed button place it displays just before sliding down.
Ok, it was solved by accident. I changed all View.INVISIBLE to View.GONE and it works like swiss watch. I guess there's some black magic involved and I would really appreciate (and accept the answer of course) if someone could explain to me why it works. Thanks.
Related
I have a custom pager that uses two animations
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
For some reason, the animations are not starting up. For example, I'm logging
Log.v("TAG", "L1 start animation"); //prints "L1 start animation"
But I'm not logging anything in onAnimationEnd
Log.v("TAG", "L1 animation end"); //nothing is printing out
Here is setAnimation() method of CustomPager class
public void setAnimation(){
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
l1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L1 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.v("TAG", "L1 animation start");
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("TAG", "L1 animation end");
l1.setVisibility(View.GONE);
l2.bringToFront();
clicked = false;
}
});
Log.v("TAG", "L1 start animation");
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
}
});
//etc
}
Here is CustomPager class
public class CustomPager extends PagerAdapter {
private Context mContext;
private View l1;
private View l2;
private LayoutInflater inflater;
private ViewGroup layout;
private Bitmap pic;
private Bitmap pic_blurred;
private boolean clicked = false;
public CustomPager(Context context) {
this.mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
inflater = LayoutInflater.from(mContext);
layout = (ViewGroup) inflater.inflate(R.layout.pager_play_panel, collection, false);
collection.addView(layout);
l1 = layout.findViewById(R.id.l1);
l2 = layout.findViewById(R.id.l2);
pic = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pic);
picture_blurred = blur(pic, 1, 100);
BitmapDrawable bkg1 = new BitmapDrawable(pic);
l1.setBackgroundDrawable(bkg1);
BitmapDrawable bkg2 = new BitmapDrawable(picture_blurred);
l2.setBackgroundDrawable(bkg2);
setAnimation();
return layout;
}
public void setAnimation(){
final Animation fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
l1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L1 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.v("TAG", "L1 animation start");
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("TAG", "L1 animation end");
l1.setVisibility(View.GONE);
l2.bringToFront();
clicked = false;
}
});
Log.v("TAG", "L1 start animation");
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
}
});
l2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("TAG", "L2 clicked");
if (clicked) {
//do nothing
} else {
clicked = true;
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
l2.setVisibility(View.GONE);
l1.bringToFront();
Log.v("TAG", "L2 visibility set to gone");
clicked = false;
}
});
l2.startAnimation(fadeOut);
l1.setVisibility(View.VISIBLE);
l1.startAnimation(fadeIn);
}
}
});
}
//Etc.
}
Looks like the problem was I had to change the scope of my view variables
public void setAnimation(View l1, View l2){
//etc
}
Rather than using them globally
public void setAnimation(){
//etc
}
I created one animation in which I used one radio button as object and a matrix background. when the activity starts the object will move along the each cells in the matrix. now I want the object to be stopped when i click on it. how can i do this??
public class MainActivity extends Activity {
RadioButton rad;
Animation animation1;
Context context = this;
Button move;
Handler handler = new Handler();
Runnable handlerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridView1);
rad = (RadioButton) findViewById(R.id.radioButton1);
// move=(Button)findViewById(R.id.button1);
gridview.setAdapter(new ImageAdapter(this));
animation1 = AnimationUtils.loadAnimation(this, R.anim.animation);
// userInputHandler();
/*
* move.setOnClickListener(new View.OnClickListener() {
*
* #Override public void onClick(View arg0) { // TODO Auto-generated
* method stub
*/
handlerTask = new Runnable() {
public void run() {
if (rad.isChecked()) {
animation1.cancel();
} else {
rad.startAnimation(animation1);
}
}
};
handlerTask.run();
// }
// });
}
public void onAnimationEnd(Animation animation) {
rad.setVisibility(View.VISIBLE);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
}
I have translate animation for items on my layout. When first item have animation end start second item and others. It dont work on Sony Ericsson Xperia Arc S LT18i and i dont know why.
This is my code
public class LoginActivity extends FragmentActivity {
private static final String TAG = LoginActivity.class.getSimpleName();
private EditText mUsername;
private EditText mPassword;
private CheckBox mSaveUserNameCheckbox;
private View contentHolder;
private ImageView logoImage;
private LinearLayout logLay, passLay, rememberLay, buttonLay;
private String key_link = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Uri data = getIntent().getData();
Log.i("URI issue", "uri="+data);
if (data != null) {
String scheme = data.getScheme();
List<String> params = data.getPathSegments();
if (params.get(0) != null && params.get(1) != null) {
Log.i("PARSING", "data=" + data + " shame=" + scheme + " host="
+ data.getHost() + " ::: param 0=" + params.get(0)
+ " param 1=" + params.get(1));
if (scheme.equalsIgnoreCase("https")) {
String host = data.getHost();
if (host.equalsIgnoreCase("jira.hiqo-solutions.us")) {
if (params.get(0).equalsIgnoreCase("browse"))
key_link = params.get(1);
} else {
Toast.makeText(this, "Incorrect link!",
Toast.LENGTH_LONG).show();
}
}
}
}
contentHolder = findViewById(R.id.content_holder);
logoImage = (ImageView) findViewById(R.id.imageLogo);
logoImage.setVisibility(View.GONE);
logLay = (LinearLayout) findViewById(R.id.usernameLay);
logLay.setVisibility(View.GONE);
passLay = (LinearLayout) findViewById(R.id.passLay);
passLay.setVisibility(View.GONE);
rememberLay = (LinearLayout) findViewById(R.id.rememberLay);
rememberLay.setVisibility(View.GONE);
buttonLay = (LinearLayout) findViewById(R.id.buttonLay);
buttonLay.setVisibility(View.GONE);
contentHolder.setVisibility(View.GONE);
if (CredentialsPrefs.mRememberMe.get() && checkConnection()) {
setMode();
doLogin(CredentialsPrefs.mLogin.get(),
CredentialsPrefs.mPassword.get());
} else {
if (!checkConnection()) {
Toast.makeText(this, "No Internet Connections!",
Toast.LENGTH_LONG).show();
}
contentHolder.setVisibility(View.VISIBLE);
CredentialsPrefs.mLogined.put(false);
final Animation logoAnim = AnimationUtils.loadAnimation(this,
R.anim.logo_animation);
final Animation loginAnim = AnimationUtils.loadAnimation(this,
R.anim.items_animation);
final Animation passAnim = AnimationUtils.loadAnimation(this,
R.anim.items_animation);
final Animation rememberAnim = AnimationUtils.loadAnimation(this,
R.anim.items_animation);
final Animation buttonAnim = AnimationUtils.loadAnimation(this,
R.anim.items_animation);
loginAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
passLay.setVisibility(View.VISIBLE);
passLay.startAnimation(passAnim);
}
});
passAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
rememberLay.setVisibility(View.VISIBLE);
rememberLay.startAnimation(rememberAnim);
}
});
rememberAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
buttonLay.setVisibility(View.VISIBLE);
buttonLay.startAnimation(buttonAnim);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
buttonAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
initializateControls();
}
});
logoAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
logLay.setVisibility(View.VISIBLE);
logLay.startAnimation(loginAnim);
}
});
logoImage.setVisibility(View.VISIBLE);
logoImage.startAnimation(logoAnim);
}
What it can be?
I would like to create a "Flash" class to display a white flash screen (200ms) as easily as a toast, like this:
Flash.create(context).show();
This layer should appear instantly then gradually disappear (alpha transition)
It must not catch events
It must adapt to the screen rotation
It can be created from any activity
I looked for a solution via WindowManager but I am having some difficulties.
What solution do you recommend?
There are some issues with that, as you'll have to pass the Activity and not just a Context into the "Flash" screen. Therefore the handling of screen rotation will be tricky.
I've created and open-sourced a small library called Crouton that can help you.
Currently it still struggles with the issue of orientation changes while a Crouton is being displayed.
So I give my own solution if it can help anybody...
(I changed the call to flash method since my first post)
public class Flasher {
private final static int DURATION_OFFSET = 300;
private final static int DURATION_FADEIN = 50;
private final static int DURATION_FADEOUT = 200;
private Animation fadein;
private Animation fadeout;
private RelativeLayout flash;
private RelativeLayout view;
private Toast toast;
private int count;
public Flasher(Context context) {
fadein = new AlphaAnimation(0, 1);
fadein.setStartOffset(DURATION_OFFSET);
fadein.setDuration(DURATION_FADEIN);
fadein.setAnimationListener(new AnimationListener() {
#Override public void onAnimationStart(Animation anim) {}
#Override public void onAnimationRepeat(Animation anim) {}
#Override public void onAnimationEnd(Animation anim) {
flash.startAnimation(fadeout);
}
});
fadeout = new AlphaAnimation(1, 0);
fadeout.setDuration(DURATION_FADEOUT);
fadeout.setAnimationListener(new AnimationListener() {
#Override public void onAnimationStart(Animation anim) {}
#Override public void onAnimationRepeat(Animation anim) {}
#Override public void onAnimationEnd(Animation anim) {
if(count > 1) {
flash(count - 1);
} else {
cancel();
}
}
});
LayoutParams params = new LayoutParams(-1, -1);
flash = new RelativeLayout(context);
flash.setLayoutParams(params);
flash.setBackgroundColor(0xffffffff);
flash.setVisibility(View.INVISIBLE);
view = new RelativeLayout(context);
view.setLayoutParams(params);
view.addView(flash);
toast = new Toast(context);
toast.setView(view);
toast.setGravity(Gravity.FILL, 0, 0);
}
public final void flash(int count) {
toast.show();
this.count = count;
flash.startAnimation(fadein);
}
public final void cancel() {
toast.cancel();
}
}
MainActivity for test it:
public class MainActivity extends Activity {
private static Flasher flasher;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
View layout = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(layout);
if(flasher == null) {
flasher = new Flasher(this);
}
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
flasher.flash(1);
}
});
}
#Override protected void onDestroy() {
if(isFinishing()) {
flasher = null;
}
super.onDestroy();
}
}
Thank you to Keyboardsurfer for the help!
Edit: a constraint is not satisfied:
The Toast does not dispatch the touch event:
flasher.flash(10);
MainActivity is not touchable during flashes effect
If anybody have a solution to resolve the problem...
After searching for many hours, it seems impossible to delegate events from one window to another. So the final functional solution I've found is to extend an activity so that it achieves this flash effect. For those who are interested in it, here's my subclass:
public class FlmActivity extends Activity {
private final static int DURATION_FADEIN = 50;
private final static int DURATION_FADEOUT = 200;
private final static int DURATION_INTERVAL = 200;
private View view;
private AnimationSet anim;
private int count;
public FlmActivity() {
super();
}
#Override protected void onPostCreate(Bundle state) {
super.onPostCreate(state);
view = new View(this) {{
setBackgroundColor(0xffffffff);
setVisibility(View.INVISIBLE);
}};
anim = new AnimationSet(false) {{
addAnimation(new AlphaAnimation(0, 1) {{
setDuration(DURATION_FADEIN);
}});
addAnimation(new AlphaAnimation(1, 0) {{
setStartOffset(DURATION_FADEIN);
setDuration(DURATION_FADEOUT);
}});
addAnimation(new Animation() {{
setStartOffset(DURATION_FADEIN + DURATION_FADEOUT);
setDuration(DURATION_INTERVAL);
setAnimationListener(new AnimationListener() {
#Override public void onAnimationStart(Animation anim) {}
#Override public void onAnimationRepeat(Animation anim) {}
#Override public void onAnimationEnd(Animation anim) {
flash(count - 1);
}
});
}});
}};
addContentView(view, getWindow().getAttributes());
}
#Override protected void onDestroy() {
((ViewGroup) view.getParent()).removeView(view);
super.onDestroy();
}
public final void flash(int count) {
if((this.count = count) > 0) {
view.startAnimation(anim);
}
}
}
Code to call flash effect in an extended activity:
flash(10);
With this implementation, the events pass through the layer
I would like to implement a SplashScreen in my app. I found the best and easiest way is to launch an activity that shows a layout with an image view at the launch of the app and then adding android:noHistory="true" attribute to the manifest.
Now, how do I set the splashscreen activity to launch the MainActivity class after a certain amount of time? Lets say 2 seconds?
This is my splashscreen activity
public class SplashActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
}
use
handler.postDelayed(runnable, delayinmilliseconds(2000 in your case));
final Runnable runnable = new Runnable()
{
public void run()
{
//start the new activity here.
}
};
Here is a complete sample.
package com.test.splash;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
public class splash extends Activity {
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 3000;a
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* #see android.os.Handler#handleMessage(android.os.Message)
*/
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splashscreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
public class TrackMyMoneyActivity extends Activity {
//member fields
private ProgressBar pbar = null;
private TextView counter_txt = null;
Thread splash_thread = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar = (ProgressBar) findViewById(R.id.splashpbar);
counter_txt = (TextView) findViewById(R.id.countertxt);
//define thread
splash_thread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
for( i=0;i<100;i++){
pbar.setProgress(i);
// counter_txt.setText(i+" %");
try {
splash_thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i==100){
Intent intent = new Intent(getBaseContext(), LoginApp.class);
startActivity(intent);
}
}
});
splash_thread.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
I hope it will be solved your solution.
You can also use java.util.Timer in this way:
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
}, 2000 /*amount of time in milliseconds before execution*/ );
public class Splashscreen extends Activity
{
private static final int SPLASH_TIME = 10 * 1000;// 3 seconds
Button logo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splashscreen.this,MainActivity.class);
startActivity(intent);
Splashscreen.this.finish();
//overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, SPLASH_TIME);
} catch(Exception e){}
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(50*1000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}