如何在单击按钮时滑入和滑出 Activity?

发布于 2024-12-01 14:19:55 字数 24 浏览 1 评论 0原文

我想滑入和滑出按钮单击事件的活动。

I want to slide-in and slide-out the activity on button click event.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-12-08 14:19:55

从 2.1 开始你就会这样做。首先,您将从 api demos 下载 anim 文件夹。然后像下面这样对每个意图进行应用。

Intent intent = new Intent(Fisrst.this, Second.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left, R.anim.slide_right);

from 2.1 onwords you will do that. First you will download anim folder from api demos. Then apply like below for every intent.

Intent intent = new Intent(Fisrst.this, Second.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
嘿哥们儿 2024-12-08 14:19:55

试试这个,

使用 viewflipper 你可以做到这一点。

  private Animation inFromTopAnimation() {

Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,   0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outToBottomAnimation() {
Animation outtoBottom = new TranslateAnimation(
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f
);
outtoBottom.setDuration(1000);
outtoBottom.setInterpolator(new AccelerateInterpolator());
return outtoBottom;
}

private Animation outToTopAnimation() {
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,   0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outFromBottomAnimation() {
Animation outFromBottom = new TranslateAnimation(
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f
);
outFromBottom.setDuration(1000);
outFromBottom.setInterpolator(new AccelerateInterpolator());
return outFromBottom;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 flipper = (ViewFlipper) findViewById(R.id.flipper);

 ImageView imgview1 = (ImageView) findViewById(R.id.imageview1);

 Button button2 = (Button) findViewById(R.id.flipback);

 imgview1.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
         flipper.setInAnimation(inFromTopAnimation());
         flipper.setOutAnimation(outToBottomAnimation());
         flipper.showNext();      
     }
 });

 button2.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
         flipper.setInAnimation(outToTopAnimation());
         flipper.setOutAnimation(outFromBottomAnimation());
         flipper.showPrevious();

     }

 });

Try this,

With viewflipper u can do this.

  private Animation inFromTopAnimation() {

Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,   0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outToBottomAnimation() {
Animation outtoBottom = new TranslateAnimation(
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f
);
outtoBottom.setDuration(1000);
outtoBottom.setInterpolator(new AccelerateInterpolator());
return outtoBottom;
}

private Animation outToTopAnimation() {
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,   0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outFromBottomAnimation() {
Animation outFromBottom = new TranslateAnimation(
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f
);
outFromBottom.setDuration(1000);
outFromBottom.setInterpolator(new AccelerateInterpolator());
return outFromBottom;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 flipper = (ViewFlipper) findViewById(R.id.flipper);

 ImageView imgview1 = (ImageView) findViewById(R.id.imageview1);

 Button button2 = (Button) findViewById(R.id.flipback);

 imgview1.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
         flipper.setInAnimation(inFromTopAnimation());
         flipper.setOutAnimation(outToBottomAnimation());
         flipper.showNext();      
     }
 });

 button2.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
         flipper.setInAnimation(outToTopAnimation());
         flipper.setOutAnimation(outFromBottomAnimation());
         flipper.showPrevious();

     }

 });
Spring初心 2024-12-08 14:19:55

以下代码对我有用:
A-> B(B 将滑入)

在活动 B 上:

    protected void onCreate(Bundle savedInstanceState) {
    //do something...
        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.g_slide_in_right));
    }

B -> A(B会滑出)
单击 B 的按钮:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
          @Override
          public void run() {
                      Intent myIntent = new Intent(B.this.getBaseContext(), A.class);
                      B.this.startActivity(myIntent);
          }
        }, 500);

The following codes worked for me:
A -> B (B will slide in)

On activity B:

    protected void onCreate(Bundle savedInstanceState) {
    //do something...
        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.g_slide_in_right));
    }

B -> A (B will slide out)
on clicked a button of B:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
          @Override
          public void run() {
                      Intent myIntent = new Intent(B.this.getBaseContext(), A.class);
                      B.this.startActivity(myIntent);
          }
        }, 500);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文