作者:米年爱秋天 | 来源:互联网 | 2023-05-18 17:15
我目前正在开发一个Android应用程序.刚开始,我能够实现我的启动画面.但是,我不喜欢它和主要活动之间的过渡.我希望启动画面淡出并主要淡入.看起来它们混合在一起,因为我有两个相同的背景图像.做了一些研究,但还没有找到正确的答案.下面,我发布了我的代码.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class Splash_screen extends Activity {
private Thread mSplashThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
final Splash_screen sPlashScreen = this;
mSplashThread = new Thread(){
@Override
public void run(){
try {
synchronized(this){
wait(3000);
}
}
catch(InterruptedException ex){
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
MainActivity类
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
随意删除此任务不需要的任何类或文件.谢谢
1> Yograj Shind..:
您可以使用两个.xml文件淡入新活动并淡出当前活动.
fade_in.xml
fade_out.xml
在代码中使用它:(在您的活动中)
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
上面的代码将淡出当前活动的Activity并淡入新启动的Activity.