gpt4 book ai didi

android - 从右向左滑动 Android 动画

转载 作者:IT王子 更新时间:2023-10-28 23:41:49 25 4
gpt4 key购买 nike

嘿,我正在开发一个需要在 Android WebView 上使用幻灯片动画的 Android 项目。当用户从左向右滑动时,它移动到新页面,当它从右向左滑动时,它移动到上一页。但是Android只有两个过渡,即slide_out_right 和 slide_in_left。使用它们后,从左到右的滑动工作完美无缺,但另一个看起来很奇怪(相反)。

任何解决方案。我希望 slide_out_left 动画更精确。

最佳答案

通读blog post以过渡动画为例,我已包含以下代码:

通话 Activity

package com.as400samplecode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button nextActivity = (Button) findViewById(R.id.nextActivity);
nextActivity.setOnClickListener(this);

}

public void onClick(View v) {

switch (v.getId()) {
case R.id.nextActivity:
Intent nextActivity = new Intent(this,NextActivity.class);
startActivity(nextActivity);
//push from bottom to top
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
//slide from right to left
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
break;

// More buttons go here (if any) ...

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

调用 Activity 的布局资源

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity"
android:background="@color/ivory">
<Button android:id="@+id/nextActivity" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="15dp"
android:text="Go to Next Activity" />
</RelativeLayout>

调用的 Activity

package com.as400samplecode;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NextActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);

Button previousActivity = (Button) findViewById(R.id.previousActivity);
previousActivity.setOnClickListener(this);

}

public void onClick(View v) {

switch (v.getId()) {
case R.id.previousActivity:
finish();
//push from top to bottom
overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
//slide from left to right
//overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
break;

// More buttons go here (if any) ...

}
}


}

被调用 Activity 的布局资源

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".NextActivity"
android:background="@color/khaki">

<Button android:id="@+id/previousActivity" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="15dp"
android:text="Go to Previous Activity" />

</RelativeLayout>

动画资源 - push_down_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="5000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" />
</set>

动画资源 - push_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%p" android:duration="5000" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="5000" />
</set>

动画资源 - push_up_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="5000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" />
</set>

动画资源 - push_up_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%p" android:duration="5000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="5000" />
</set>

动画资源 - slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="-100%" android:toXDelta="0%"/>
<alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

动画资源 - slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

动画资源 - slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

动画资源 - slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="100%"/>
<alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

关于android - 从右向左滑动 Android 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18147840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com