Java Android:加载应用程序时添加动画
我有一个动画,每当单击ImageButton时,它都会在ImageButton上产生一个小的“弹出”效果:Java Android:加载应用程序时添加动画,java,android,android-animation,imagebutton,Java,Android,Android Animation,Imagebutton,我有一个动画,每当单击ImageButton时,它都会在ImageButton上产生一个小的“弹出”效果: <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.9" android:toXScale="1.05" android:fromYScale="0.9" android:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.9"
android:toXScale="1.05"
android:fromYScale="0.9"
android:toYScale="1.05"
android:pivotX="50%"
android:pivotY="50%"
android:duration="180"
/>
这很简单。在活动的onStart()函数中显示动画。加载所有资源和视图时调用OnStart()函数。您不能在oncreate中运行动画,因为它尚未附加到视图,因此要运行启动动画,请覆盖此方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
RunAnimations();
super.onWindowFocusChanged(hasFocus);
}
在你的xml动画中试试这个
android:repeatCount="0"
我不确定是否需要为缩放设置oneshot,但我的一次性启动动画列表如下所示:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/artsun" android:duration="100"/>
<item android:drawable="@drawable/artsun2" android:duration="100"/>
<item android:drawable="@drawable/artsun3" android:duration="100"/>
<item android:drawable="@drawable/artsun4" android:duration="100"/>
</animation-list>
全班如下
public class Splash extends Activity {
/** Called when the activity is first created. */
//MediaPlayer mpSplash;
AnimationDrawable logoAnimation;
ImageView logoImage;
ProgressBar progressBar1;
View ticker;
ImageView gplay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
progressBar1=(ProgressBar)findViewById(R.id.progressBar1);
progressBar1.setVisibility(View.INVISIBLE);
gplay=(ImageView) findViewById(R.id.gplay);
ticker = (View) findViewById(R.id.ticker);
ticker.setFocusable(true);
ticker.requestFocus();
ticker.setSelected(true);
this.gplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.gmaninc.dopewars"));
startActivity(browserIntent);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//mpSplash.release();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//mpSplash.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//mpSplash.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
RunAnimations();
super.onWindowFocusChanged(hasFocus);
}
private void RunAnimations() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.fade);
/* a.reset();
logoImage = (ImageView) findViewById(R.id.dopescrawl);
logoImage.setBackgroundResource(R.drawable.dopesplash);
logoAnimation = (AnimationDrawable) logoImage.getBackground();
logoImage.clearAnimation();
logoImage.startAnimation(a);
*/
a = AnimationUtils.loadAnimation(this, R.anim.slide);
a.reset();
ImageView title = (ImageView) findViewById(R.id.dopescrawl);
title.clearAnimation();
title.startAnimation(a);
//logoAnimation.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
progressBar1.setVisibility(View.VISIBLE);
progressBar1.setIndeterminate(true);
startActivity(new Intent("com.gmaninc.dopewarsfree.MG"));
return super.onTouchEvent(event);
}
}
谢谢你能告诉我在我的代码中应该在哪里插入新代码吗?是的,我编辑了文章的底部。希望你能理解,这是我的启动动画代码逐字逐句。如果您想查看整个活动,我可以向您展示一个小问题,每次应用程序打开和关闭新活动时,动画都会启动?您是否重新启动相同的活动?或者它们是独立的活动?你在不同的活动中有相同的动画?是的,它会重新启动相同的活动,有没有办法让它只在应用程序第一次打开时启动?
@Override
public void onWindowFocusChanged(boolean hasFocus) {
RunAnimations();
super.onWindowFocusChanged(hasFocus);
}
can go anywhere in your main activity, then just create the run animations method and in it put anything you want to start
private void RunAnimations() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.fade);
/* a.reset();
logoImage = (ImageView) findViewById(R.id.dopescrawl);
logoImage.setBackgroundResource(R.drawable.dopesplash);
logoAnimation = (AnimationDrawable) logoImage.getBackground();
logoImage.clearAnimation();
logoImage.startAnimation(a);
*/
a = AnimationUtils.loadAnimation(this, R.anim.slide);
a.reset();
ImageView title = (ImageView) findViewById(R.id.dopescrawl);
title.clearAnimation();
title.startAnimation(a);
//logoAnimation.start();
}
public class Splash extends Activity {
/** Called when the activity is first created. */
//MediaPlayer mpSplash;
AnimationDrawable logoAnimation;
ImageView logoImage;
ProgressBar progressBar1;
View ticker;
ImageView gplay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
progressBar1=(ProgressBar)findViewById(R.id.progressBar1);
progressBar1.setVisibility(View.INVISIBLE);
gplay=(ImageView) findViewById(R.id.gplay);
ticker = (View) findViewById(R.id.ticker);
ticker.setFocusable(true);
ticker.requestFocus();
ticker.setSelected(true);
this.gplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.gmaninc.dopewars"));
startActivity(browserIntent);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//mpSplash.release();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//mpSplash.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//mpSplash.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
RunAnimations();
super.onWindowFocusChanged(hasFocus);
}
private void RunAnimations() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.fade);
/* a.reset();
logoImage = (ImageView) findViewById(R.id.dopescrawl);
logoImage.setBackgroundResource(R.drawable.dopesplash);
logoAnimation = (AnimationDrawable) logoImage.getBackground();
logoImage.clearAnimation();
logoImage.startAnimation(a);
*/
a = AnimationUtils.loadAnimation(this, R.anim.slide);
a.reset();
ImageView title = (ImageView) findViewById(R.id.dopescrawl);
title.clearAnimation();
title.startAnimation(a);
//logoAnimation.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
progressBar1.setVisibility(View.VISIBLE);
progressBar1.setIndeterminate(true);
startActivity(new Intent("com.gmaninc.dopewarsfree.MG"));
return super.onTouchEvent(event);
}
}