I have the following code:
public boolean stopped = false;
public void fadeOut(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(1.00f, 0.00f);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
and this code works fine. My object (an ImageView) fades out beautifully. But when I run this:
public void fadeIn(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(0, 1);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
It doesn't work - there is no fading in. The ImageView stays completely transparent.
Why is this, any ideas?
And for those who are wondering, yes, I have declared and set the boolean stopped, so it's not the issue, because when I look at LogCat, it's printing the text as it runs the fadeIn() function.
Solved!
Turns out, if you use [ImageView Object].setAlpha, and set it to 0 for instance, then when you run AlphaAnimation, it works between the boundaries of 0 and the current alpha of the image.
So, if you want to keep the image invisible after fadeout, the solution is to use [ImageView Object].setVisibility(View.INVISIBLE), and just set it back to View.VISIBLE before you run your animation.
Mission Accomplished.
Is your view's visibility set to View.VISIBLE?
Related
I am curently working on a GUI and when clicking a certain button:
settingsB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Settings");
}
});
Instead of just printing it there should a new window open where you can adjust settings with sliders textboxes, ... How would you approach that? With new classes or all inside the action listener?
Edit:
My "current solution" is based on the great suggestion from user Holger to call a method showSettingsDialogue():
private void showSettingsDialogue() {
if(settingsOpen) {
return;
}
else {
settingsOpen = true;
settingWindow = new SettingsWindow();
settingWindow.addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosed(WindowEvent e) {
settingsOpen = false;
}
#Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
}
Unfortunally with this way I have to write down even unimplemented methods. Any bether solution then this?
I want to make some text appear after an animation like rotating for 2min.
I tried using method like isRunning() in Animator but didn't work.
ObjectAnimator object = ObjectAnimator.ofFloat(ima, "rotation", 1080);
object.setInterpolator(new AccelerateInterpolator());
object.setDuration(1000);
object.setRepeatCount(0);
object.start();
boolean bo = object.isRunning();
while(!bo) {
Random ran = new Random();
int count = ran.nextInt(10);
String str = Integer.toString(count);
text.setText(str);
bo = true ;
}
Take your animation object and add animation listener to it
rotatationAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
**// set your text visisbilty **
}
});
To listen to an ObjectAnimator, just add the following code :
object.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
/*make your text appear*/
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
I hope I could have helped you!
I am using rxjava in eclipse. I cretaed the below posted example.
the issue is,, when i run the code, I receive only
onSubscribe: 0
and I dont receive any output from onNext.
please let me know why I do not receive any output from onNext, and how to fix it
code:
public static void main(String[] args) {
Observable<String> stringObservable = Observable.just("Hello from just Operator1");
stringObservable
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.io())
.subscribe(new Observer<Object>() {
#Override
public void onComplete() {
// TODO Auto-generated method stub
System.out.println("onComplete: OK");
}
#Override
public void onError(Throwable arg0) {
// TODO Auto-generated method stub
}
#Override
public void onNext(Object arg0) {
// TODO Auto-generated method stub
System.out.println("onNext: " + (String) arg0);
}
#Override
public void onSubscribe(Disposable arg0) {
// TODO Auto-generated method stub
System.out.println("onSubscribe: " + arg0);
}
});
}
In my Activity I've a Method which must repeat until the user clicks on a button
private void AnimateItem(int i){
((AnimationDrawable) myList.getChildAt(i).getBackground()).start();
}
private void CheckItems(){
int[] items = new int[]{1,3,5};
for(int i = 0; i<items.length(); i++){
AnimateItem(i);
}
}
So, here I'll have:
AnimateItem(1);
AnimateItem(3);
AnimateItem(5);
How to execute methods one by one (when 1st was ended, start 2nd... and repeating cycle after last method end) until button was clicked.
Instead on AnimateItem(i) method of yours, if your activity implements AnimationListener we can over ride following methods
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (animation == your desired Animation) {
//start your new Animation
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
I made a game and used an ads company on it (StartApp). I wanna use Interstitial Ads on it. I followed the step they show but can't achieve it. I'm getting error. Here is the LogCat:
http://i.stack.imgur.com/SaIXE.png
Here is the codes:
GameActivity.java
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
StartAppAd.init(this, "10254544", "20454573");
}
MenuScene.java
private StartAppAd startAppAd = new StartAppAd(activity);
#Override
public void createScene() {
startAppAd.showAd();
startAppAd.loadAd();
}
Check the Manifest.xml file. Be sure you wrote the right package name in there.
Try This code and u should update manifest file also
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartAppSearch.showSearchBox(this);
StartAppAd.init(this, "107181003", "211487617");
StartAppSearch.init(this, "107181003", "211487617");
btnnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); //load next add
Intent intent=new Intent(getApplicationContext(),Second.class);
startActivity(intent);
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
boolean _active = true;
}
return true;
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
startAppAd.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
startAppAd.onResume();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private StartAppAd startAppAd = new StartAppAd(this);
#Override
public void onBackPressed() {
startAppAd.onBackPressed();
super.onBackPressed();
}
and update in manifest file also
You can not use the
startAppAd.showAd();
startAppAd.loadAd();
inside the Scene class (MenuScene). Instead you can try below method.
In GameActivity class you must be having KeyEvent method. put the code to show the startApp ads in there as below,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if(SceneManager.getInstance().getCurrentSceneType().toString() == "SCENE_MENU"){
try{
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}catch(Exception e){
}
}
try{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}catch(Exception e){
}
}
return false;
}
Regards,
Deepak