Final position visible during SharedElementTransition using RecyclerView - android

I'm using a SharedElementTransition when opening a new Activity. The shared element is in a RecyclerView in ActivityA (starting Activity), and also in a RecyclerView in a Fragment in ActivityB (new Activity). The animation works mostly as expected, except that the shared element is also visible in its final position throughout the animation, which looks pretty terrible.
My ActivityB.onCreate() looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
<snip>
if (Utils.hasLollipop()) {
postponeEnterTransition();
}
}
In the ViewHolder.onBindViewHolder() for the holder that contains the image I'm using as a shared element, I call this after loading the image data into the view (in a Picasso callback):
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void triggerTransition() {
if (!hasTriggeredTransition) {
hasTriggeredTransition = true;
sharedElement.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
sharedElement.setTransitionName("sharedElement");
sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
containingFragment.getActivity().startPostponedEnterTransition();
return true;
}
});
}
}
The image transitions from its location in ActivityA to where it should be in ActivityB, but during the animation, it also appears in the final location. How can I hide the final location until the animation is finished?
Update: In an attempt at simplifying, I've removed the image callbacks. Now, I just set the view to a solid background color and immediately calling triggerTransition(), with the same results.
Relatedly, is there a way to slow down the animation for debugging?

You should be able to slow down the animation for debugging purposes by increasing the duration value in your transition xml file like below:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:transitionOrdering="together"
android:duration="250">
You can even change the Ordering to "sequencing" to further analyse..

Related

Refresh android ImageButton after rotation

