run animation for buttons one after an other - android

I use animation to display my buttons, i have 9 buttons so i used duration for display, but it is run animation for all buttons at same time and just with diffrent duration, of course it is correct, but i want to run animation for button1 first and when it is done, do it again for button2 and when it is done too, do it for next buttons....
I mean run same animation with same speed for different buttons one after another,not same time. now my apk is this:
https://drive.google.com/file/d/0B9Q0pN8FVwEORGRQVE1kQmtvS28/view?usp=sharing
it must be like this:
http://up.vbiran.ir/uploads/4517142605716735676_animationn.gif
my full code:
public class MainActivity extends ActionBarActivity {
public MediaPlayer player;
public MediaPlayer playerp;
private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
new ButtonSupport(R.id.Button06, 1000l, YourClassActivity.class),
new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
new ButtonSupport(R.id.Button08, 5000l,YourClassActivity4.class),
new ButtonSupport(R.id.Button04, 6000l,YourClassActivity5.class),
new ButtonSupport(R.id.Button01, 7000l,YourClassActivity6.class),
new ButtonSupport(R.id.Button07, 9000l,YourClassActivity7.class),
new ButtonSupport(R.id.Button05, 10000l,YourClassActivity8.class),
new ButtonSupport(R.id.Button02, 11000l,YourClassActivity9.class),
};
private static class ButtonSupport{
final int buttonId;
final long duration;
final Class<? extends Activity> clazz;
ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
this.buttonId = buttonId;
this.duration = duration;
this.clazz = clazz;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (ButtonSupport buttonSupport : buttonSupports) {
animButton(buttonSupport);
}
}
private void animButton(final ButtonSupport buttonSupport) {
final Button button = (Button) findViewById(buttonSupport.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(v, buttonSupport.clazz);
}
});
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
anim.setDuration(buttonSupport.duration);
button.startAnimation(anim);
}
public void startButtonAnimation(View btn, final Class<? extends Activity> clazz) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
player = MediaPlayer.create(MainActivity.this, R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(1,1);
player.start();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
player.stop();
startActivity(new Intent(getApplicationContext(), clazz));
overridePendingTransition(R.anim.animation, R.anim.animation2);
playerp = MediaPlayer.create(MainActivity.this, R.raw.musicp);
playerp.setLooping(false); // Set looping
playerp.setVolume(1,1);
playerp.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
animation xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>

I guess the easiest change to make it work looks like this:
int ANIM_OFFSET=100;
....
for (int i=0;i<buttonSupports.length;i++) {
ButtonSupport buttonSupport =buttonSupports[i];
animButton(buttonSupport,i);
}
....
anim.setStartOffset(i*ANIM_OFFSET);

Related

Animation not showing - why?

I am new and studying online . This is my first animation study at Android. The problem is that when I choose the answer my Card View animation is suppose to show immediately. But it didn't show as soon as I click . But I found out that after I click the button, I need to press Home button and resume it from the background apps to start the animation.The animation starts only after I do this process. Toast is showing without a problem. After I choose the answer I always need to do like that to show my animation. The app is about True or false.
Also the coding is exactly the same as my online tutorial. maybe my phone problem ?? I am using Samsung S 7 edge.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView QuestionTextView,QcountTextView;
private Button Tbutton,Fbutton;
private ImageButton prev,next;
private int count = 0;
private CardView cardView;
private Animation shake;
public Questions Qarray[] = new Questions[]{
new Questions(R.string.Q1, true),
new Questions(R.string.Q2, false),
new Questions(R.string.Q3, true),
new Questions(R.string.Q4, true),
new Questions(R.string.Q5, false),
new Questions(R.string.Q6, true),
new Questions(R.string.Q7, false)
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tbutton = findViewById(R.id.tb);
Fbutton = findViewById(R.id.fb);
QuestionTextView = findViewById(R.id.questionshow);
QcountTextView = findViewById(R.id.Qcount);
next = findViewById(R.id.nb);
prev = findViewById(R.id.pb);
cardView = findViewById(R.id.cardView);
shake = AnimationUtils.loadAnimation(MainActivity.this,R.anim.shakeanimation);
Tbutton.setOnClickListener(this);
Fbutton.setOnClickListener(this);
next.setOnClickListener(this);
prev.setOnClickListener(this);
QuestionTextView.setText(Qarray[count].question);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tb:
giveanswer(true);
pagechange();
break;
case R.id.fb:
giveanswer(false);
pagechange();
break;
case R.id.pb:
if(count != 0) {
count = (count - 1);
pagechange();
}
break;
case R.id.nb:
count = (count + 1) % Qarray.length;
pagechange();
break;
}
}
private void pagechange() {
QuestionTextView.setText(Qarray[count].getQuestion());
QcountTextView.setText((count+1) + " out of " + Qarray.length);
}
private void giveanswer(boolean b) {
boolean correctanswer = Qarray[count].ans;
if(b == correctanswer){
fadeView();
Toast.makeText(this,R.string.yes,Toast.LENGTH_SHORT).show();
}
else{
ShakeAnimation();
Toast.makeText(this,R.string.no,Toast.LENGTH_SHORT).show();
}
}
private void fadeView(){
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new
Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.GREEN);
}
#Override
public void onAnimationEnd(Animation animation){
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void ShakeAnimation(){
cardView.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.RED);
}
#Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
I expect the animation show as soon as I choose the answer.
you need to call alphaAnimation.start(); or shake.start(); just having animation listeners is not gonna start your animation when you want it to start.
try adding these lines exactly where you want the animations to run.
For Example:
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.start();
Also setAnimation should to be used with xml Animations if I'm not mistaking

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!!

