Stop view blinking thread - android

I have function that starts thread for blinking view if accepted true, but how do I stop it if false received?
private void blinkText(boolean b){
final Handler handler = new Handler();
if(b)
{
new Thread(new Runnable() {
#Override
public void run() {
int timeToBlink = 1000; //in ms
try{
Thread.sleep(timeToBlink);
}catch (Exception e) {
}
handler.post(new Runnable() {
#Override
public void run() {
TextView txt = (TextView) findViewById(R.id.tv);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blinkText(true);
}
});
}}).start();
}
else
{
// stop blinking
}
}

You can define a blinking animation in xml and add it to your textview after initialising it:
res/anim/blink.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="200"
android:repeatMode="reverse"
android:repeatCount="infinite"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
Add animation to view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
TextView textView = (TextView) this.findViewById(R.id.textview);
Animation blink = AnimationUtils.loadAnimation(this, R.anim.blink);
textView.setAnimation(blink);
}
To start:
textView.getAnimation().start()
To stop:
textView.getAnimation().cancel()
textView.getAnimation().reset()

you need to create a boolean field as class member ( boolean mStopBlink = false; ) and track of whether the TextView is currently blink:
txt.setVisibility(View.VISIBLE);
if(!mStopBlink){
txt.setVisibility(View.INVISIBLE);

Related

How to make text on button appear and disappear continualy

I have a button that I want its text appear for 1 second then disappear for 1 second, loop for 6 seconds, here is what I tried:
for(int i = 0 ; i < 6 ; i++) {
button.setTextSize(18);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
button.setTextSize(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
first is it appear that the button's text size became 36 instead of 18,
second is it is does not act as I expected, never show the text,
note: the text size from start is 0.
How can I achieve this?
okey as you suggested you want to just blink your TextView, here what you can do.
create on folder under res called "anim", now create one xml file under that say
blink.xml and copy this code in it.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
Now in your Java class just apply this animation to your TextView
Animation anim = AnimationUtils.loadAnimation(getActivity(),
R.anim.blink);
yourtextview.startAnimation(anim);
Done.
Instead of blocking thread using Thread.sleep(1000) you can use handler post delayed and after 1 second and make it visible and post another one to make it disappear .
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Make it visible here. */
button.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Make it disappear here. */
button.setVisibility(View.GONE);
}
}, 1000);
}
}, 1000);
This simple code can help you blink your textview:
final String text = yourTextView.getText().toString();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (yourTextView.getText().length() == 0) {
yourTextView.setText(text);
} else {
yourTextView.setText("");
}
}
});
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(timerTask,1000,1000);

Android - show animation atleast a few second

I have Button which function is to animate one of my ImageView for a second and delete specific table, recreate and insert the default value. Everything's working fine but one of my requirements is to show the animation aleast 1 second even recreating table is done. how can i achieve this?
NOTE: I animate my ImageView with Rotate effect so it will looks like refreshing.
refreshDatabase function
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
refreshing.clearAnimation();
}
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:duration="300"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
I try to add this,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//recreate table here
refreshing.clearAnimation();
}
}, 1000);
but i got an error Handler is abstract; cannot be instantiated.
Anybody can help me? Thank You!!!
You can use Handler which will wait for a second and then execute clearAnimation method:
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
refreshing.clearAnimation();
}
}, 1000);
}
Use a Handler (http://developer.android.com/reference/android/os/Handler.html) to post delay a message or a Runnable in which you call clearAnimation()
You can set the duration of the animation like the following example in your rotate.xml file
<rotate android:interpolator="#android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" //set this property (1000 is 1second)
android:startOffset="1500"/>
Or you can do it programatically too by using setDuration() .
ImageView iv_follow_insta = findViewById(R.id.iv_follow_insta);
Animation zoomInanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomin);
Animation zoomOutanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomout);
zoomOutanimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomInanimation);
}
});
zoomInanimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomOutanimation);
}
});
iv_follow_insta.startAnimation(zoomInanimation);
iv_follow_insta.startAnimation(zoomOutanimation);
new Handler().postDelayed(() -> {
zoomInanimation.setAnimationListener(null);
zoomOutanimation.setAnimationListener(null);
iv_follow_insta.clearAnimation();
}, 10000);
try this

How to get the location of ImageView while in animation?