I have an ImageButton in my fragment which has its background set to an android drawable icon. In some cases that button needs to be rotated (this works). My problem is that when I come back to the fragment after being in another activity then I want the button to be "reset" to its original position.
Here is how the button is set:
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
button = (ImageButton)view.findViewById(R.id.doButtonThing);
button.setOnClickListener(this);
if (condition) {
button.animate().rotation(90f).setInterpolator(new AccelerateDecelerateInterpolator());
}
}
I have tried using:
button.clearAnimation();
button.invalidate();
button.postInvalidate();
inside the onResume() method but none of them work. How can I reset/reload the original button?
The code of clearAnimation()
/**
* Cancels any animations for this view.
*/
public void clearAnimation() {
if (mCurrentAnimation != null) {
mCurrentAnimation.detach();
}
mCurrentAnimation = null;
invalidateParentIfNeeded();
}
which gives me the impression that it clears a currently running animation (Although I'm not sure). Instead of button.clearAnimation();, you can use button.animate().rotation(0.0f); to reverse-rotate the button.
Also instead of onResume(), I suggest you to call the function later on, maybe onStop(). In the lifecycle, onResume() comes after onViewCreated(final View view, Bundle savedInstanceState), calling it in onResume() would invalidate your animation.

Unable to set ImageResource of Button after orientation change

the aim is to change the image resource of the button. If I don't change the orientation it works correctly, but if I change the orientation, the image resource is not set (or probably not updated).
I observe in DebugMode that the following code (toggleButton method) is always executed, regardless the screen orientation is changed. The buttonIconID is also always correct. The problem is, that the image resource of the button is not set after changing the orientation.
#Override
public void onCreate(Bundle savedInstanceState) {
...
mTopBar = new TopBar(this);
...
}
TopBar constructor:
public TopBar(MainActivity mainActivity) {
this.mainActivity = mainActivity;
mButton = (ImageButton) mainActivity
.findViewById(R.id.toggleButton);
mButton.setOnTouchListener(this);
...
}
toggleButton method in TopBar:
public void toggleButton(final int buttonIconID) {
mainActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mButton.setImageResource(buttonIconID);
}
});
}
Thanks for your help!
An Activity is recreated every time you change the orientation. So when you change the orientation all views are reseted to their standard configuration (or the settings you have set in the onCreate()/onResume() etc methods.
So if you want to keep the image resource you have set programmatically after changing the orientation, you should buffer the data and set it again in the onCreate() or onResume() method.

addView inconsistent behavior

I have a custom component that extends RelativeLayout which in turns holds a GridLayout(named mFormLayout). I have a public method that adds two spinners with their proper adapter source and an imageview which acts as a button to remove rows.
public class EditableTwinSpinnerGridForm extends EditableGridForm
{
public void addTwinSpinnerRow(final Locale.MapKey spinner1DefVal, final Locale.MapKey spinner2DefVal)
{
Spinner spinner1 = createSpinner(mTSP.getFirstSpinnerRes(), spinner1DefVal.getId());
spinner1.setOnItemSelectedListener(mTSP.getIsl());
Spinner spinner2 = createSpinner(mTSP.getSecondSpinnerRes(), spinner2DefVal.getId());
ImageView rmvBtn = createRemoveBtn();
mFormLayout.addView(spinner1);
mFormLayout.addView(spinner2);
mFormLayout.addView(rmvBtn);
}
}
For some reason, this method works when I am adding rows from a call to onCreate in an activity, but when I am calling this method after the activity is created(from an onclicklistener) the Spinners are either not there or only one of them shows up. They do take the space because I see the row and the removable image view.
I have also noticed that when I focus on a EditText in the same activity and the keyboard pops up, the added spinners show up when I press back to remove the keyboard.
Here's the code I use to create a spinner :
protected Spinner createSpinner(Integer spinnerSrc, String defaultSpinnerValue)
{
Spinner spinner = new Spinner(mCtx, Spinner.MODE_DIALOG);
// Setting the bg color to the containing color to remove the spinner arrow.
spinner.setBackgroundResource(R.color.container_bg);
SparseArray<Phrase> map = Locale.getInstance().getMap(spinnerSrc);
PhraseArrayAdapter adapter = createSpinnerFromMap(spinnerSrc);
spinner.setAdapter(adapter);
if (defaultSpinnerValue.equals(Utilities.EMPTY_STRING) || defaultSpinnerValue.isEmpty())
{
throw new IllegalStateException();
}
else
{
Utilities.getInstance().setMapSpinnerPosByValue(map, defaultSpinnerValue, spinner);
}
adapter.setDropDownViewResource(R.layout.editable_spinner_dropdown_item);
setSpinnerLayoutParams(spinner);
return spinner;
}
protected void setSpinnerLayoutParams(Spinner spinner)
{
GridLayout.LayoutParams lp = createDefaultGridParams();
lp.setGravity(Gravity.FILL_HORIZONTAL);
lp.width = 250;
lp.rightMargin = 0;
spinner.setLayoutParams(lp);
}
The code works when the activity is loaded so I'm a bit stumped. I looked around and some people suggested I set LayoutParams in addView, but why would this method work in onCreate, but not afterwards?
Here's what's happening visually(The first three rows are added from a loop in onCreate(), the two second ones are added by pressing "Add +"). As you can see the second spinner isn't showing up, sometimes both aren't showing up. I also tried calling invalidate and requestLayout to no avail.
I had looked into the invalidate method, here's where it is located currently :
public abstract class EditableForm extends RelativeLayout implements ObservableInt
{
private class OnAddClicked implements OnClickListener
{
#Override
public void onClick(View v)
{
onAddClicked(v);
EditableForm.this.invalidate();
mFormLayout.invalidate();
}
}
}
Which calls(In a subclass of EditableGrid) :
#Override
protected void onAddClicked(View clickedView)
{
addTwinSpinnerRow();
notifyObservers(new ObservableData(EDITABLE_ADD_GRID_CLICKED, null));
}
protected void addTwinSpinnerRow()
{
Locale.MapKey v1 = Locale.getInstance().getMap(mTSP.getFirstSpinnerRes()).get(0).getMapId();
Locale.MapKey v2 = Locale.getInstance().getMap(mTSP.getSecondSpinnerRes()).get(0).getMapId();
addTwinSpinnerRow(v1, v2);
}
Have you tried calling the Invalidate method of the container view rather than the added view?
Most likely the views you are adding are there, they just need to be drawn which is suggested by your keyboard hide/show difference. Does rotating the device also cause them to appear? If so, this again suggests that you need to redraw your custom layout.
When it's necessary to execute invalidate() on a View?

How to make a page flip like animation when changing between activities?

How can I make a page flip like animation when moving from one activity to other? On some ios applications I saw this, but when I searched for android I could not find any tutorials or code snippets for this.
Please help
Yes it is possible . Please look at this tutorial.
Here's a tutorial on how to add an animation when transistioning between two activities. However, instead of using a translate animation like in the article, you'll want to use a rotate animation.
The flip animation for Activity doesn't exist on Android..sorry!
Here is demo code from sdk:
/**
* <p>Example of using a custom animation when transitioning between activities.</p>
*/
public class Animation extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.fade_animation);
button.setOnClickListener(mFadeListener);
button = (Button)findViewById(R.id.zoom_animation);
button.setOnClickListener(mZoomListener);
}
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, Controls1.class));
// Supply a custom animation. This one will just fade the new
// activity on top. Note that we need to also supply an animation
// (here just doing nothing for the same amount of time) for the
// old activity to prevent it from going away too soon.
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
private OnClickListener mZoomListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, Controls1.class));
// This is a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
};
}
all code is at apidemo/app/ ,:)

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

Categories

Resources