gpt4 book ai didi

java - Android:加载应用程序时添加动画

转载 作者:行者123 更新时间:2023-12-02 06:01:11 25 4
gpt4 key购买 nike

我有一个动画,每当单击 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:toYScale="1.05"
android:pivotX="50%"
android:pivotY="50%"
android:duration="180"
/>

我该如何修复它,以便在加载应用程序时启动动画一次,只是为了“显示 ImageButton 是可点击的”?

这是 ImageButton 代码:

    final ImageButton a = (ImageButton) findViewById(R.id.imageNew);
a.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
a.startAnimation(anim);

Intent intent = new Intent(MainActivity.this,NewActivity.class);
startActivity(intent);
}
});

最佳答案

您无法在 oncreate 中运行动画,因为它尚未附加到 View ,因此要运行启动动画,请重写此方法。

  @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>

Android 文档明确指出要使用覆盖上述方法的启动动画,而不是 ONSTART() 或 ONCREATE()!

@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);
}
}

关于java - Android:加载应用程序时添加动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668396/

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