The animation starts when I clicked the ball.
It just moves down then to top then to the middle again.
However, the second onClick in the ball only works if I click in the middle, meaning the ball physically moves but the function's still in the middle.
How do I set its position according to the animation?
(The second click will cancel the animation)
onClick :
public void ballChange(View v)
{
isClicked = false;
if (ballstarted == false)
{
L1_animation = AnimationUtils.loadAnimation(this, R.anim.mercury_anim);
L1_Ball2.startAnimation(L1_animation);
ct = new CountDownTimer(4000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
}
#Override
public void onFinish()
{
if (isClicked == false)
{
lifeline-=1;
if (lifeline == 0)
{
Toast.makeText(getApplicationContext(), "Gameover dude",
Toast.LENGTH_LONG).show();
heart.setText(""+lifeline);
GameOverDialog wp = new GameOverDialog(getApplicationContext());
wp.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
wp.show();
wp.setCancelable(false);
Mercury_L1.L1_Ball.clearAnimation();
Mercury_L1.L1_countDownTimer.cancel();
Mercury_L1.timerHasStarted = false;
Mercury_L1.isCancelled = true;
}
else
{
Toast.makeText(getApplicationContext(), String.valueOf(lifeline), Toast.LENGTH_SHORT).show();
heart.setText(""+lifeline);
minuslife.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
minuslife.setVisibility(View.GONE);
}
}, 500);
Mercury_L1.this.v.vibrate(500);
}
}
}
}.start();
ballstarted = true;
Toast.makeText(getApplicationContext(), String.valueOf(ballstarted), Toast.LENGTH_SHORT).show();
}
else
{
L1_Ball2.clearAnimation();
ballstarted = false;
}
}
You are doing a view animation which only updates the place the ball is drawn and not its actual location. You should use a property animation like objectanimator. Below is some sample code for how to do that with XML.
public void doObjectAnimatorXML(){
AnimatorSet object = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(),R.animator.property_animator);
object.addListener(new AnimatorListenerAdapter(){
#Override
public void onAnimationEnd(Animator animation) {
simpleLock= false;
}
});
object.setInterpolator(new LinearInterpolator());
object.setTarget(mLittleChef); //mLittleChef is the view
object.start();
}
And the XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" >
<objectAnimator
android:propertyName="x"
android:duration="1000"
android:valueTo="138"
android:valueType="floatType"/>
<objectAnimator
android:propertyName="x"
android:duration="1000"
android:valueTo="276"
android:valueType="floatType"/>
</set>

Android Animation within a Fragment not working

I have three scroll views that overlap. For some reason, when I set the other two to View.Gone and the one scroll view I wanted to View.Visible, then start an animation, it doesn't get triggered. These scroll views are within a fragment -- I know some features don't work fully within a fragment. Animation seems pretty basic though.
Here is my button listener's method;
sv2.setVisibility(View.GONE);
sv3.setVisibility(View.GONE);
sv1.setVisibility(View.VISIBLE);
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);
//set your animation
sv1.startAnimation(fadeInAnimation);
also tried to set invisible, load animation, then make it visible;
sv1.setVisibility(View.INVISIBLE);
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);
//set your animation
sv1.startAnimation(fadeInAnimation);
sv1.setVisibility(View.VISIBLE);
And here is my animation xml;
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"
android:repeatCount="infinite"/>
</set>
For use animation in Fragment try below code
This is my layout file
<?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="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_banner"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
This is my fragment java class
public class Fragment_Home extends Fragment {
public int currentimageindex = 0;
Handler mHandler = new Handler();
Runnable mUpdateResults;
//Array of drawable images
private int[] IMAGE_IDS = {
R.drawable.home_slider_stemer, R.drawable.home_slider_plane
};
//image view
private ImageView iv_banner;
private View rootView;
public Fragment_Home() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_home, container, false);
LoadUIElements();
return rootView;
}
private void LoadUIElements() {
iv_banner = (ImageView) rootView.findViewById(R.id.iv_banner);
int delay = 1000; // delay for 1 sec.
int period = 2000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
mHandler.post(mUpdateResults);
}
}, delay, period);
mUpdateResults = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
AnimateandSlideShow();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
/**
* Helper method to start the animation on the splash screen
*/
protected void AnimateandSlideShow() {
// TODO Auto-generated method stub
try {
iv_banner.setImageResource(IMAGE_IDS[currentimageindex
% IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(getActivity()
.getBaseContext().getApplicationContext(), R.anim.fade_in);
iv_banner.startAnimation(rotateimage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Don't forget to put images in your Drawable folder in res.
I got around the problem by setting an animation listener and managing all the visibility stuff inside there.
sv1.setVisibility(View.INVISIBLE);
//grab animation from anim folder in res/
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_anim);
fadeInAnimation.setAnimationListener(new AnimationListener() {
//set other scroll views to invisible once done
public void onAnimationEnd(Animation animation) {
sv2.setVisibility(View.INVISIBLE);
sv3.setVisibility(View.INVISIBLE);
}
public void onAnimationRepeat(Animation animation) {
}
//once our animation starts, we set our view to visible
public void onAnimationStart(Animation animation) {
sv1.setVisibility(View.VISIBLE);
}
});
scrollViewAnimationActive = true;
//start our animations for views that need to be removed.
//We know one of these views were showing by checking if it was "visible".
if (sv2.getVisibility() == View.VISIBLE)
sv2.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
else if (sv3.getVisibility() == View.VISIBLE) {
sv3.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
}else if (wikiParentLL.getChildCount() > 1) {
wikiParentLL.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
}
//finally, start our "animation"
sv1.startAnimation(fadeInAnimation);
Hope this helps.
I had exactly problem, I solved it by using onWindoFocusChangeListener and Handler.
mview.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
#Override
public void onWindowFocusChanged(final boolean hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startSeedAnimation();
}
}, 600);
}
});
Where you can get the view object in onViewCreated or simply call view? / getView() from everywhere.

