Change DialogFragment enter/exit transition at just before dismissing - android

I have a DialogFragment and I set animation for enter/exit in the onActivityCreated method as below:
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
My DialogAnimation style files is as follows:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
This works for me now...
Now my problem is i want to have two different exit animation one for when the "OK" button is clicked and one for the cancel button. So what I did was I tried changing the transition just before dismissing but it didn't work.
Any solution on how it can be achieved?
This is what I tried:
#Override
public void onClick(View v) {
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation2;
this.dismiss();
}

You can do it inside your DialogFragment, without changing
getDialog().getWindow()
.getAttributes().windowAnimations
You should animate "decor view" in onStart and onClick.
This is the code snipped :
Create Dialog first
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Hello from animated dialog :)")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//we have to add button here and then override it's click in onStart
}
}
)
.setCancelable(false)
.create();
}
Then override onStart method
#Override
public void onStart() {
super.onStart();
AlertDialog dialog = (AlertDialog)getDialog();
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));
scaleDown.setDuration(2000);
scaleDown.start();
Button positiveButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);
positiveButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
scaleDown.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
dismiss();
}
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
scaleDown.setDuration(2000);
scaleDown.start();
}
});
}
Here is the result animation
And if you remove scale properties from my code you will get only alpha animation. Exactly as you wanted.
Remove this:
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),

You can set an Up & Down animations for dialog fragment. In 'res/anim' add two files:
// Slide up animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0" />
</set>
// Slide dowm animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%p" />
</set>
// Style
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
// Inside Dialog Fragment
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}

I think the best approach is to call the different animations on Button click. Hence you would have something similar to the below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button OkButton = (Button) findViewById(R.id.btnOk);
Button CancelButton = (Button) findViewById(R.id.btnCancel);
OkButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}
});
return true;
CancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation2;
}
});
return true;
}
If I were you, I would also use correct naming conventions for future reference. For example setting DialogAnimation to OkAnimation and DialogAnimation2 to CancelAnimation.
Home this helps :)

One simple way to do what you want is to use animation listeners like.
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
// dismiss your dialog in here and it will work
}
});
Start animation on your onclick method and dismiss dialog on onAnimationEnd() method.You may have to make snimation objects and start them manually with View's startAnimation(animation) method.

You should set a theme for your base dialog
let say :-
public class CustomDialogFragment extends DialogFragment implements OnEditorActionListener
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Set a theme on the dialog builder constructor!
AlertDialog.Builder builder =
new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
builder
.setTitle( "Your title" )
.setMessage( "Your message" )
.setPositiveButton( "OK" , new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/anim_in</item>
<item name="android:windowExitAnimation">#anim/anim_out</item>
</style>
refer this

If any one ends up using the onCreateDialog answer in many dialogs, then you can use this Kotlin extension function.
//ExtensionFunctions.kt file
fun Dialog.setWindowAnimations(#StyleRes id: Int): Dialog {
this.window?.attributes?.windowAnimations = id
return this
}
and in your onCreateDialog function
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState)
.setWindowAnimations(R.style.MyDialogAnimation)
}

Related

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 Customizing onAnimationEnd

I simply want to use this working animation code I have in a more efficient manner. The code below works exactly as I want it to but I don't want to copy and paste this everywhere in the thirty places across several activities in which I'll be using this specific fadeout animation. How can I set up a class to do this? The primary problem is setting the view to invisible in onAnimationEnd.
final TextView ph = (TextView) findViewById(R.id.textPlaceholder);
final Animation fadeOut = new AlphaAnimation(1.0f, 0.0f);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(500);
fadeOut.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
ph.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
ph.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ph.startAnimation(fadeOut);
}
});
Create a helper class:
Class ClickToFadeOutAnimation
private View view;
public static void attachTo(View view) {
new FadeOutAnimation(View view);
}
public FadeOutAnimation(View view) {
this.view = view;
view.setOnCLickListsner(new OnClickListener() {
animate();
)
}
public void animate() {
Animation fadeOut = ...
.. rest of your animation code
}
}
And to use it:
TextView ph = (TextView) findViewById(R.id.textPlaceholder);
ClickToFadeOutAnimation.attachTo(ph);
Extend the AlphaAnimation class where you set the duration, offset and AnimationListener in the constructor.

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.

translation animation issue

I want to make a layout animated through translation animation from screen's particular X Y position. and this is happening very correcty. now the problem starts when animations completes the layout again go to screen's top rihgt corner. i want the layout stay at the same position on which the animation ends.
AnimationListener animationListener = new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
drawer_layout.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
drawer_layout.setVisibility(View.VISIBLE);
}
};
final Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.top_to_bottom_in);
animTranslate.setAnimationListener(animationListener);
drawer_layout = (LinearLayout) findViewById(R.id.drawer_layout);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// bitmapBackground = CaptureBackground();
faded_layout.setBackgroundColor(Color.parseColor("#50ffffff"));
// HandleDropAnimation(drawer_layout);
drawer_layout.startAnimation(animTranslate);
}
});
and this is my xml.
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="25%p"
android:toYDelta="50%p" />
Call
animTranslate.setFillAfter(true);
before you start the animation. According to java docs:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
use before animation starts
animTranslate.setFillAfter(true);

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