how to define button display to appear one by one with animation?

i have 9 buttons in my activity, i use push right animation to show my buttons on Create, i change duration to set display, so i have 9 animation.xml with diffrent duration (from 1000 to 5000 for ex).
also i have to repeat 3 line code for 9 time in activity ,
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
so, i need to know is there any easier way to make something like that with Less code?
for ex, use 1 animation.xml and define button to run animation One after another?!
my full code:
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
final Button b1 = (Button)findViewById(R.id.Button03);
Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in1);
b1.startAnimation(anim1);
final Button b2 = (Button)findViewById(R.id.button1);
Animation anim2=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in2);
b2.startAnimation(anim2);
final Button b3 = (Button)findViewById(R.id.Button08);
Animation anim3=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in3);
b3.startAnimation(anim3);
final Button b4 = (Button)findViewById(R.id.Button04);
Animation anim4=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in4);
b4.startAnimation(anim4);
final Button b5 = (Button)findViewById(R.id.Button01);
Animation anim5=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in5);
b5.startAnimation(anim5);
final Button b6 = (Button)findViewById(R.id.Button07);
Animation anim6=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in6);
b6.startAnimation(anim6);
final Button b7 = (Button)findViewById(R.id.Button05);
Animation anim7=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in7);
b7.startAnimation(anim7);
final Button b8 = (Button)findViewById(R.id.Button02);
Animation anim8=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in8);
b8.startAnimation(anim8);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(b);
}
});
}
public void startButtonAnimation(Button btn){
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(), Activity2.class));
overridePendingTransition(R.anim.animation, R.anim.animation2);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Yes you can create method
private void buttonAnim(int buttonId, long duration){
Button b = (Button) findViewById(buttonId);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this,your_anim_id);
anim.setDuration(duration);
b.startAnimation(anim);
}
And use it to call your animation for ex:
buttonAnim(R.anim.Button06, 1000);
Additionally you can put your buttons in array and do it in loop as is shown in the code below:
int[] buttonsArrays = new int {
R.anim.Button01,
R.anim.Button02,
R.anim.Button06,
...
}
int[] durations = new int {
1000,
5000,
1000,
...
}
And in your code:
for(int index=0; index <buttonsArrays.length; index++){
buttonAnim(buttonsArrays[index],durations[index]);
}
But remember the lengths of arrays have to be the same
Or you can use Pair object as is shown in the code below:
Pair<Integer, Long>[] pairs = new Pair[]{
new Pair(R.id.Button01, 1000l),
new Pair(R.id.Button02, 5000l),
...
};
And in your code:
for(Pair<Integer,Long> pair : pairs ){
buttonAnim(pair.first,pair.second);
}
It will be most save
EDIT
Please find my proposition of your class:
public class MainActivity extends ActionBarActivity {
private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
new ButtonSupport(R.id.Button06,1000l, YourClassActivity.class),
new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
new ButtonSupport(R.id.Button08, 4000l,YourClassActivity4.class),
new ButtonSupport(R.id.Button04, 5000l,YourClassActivity5.class),
new ButtonSupport(R.id.Button01, 6000l,YourClassActivity6.class),
new ButtonSupport(R.id.Button07, 7000l,YourClassActivity7.class),
new ButtonSupport(R.id.Button05, 8000l,YourClassActivity8.class),
new ButtonSupport(R.id.Button02, 9000l,YourClassActivity9.class),
};
private static class ButtonSupport{
final int buttonId;
final long duration;
final Class<? extends Activity> clazz;
ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
this.buttonId = buttonId;
this.duration = duration;
this.clazz = clazz;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (ButtonSupport buttonSupport : buttonSupports) {
animButton(buttonSupport);
}
}
private void animButton(final ButtonSupport buttonSupport) {
final Button button = (Button) findViewById(buttonSupport.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(v, buttonSupport.clazz);
}
});
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.your_animation);
anim.setDuration(buttonSupport.duration);
button.startAnimation(anim);
}
public void startButtonAnimation(View btn, final Class<? extends Activity> clazz) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(), clazz));
overridePendingTransition(R.anim.animation, R.anim.animation2);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Slideshow with multiple animation and Viewpager images

