I want to set a animation for several View.and start One after another(such below imag)
I create a translation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-6%p" android:toXDelta="50%p"
android:duration="2000"/>
and set for Views.
imag1=(ImageView) findViewById(R.id.imag_icon1);
imag2=(ImageView) findViewById(R.id.imag_icon2);
imag3=(ImageView) findViewById(R.id.imag_icon3)
anim1=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.several_anim);
int count=0;
ImageView [] arr_imag={imag1,imag2,imag3};
arr_imag[count].startAnimation(anim1);
anim1.setAnimationListener(new 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 anim) {
count++;
if(count<3)
arr_imag[count].startAnimation(anim1);
}
});
but when run app .
start Animation with together.
I'm really confused.
What is the problem
Try This.
anim1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
anim2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:startOffset="500"
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
anim3.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:startOffset="1000"
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
then use this
Animation animanim1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim1);
Animation animanim2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim2);
Animation animanim3 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim3);
imag1.startAnimation(animanim1);
imag2.startAnimation(animanim2);
imag3.startAnimation(animanim3);
Related
I want to change direction of an image in android animation.My fragment is
public class IconAnimation extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.icon_animation, container,false);
return v;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.comedy:
Animation animation1 AnimationUtils.loadAnimation(getActivity(),
R.anim.slide);
ImageView image= (ImageView)v.findViewById(R.id.image);
image.startAnimation(animation1);
break;
}
}
}
and My animation XML is
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="150%p"
android:duration="800"
/>
Its output is something like this:Output
But I want something like this:expected outout
final TranslateAnimation animationt1 = new TranslateAnimation(fromXoffset, toXOffset, fromYoffset,toYoffset);
animationt1.setDuration(300);
animationt1.setFillAfter(true);
animationt1.setAnimationListener(this);
yourView.startAnimation(animation1);
You can do it also by XML :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator" >
<!-- Use startOffset to give delay between animations -->
<!-- Move -->
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="700"
android:toXDelta="50%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="800"
android:toYDelta="-10%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="1300"
android:toXDelta="75%p" />
How can I move an object out of the screen?
This is how I move the TextView from under the screen into the screen.
slide_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="500" android:fillAfter="true"/>
</set>
This is how I move it out of the screen:
slide_down_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="100%" android:duration="500" android:fillAfter="true"/>
</set>
But the TextView appears again. Why?
That is because it renders it again after the animation and will actually place the TextView to its default position from the screen/layout.
solution:
You need to add a listener and set the visibility of your TextView to gone or invisible at the end of the listener.
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
You_text_view.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
I'm trying to make a animation from the top to the button.
If I click on a button, he should show a View from top. And if I'm clicking again, he should animate it back to the top. This is, what i have:
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btFilterDown = (Button) findViewById(R.id.btFilterDown);
Button btFilterUp = (Button) findViewById(R.id.btFilterUp);
final View layout = findViewById(R.id.slide);
layout.setVisibility(View.GONE);
btFilterUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideView(layout);
}
});
btFilterDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showView(layout);
}
});
}
private void hideView(final View view){
Animation animation = AnimationUtils.loadAnimation(this, R.layout.slide_out_up);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(animation);
}
private void showView(final View view){
Animation animation = AnimationUtils.loadAnimation(this, R.layout.slide_in_down);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.VISIBLE);
}
});
view.startAnimation(animation);
}
#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;
}
}
Slide_in_down.xml:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="-1000"
android:duration="#android:integer/config_longAnimTime" />
Slide_out_up.xml:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:toXDelta="0"
android:toYDelta="-1000"
android:duration="#android:integer/config_longAnimTime" />
And my main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/darker_gray" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white" >
<Button
android:id="#+id/btFilterDown"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="FILTER!!!"/>
</LinearLayout>
<LinearLayout
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#99009900"
android:orientation="vertical">
<Button
android:id="#+id/btFilterUp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" NO FILTER!!!"/>
</LinearLayout>
</LinearLayout>
So now I have 2 problems:
First:
The button of my first layout is not visible anymore, if the second layout is over it. I want to make the view, which animated in, transparency. But it looks very bad, if the objects from the first view away.
How can I lay the animated layout over the first, so that all objects on the first layout are visible?
Second:
The animation should start at the bottom of the Actionbar. I mean, if the layout is coming in, that he starts on bottom edge and not on the top of the screen.
How can I set the start point of the animation?
Thanks a lot :)
Your animation must have 4 xml's like slide_down_in, slide_down_out, slide_up_in, slide_up_out, and use percentage like
slide_down_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="-100%"
android:toYDelta="0%"
android:duration="500" />
</set>
slide_down_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="500" />
</set>
slide_up_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="100%"
android:toYDelta="0%"
android:duration="500" />
</set>
slide_up_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:toYDelta="-100%"
android:duration="500" />
</set>
Its Working
Intent i = new Intent(getActivity(), FeedbackSuccessActivity.class);
i.putExtra("title", "Success!");
i.putExtra("message", "\n Profile Update Successfully \n ");
startActivity(i);
overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);
slide_down_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="-100%"
android:toYDelta="0%"
android:duration="500" />
</set>
slide_down_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="500" />
</set>
I have 2 imageview (a imageview for a pen image, and a imageview for a line, both are drawable), that each one has it own view animation that works perfectly.
my problem is that when I start the animation for the pen I want it to interact and animate the drawing of the line in the other view animation (I want that it will show as the pen is drawing the line), how do I do that?
my xml for animation for the pen imageview is:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:fromYDelta="-50%p"
android:toYDelta="-50%p"
android:duration="2000"/>
</set>
my xml for animation for the line imageview is:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="0%"
android:pivotY="50%"
android:fromXScale="0%"
android:toXScale="100%"
android:fromYScale="1.0"
android:toYScale="1.0"
android:fillAfter="true"
android:duration="2000"
android:interpolator="#android:anim/linear_interpolator"
/>
please help!
I believe this can be done with the help of animationListener. Add an animationListener for the pen's animation. In onAnimationStart method do lineImage.startAnimation(lineAnimation);
Sample:
penAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
lineImage.startAnimation(lineAnimation);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I have add a ViewFlipper in which has 2 linearlayout,and I have made an animation xml:
left_in.xml:
<?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="3000"/>
</set>
right_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="3000"/>
</set>
left_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="3000"/>
</set>
right_in.xml:
<?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="3000"/>
</set>
the "Next" button in one linearlayout which shows when first load the app:
mNext.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
//mViewFlipper.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));
mViewFlipper.showNext();
}
});
and the "Prev" button:
mPrev.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));
mViewFlipper.showPrevious();
}
});
The "Next" Button goes well, But the "Prev" Button goes strange: when I click the "prev",it first change into the previous view and then start the animation ,and at last it changes into the Previous view again! How to solve it??
Thanks in advance!!
You want to use setOutAnimation() and setInAnimation().
Well this a very old post. but still the fix is here:
you need to call viewFlipper.setOutAnimation(null) and viewFlipper.setInAnimation(null) to reset the animation .
#Override
public void onClick(View v)
{
if (v.equals(mNext))
{
mViewFlipper.setOutAnimation(null);
mViewFlipper.setInAnimation(null);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));
mViewFlipper.showNext();
}
else if (v.equals(mPrev))
{
mViewFlipper.setOutAnimation(null);
mViewFlipper.setInAnimation(null);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));
mViewFlipper.showPrevious();
}
}