How to get the location of ImageView while in animation? - android

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>

Related

How to scroll recyclerView programmatically?

I have a horizontal recyclerView, When I first open the activity I want to make all the items in recyclerview scroll to the bottom (to the right in this case) and back to the top (to the left). Kinda like an animation. Scroll behavior should be visible to the user.
I tried to do it like:
Animation slideRight = AnimationUtils.loadAnimation(this, R.anim.slide_right);
Animation slideLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left);
slideRight.setDuration(1000);
slideLeft.setDuration(1000);
slideRight.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
recyclerView.startAnimation(slideLeft);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
recyclerView.startAnimation(slideRight);
anim slide left:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="200"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
anim slide right:
<translate
android:duration="200"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
it works however it just slides the recyclerview as a whole, I just want to scroll(slide) the items. How can I do this?
You can use scrollToPosition()
recyclerView.post(new Runnable() {
#Override
public void run() {
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
// Here adapter.getItemCount()== child count
}
});
or smoothScrollToPosition()
recyclerView.post(new Runnable() {
#Override
public void run() {
recyclerView.smoothScrollToPosition(adapter.getItemCount()- 1);
}
});
To move up again, you need to call the above method with index 0. But first, you need to make sure that the RecyclerView is scrolled to last.
So, put a ScrollListener on RecyclerView to make sure the last item is visible.
Use this..
int top = 0;
recyclerView.smoothScrollToPosition(top); // for top
int bottom = recyclerView.getAdapter().getItemCount()-1;
recyclerView.smoothScrollToPosition(bottom);
if you need scroll item of recyclerview to CENTER of screen when for ex. expand your item - use this way:
in app/build.gradle - insert:
implementation 'com.andkulikov:transitionseverywhere:1.7.4'
when hadle click in onBindViewHolder of adapter - insert:
AutoTransition transitionPort = new AutoTransition();
transitionPort.setDuration(100);
transitionPort.addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(#NonNull Transition transition) {
recyclerView.setEnabled(false);
recyclerView.setClickable(false);
}
#Override
public void onTransitionEnd(#NonNull Transition transition) {
recyclerView.post(() -> recyclerView.smoothScrollToPosition(position));
recyclerView.setEnabled(true);
recyclerView.setClickable(true);
}
#Override
public void onTransitionCancel(#NonNull Transition transition) {
recyclerView.setEnabled(true);
recyclerView.setClickable(true);
}
#Override
public void onTransitionPause(#NonNull Transition transition) {
}
#Override
public void onTransitionResume(#NonNull Transition transition) {
}
});
TransitionManager.beginDelayedTransition(recyclerView, transitionPort);
Not working after trying other things?
This excellent piece of code worked for me
private void scrollToBottom(final RecyclerView recyclerView) {
// scroll to last item to get the view of last item
final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
final int lastItemPosition = adapter.getItemCount() - 1;
layoutManager.scrollToPositionWithOffset(lastItemPosition, 0);
recyclerView.post(new Runnable() {
#Override
public void run() {
// then scroll to specific offset
View target = layoutManager.findViewByPosition(lastItemPosition);
if (target != null) {
int offset = recyclerView.getMeasuredHeight() - target.getMeasuredHeight();
layoutManager.scrollToPositionWithOffset(lastItemPosition, offset);
}
}
});
}
You can modifyline, if you want to jump to some other listitem & not last
final int lastItemPosition = adapter.getItemCount() - 1;
Source

Slide up animation for selected listview items