I have listactivity app forming many rows , one row is images Slideshow , when you click
the row open activity showing ImageView , also there is optionmenu of one item which is( slideshow animation setting ), when you click it , it open checkbox preference
animations screen with multiple checkboxes each one apply different animation to images
slideshow ,where user determine either to slide images with many animations available by
check its checkbox animation name or when uncheck all the checkboxs so the slideshow activity
must show images in viewpager pattern .
android:defaultValue="true" for first animation which is fade_in animation.
BUT: when you open slideshow activity its open the images in imagepager pattern ,and ignoring android:defaultValue="true" for fade_in checkbox,
then after go to preference screen to choose another animation then back to slideshow
activity , it doesn't apply the new animation , i have to press back button many times
till finish all images scrolled in pager then it apply the next animation ,
and sometimes it stuck on image pager and freeze's ,the normal behavior is applying
the next animation once press back button which return to slideshow .
another thing when i was in image viewpager pattern and scroll it ,
it scroll a few number of images then back to first image then i scroll images again
and suddenly it back to first image one and so on .
whole project can be downloaded from here
any help will be appreciated.
SlideShow.java
public class SlideShow extends Activity {
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.day_one_1, R.drawable.day_one_2, R.drawable.day_one_3,
R.drawable.day_one_4, R.drawable.day_one_5, R.drawable.day_one_6,
R.drawable.day_one_7, R.drawable.day_one_8, R.drawable.day_one_9,
R.drawable.day_one_10, R.drawable.day_one_11, R.drawable.day_one_12,
R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18,
R.drawable.day_one_19,R.drawable.day_one_20
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide);
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 8000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
private void AnimateandSlideShow() {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean animation = getPrefs.getBoolean("animation", true);
boolean animation_one = getPrefs.getBoolean("animation_one", false);
boolean animation_two = getPrefs.getBoolean("animation_two", false);
boolean animation_three = getPrefs.getBoolean("animation_three", false);
boolean animation_four = getPrefs.getBoolean("animation_four", false);
boolean animation_five = getPrefs.getBoolean("animation_five", false);
if (animation == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
slidingimage.startAnimation(rotateimage);
}else if(animation_one == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
slidingimage.startAnimation(rotateimage);
}else if (animation_two == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_out);
slidingimage.startAnimation(rotateimage);
}else if (animation_three == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.bounce);
slidingimage.startAnimation(rotateimage);
}else if(animation_four == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in_2);
slidingimage.startAnimation(rotateimage);
}else if (animation_five == true) {
slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.flip);
slidingimage.startAnimation(rotateimage);
}else if(animation == false && animation_one == false && animation_two == false){
Intent intent = new Intent(SlideShow.this, ImagePager.class);
startActivity(intent);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Intent p = new Intent("com.test.test.SETTING");
startActivity(p);
break;
}
return false;
}
}
ImagePager.java
public class ImagePager extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.day_one_1, R.drawable.day_one_2, R.drawable.day_one_3,
R.drawable.day_one_4, R.drawable.day_one_5, R.drawable.day_one_6,
R.drawable.day_one_7, R.drawable.day_one_8, R.drawable.day_one_9,
R.drawable.day_one_10, R.drawable.day_one_11, R.drawable.day_one_12,
R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18,
R.drawable.day_one_19,R.drawable.day_one_20
};
public class ImagePagerAdapter extends PagerAdapter {
Activity activity;
int[] imageArray;
public ImagePagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
ImageView im=(ImageView) layout.findViewById(R.id.pager_imageView);
im.setImageResource(imageArray[position]);
((ViewPager) collection).addView(layout, 0);
return layout;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Intent p = new Intent("com.test.test.SETTING");
startActivity(p);
break;
}
return false;
}
}
Please post the project to be work out a proper solution mean while try using this
First of all as you have declared the ImageView slidingimage; as an instance variable, declare the Animation rotateimage as an instance variables and use it as
rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
Now once you add the animation to imageview call the invalidate() method inside the if loops
slidingimage.startAnimation(rotateimage)
slidingimage.invalidate();
And in your imagePager class make ViewPager myPager as static instance variable private static ViewPager myPager and add this code to the class
public static void refreshPager(){
if(myPager != null)
myPager.invalidate();
}
And in your settings class call this method on the onBackPressed() event
#Override
public void onBackPressed() {
ImagePager.refreshPager();
super.onBackPressed();
}
EDIT
All you just need to do is this, add this code in your Slide.java file after the setContentView(R.layout.main); and before final Handler mHandler = new Handler();
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
if(getPrefs.getBoolean("Initialization", false) == false){
SharedPreferences.Editor edit = getPrefs.edit();
edit.putBoolean("animation_one", true);
edit.putBoolean("Initialization", true);
edit.commit();
}
Like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
if(getPrefs.getBoolean("Initialization", false) == false){
SharedPreferences.Editor edit = getPrefs.edit();
edit.putBoolean("animation_one", true);
edit.putBoolean("Initialization", true);
edit.commit();
}
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 8000; // repeat every 4 sec.
Timer timer = new Timer
// Some more code............
The preference file was not created only ;)
EDIT 2
In you Slide.java file declare a boolean like this
public boolean loaded ;
and code your else if like this
else if(animation_one == false && animation_two == false && animation_three == false
&& animation_four == false && animation_five == false){
Intent intent = new Intent(Slide.this, ImagePager.class);
if(loaded)
startActivity(intent);
loaded = true;
}
Create a static variable in Slide.java public static TimerTask task; and in your ImagePager.java add this code if(imageArra.length-1 == position)Slide.task.cancel(); in instantiateItem() method like this
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
ImageView im=(ImageView) layout.findViewById(R.id.myimage);
im.setImageResource(imageArray[position]);
((ViewPager) collection).addView(layout, 0);
if(imageArra.length-1 == position)
Slide.task.cancel();
return layout;
}
Hope this works

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