Android Transition Images (TransitionDrawable) for Splash Screen

I'm a little new to Android, but pretty fluent in VB.net. I have two questions regarding Splash Screens:
I am trying to create a Splash Screen that launches on Application start. I can do it with Frame-Animations but would like to use the TransitionDrawable class because of the effect it has (fadeIn) that I would like to use. I used the same code for the Frame-Animation, after changing the definitions, but can't get it to work. What am I doing wrong?
This logo I am loading consists of 16 images. How can I use the TransitionDrawable class to go from logo1 to logo2 to logo3... to logo16? I tried using a loop and the array of "imageIds" to create my own Frame Animation, but can't it to work for the Transition. Help would be greatly appreciated.
Here is my code:
public class SplashScreenActivity extends Activity {
TransitionDrawable animation;
ImageView transImage;
Integer[] imageIds = { R.drawable.logo1, R.drawable.logo2,
R.drawable.logo3, R.drawable.logo4, R.drawable.logo5,
R.drawable.logo6, R.drawable.logo7, R.drawable.logo8,
R.drawable.logo9, R.drawable.logo10, R.drawable.logo11,
R.drawable.logo12, R.drawable.logo13, R.drawable.logo14,
R.drawable.logo15, R.drawable.logo16 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
transImage = (ImageView) findViewById(R.id.splashImageView);
animation = (TransitionDrawable) getResources().getDrawable(R.anim.transition_list);
transImage.setBackgroundDrawable(animation);
transImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
finish();
startActivity(new Intent("com.V1.V1LogoSplash.V1LogoMainActivity"));
}
return false;
}; // END ONTOUCH
}); // END ONLISTSENER
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
animation.startTransition(3000);
finish();
}
}
you try this type code
public class SplashActivity extends Activity {
Thread mSplashThread;
private int SPLASH_DISPLAY_LENGTH = 3800;
ImageView image;
AnimationDrawable frameAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashactivity);
image = (ImageView) findViewById(R.id.splashImg);
image.setBackgroundResource(R.drawable.splash_animation);
frameAnimation = (AnimationDrawable) image.getBackground();
Thread splashTread = new Thread() {
public void run() {
try {
sleep(SPLASH_DISPLAY_LENGTH);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent;
intent = new Intent(SplashActivity.this,
ProductListActivity.class);
startActivity(intent);
finish();
}
}
};
splashTread.start();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
frameAnimation.start();
}
}
and other one
drawable in xml (your img put in xml)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="#drawable/survey11"
android:duration="500"/>
<item
android:drawable="#drawable/survey6"
android:duration="300"/>
<item
android:drawable="#drawable/survey5"
android:duration="300"/>
<item
android:drawable="#drawable/survey3"
android:duration="300"/>
<item
android:drawable="#drawable/survey2"
android:duration="300"/>
<item
android:drawable="#drawable/survey1"
android:duration="800"/>
</animation-list>

Categories

Resources