I have a listview. I want to add one animation that is if I select one list item then it will be removed and rest of the items below that item will be shifted up with slide up animation. I have done this with linear layout by getting its child position but I am not able to understand how to slide up rest elements of listview. Please help
Below is my code for animation in dynamically created linear layout
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation animation1 = AnimationUtils.loadAnimation(getActivity(), R.anim.move);
final Animation animation2 = AnimationUtils.loadAnimation(getActivity(), R.anim.moveup);
this is animation of that item on which we click
lay1.startAnimation(animation1);
this is code for animation rest child to slide up
final int i = lay1.getId() + 1;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
try {
LinearLayout l = (LinearLayout) layoutall.getChildAt(i);
l.startAnimation(animation2);
layoutall.removeView(lay1);
} catch (Exception e) {
//connectToDatabase();
}
}
}, 600);
Updated Code
Thanks a lot. I was able to implement this functionality but problem is that I have used two animation in one listview but not able to implement first animation.
My code for list item to whom I want to swipe left while on click
viewHolder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeListItem(viewHolder.order_card_layout, position);
}
});
And code for animation is
protected void removeListItem(final View rowView, final int positon) {
// TODO Auto-generated method stub
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
Animation.AnimationListener al = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
ViewHolder vh = (ViewHolder) rowView.getTag();
//vh.needInflate = true;
}
#Override public void onAnimationRepeat(Animation animation) {}
#Override public void onAnimationStart(Animation animation) {}
};
collapse(rowView, al);
}
but problem is that My first animation is not working that is slide right
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
New Updated Code
I was able to implement this functionality but problem is that I have used two animations and want to do first animation on that item whom I click and another animation on all the list items below that clicked item.But my problem is that the item on which I clicked slide right and also it re appears and slide up with whole list but I want that the item on which i click will be slide right and rest will be slide up.
My code for list item to whom I want to swipe left while on click
viewHolder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeListItem(viewHolder.order_card_layout, position);
}
});
And code for animation is
protected void removeListItem(final View rowView, final int positon) {
// TODO Auto-generated method stub
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
try {
Animation.AnimationListener al = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
ViewHolder vh = (ViewHolder) rowView.getTag();
//vh.needInflate = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
};
collapse(rowView, al);
} catch (Exception e) {
//connectToDatabase();
}
}
}, 600);
}
private void collapse(final View v, Animation.AnimationListener al) {
final int initialHeight = v.getMeasuredHeight();
Animation anim = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
}
else {
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
if (al!=null) {
anim.setAnimationListener(al);
}
anim.setDuration(600);
v.startAnimation(anim);
}
Here is code related to your requirement , Try this
https://github.com/paraches/ListViewCellDeleteAnimation
Download code and run it.
if you want to see demo than see this video,
http://www.youtube.com/watch?v=bOl5MIti7n0
Here is Code, where i am implementing both animation.
1) Slide from right while click on List-view Item-click.
2) while clicking on Delete Button, slide-up animation for remaining rows in list-view (which is already done in link i have given).
Copy below files to your anim folder.
1) fade_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:duration="700"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
2) fade_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:duration="700"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
3) slide_in_right.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="700"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
4) slide_out_right.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="700"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
Now add below methods and class to your activity,
//Animation
private void fadeOutView(View view) {
Animation fadeOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_out);
if (fadeOut != null) {
fadeOut.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(fadeOut);
}
}
private void fadeInView(View view) {
Animation fadeIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in);
if (fadeIn != null) {
fadeIn.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(fadeIn);
}
}
private void slideInView(View view) {
Animation slideIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_right);
if (slideIn != null) {
slideIn.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(slideIn);
}
}
private void slideOutView(View view) {
Animation slideOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_out_right);
if (slideOut != null) {
slideOut.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(slideOut);
}
}
private abstract class ViewAnimationListener implements Animation.AnimationListener {
private final View view;
protected ViewAnimationListener(View view) {
this.view = view;
}
#Override
public void onAnimationStart(Animation animation) {
onAnimationStart(this.view, animation);
}
#Override
public void onAnimationEnd(Animation animation) {
onAnimationEnd(this.view, animation);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
protected abstract void onAnimationStart(View view, Animation animation);
protected abstract void onAnimationEnd(View view, Animation animation);
}
Now, if you want to display animation on List Item Click then, give animation in Adapter class in which you have inflate raw_layout for listView.
5) raw_layout.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linLayout">
<ImageButton
android:id="#+id/cell_trash_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/trash_can"
android:scaleType="fitXY" />
<TextView
android:id="#+id/cell_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="cell name" />
</LinearLayout>
6) finally Apply animation to your Parent Layout of raw_layout in getView() method of adapter onClick of Parent Layout.Here My parent layout for raw_layout is LinearLayout which have id named linLayout.
vh.linLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (vh.linLayout.getVisibility() == View.VISIBLE) {
fadeOutView(vh.linLayout);
slideInView(vh.linLayout);
} else {
fadeInView(vh.linLayout);
slideOutView(vh.linLayout);
}
}
});
i have tried this and its working. So try this once!!

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

Stop view blinking thread

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);

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.

Categories

Resources