gpt4 book ai didi

java - 在android中如何处理Runnable Handler类

转载 作者:行者123 更新时间:2023-11-30 02:19:51 25 4
gpt4 key购买 nike

在我的安卓代码中,我的要求是:--当我单击按钮时,它会显示一个动画。这个动画将运行 4 秒。然后它将转到另一个 Activity 。

我的代码是:--

public class Animation_test extends Activity {

private Handler handler;
Context context;

private TransparentProgressDialog pd;
Button btnOpenPopup;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_test);
btnOpenPopup = (Button)findViewById(R.id.btn1);
buttonperform();
handler = new Handler();
pd = new TransparentProgressDialog(this, R.drawable.uktrafficlights);


}


public void buttonperform(){

btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
new Thread(new Task()).start();
}

});
}


class Task implements Runnable {
@Override
public void run() {

handler.post(new Runnable() {
@Override
public void run() {
pd.show();

}
Intent intent = new Intent(context, secondActivity.class);
startActivity(intent);
});
}
}


@Override
protected void onDestroy() {
//handler.removeCallbacks(new Ru);
if (pd.isShowing() ) {
pd.dismiss();
}
super.onDestroy();
}

private class TransparentProgressDialog extends Dialog {

private ImageView iv;

public TransparentProgressDialog(Context context, int resourceIdOfImage) {
super(context, R.style.TransparentProgressDialog);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
iv = new ImageView(context);
iv.setImageResource(resourceIdOfImage);
layout.addView(iv, params);
addContentView(layout, params);
}


@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
//anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(4000);
iv.setAnimation(anim);
iv.startAnimation(anim);

}
}


}

但我的代码只显示动画..问题出在哪里???

最佳答案

不需要处理程序和线程来实现您想要实现的目标。动画有一个 Listener Animation.AnimationListener,具有三个回调。

  1. onAnimationStart
  2. 动画结束
  3. onAnimationRepeat

您可以为您的动画注册监听器,当 onAnimationEnd 被调用时,您可以开始您的 Activity 。例如

anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(Animation_test.this, secondActivity.class);
startActivity(intent);
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});

关于java - 在android中如何处理Runnable Handler类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760269/

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