Android Left to Right slide animation

前端 未结 9 958
旧时难觅i
旧时难觅i 2020-11-22 08:18

I have three activities whose launch modes are single instance.
Using onfling(), I swing them left and right.

The problem is when I swipe right to

9条回答
  •  囚心锁ツ
    2020-11-22 08:53

    If you want the transition work for whole application you can create a rootacivity and inherit it in the activity you need. In Root Activity's onCreate call overridePendingTransition with desired direction. And onStart call overridePendingTransition with other direction if activity is resumed. Here I am giving full running code below.Correct me if I am wrong.

    create this xml file on your anim folder

    anim_slide_in_left.xml

    
    
        
        
    
    

    anim_slide_in_right.xml

    
    
        
        
    
    

    anim_slide_out_left.xml

    
    
        
        
    
    

    anim_slide_out_right.xml

    
    
        
        
    
    

    RootActivity

    import android.app.Activity;
    import android.os.Bundle;
    
    public class RootActivity extends Activity {
        int onStartCount = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            onStartCount = 1;
            if (savedInstanceState == null) // 1st time
            {
                this.overridePendingTransition(R.anim.anim_slide_in_left,
                        R.anim.anim_slide_out_left);
            } else // already created so reverse animation
            { 
                onStartCount = 2;
            }
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            if (onStartCount > 1) {
                this.overridePendingTransition(R.anim.anim_slide_in_right,
                        R.anim.anim_slide_out_right);
    
            } else if (onStartCount == 1) {
                onStartCount++;
            }
    
        }
    
    }
    

    FirstActivity

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class FirstActivity extends RootActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.tvTitle);
            tv.setText("First Activity");
            Button bt = (Button) findViewById(R.id.buttonNext);
            bt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(FirstActivity.this, SecondActivity.class);
                    startActivity(i);
    
                }
            });
        }
    
    }
    

    SecondActivity

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class SecondActivity extends RootActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.tvTitle);
            tv.setText("Second Activity");
            Button bt = (Button) findViewById(R.id.buttonNext);
            bt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
                    startActivity(i);
    
                }
            });
    
        }
    
    }
    

    ThirdActivity

    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class ThirdActivity extends RootActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.tvTitle);
            tv.setText("Third Activity");
            Button bt = (Button) findViewById(R.id.buttonNext);
            bt.setText("previous");
            bt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
    
                }
            });
        }
    
    }
    

    and finally Manifest

    
    
    
        
    
        
            
                
                    
    
                    
                
            
            
            
            
            
        
    
    
    

提交回复
热议问题