Android 安卓:从底部延时按下按钮
在我的应用程序中,我得到了一个Android 安卓:从底部延时按下按钮,android,animation,button,Android,Animation,Button,在我的应用程序中,我得到了一个活动,它从数据库接收一定数量的条目。对于每个条目,都会制作一个按钮并添加到布局中。我希望按钮从下到上“按入”。我通过稍微编辑API示例push\u-up\u in.xml实现了这一点。现在令人不安的是,看起来所有的按钮都是同时按下的。每次按下按钮,我都会有一点延迟,以便看起来更“异步” 到目前为止,相关代码如下所示 public void onCreate(Bundle icicle){ super.onCreate(icicle); getDest
活动
,它从数据库接收一定数量的条目。对于每个条目,都会制作一个按钮并添加到布局中。我希望按钮从下到上“按入”。我通过稍微编辑API示例push\u-up\u in.xml
实现了这一点。现在令人不安的是,看起来所有的按钮都是同时按下的。每次按下按钮
,我都会有一点延迟,以便看起来更“异步”
到目前为止,相关代码如下所示
public void onCreate(Bundle icicle){
super.onCreate(icicle);
getDestinationLayout();
setContentView(linearLayout);
challengeName = getIntent().getExtras().getString("challengeName");
context = this;
datasource = new CustomDataSource(this);
showGames();
}
(...)
private void getDestinationLayout(){
linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main_all_entries, null);
scrollView = (ScrollView) linearLayout.findViewById(R.id.rel_all_football);
buttonHolder = (LinearLayout) scrollView.findViewById(R.id.lin_all_buttonholder);
}
// is called from showGames() after taking fetched entries, sorted them and so on
private void createAndAddButtons(){
// the fetched entries are taken in another method
for(int i=0; i<fetchedEntries.size(); i++){
(1)
entryButton = new Button(this) //entryButton is global
// do some layout stuff
entryButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));
(2)
buttonHolder.add(entryButton);
}
}
(虽然getDestinationLayout()
在showGames()
中),但问题是我首先添加了按钮,然后调用setContentView(linearLayout)
(我现在认为接近我的目标确实是错误的),但正如前面所说的,实际版本也不是我想要的。我怎样才能做到这一点?显然,问题在于使用线程。sleep(x)
,但另一方面,我不完全理解为什么,因为从逻辑上讲,这个过程应该是
->setContentView(linearLayout)
=>活动显示在屏幕上
->循环
->线程。睡眠(x)
=>程序等待x毫秒
->创建和添加按钮=>按钮i按下
重复直到结束
编辑:
下面是动画xml文件“push_up_in.xml”
我不确定,但我认为使用
可运行的处理程序将解决您的问题
private void createAndAddButtons(){
Handler h = new Handler();
//h.post(
h.postDelayed(new Runnable(){
public void run() {
// the fetched entries are taken in another method
for(int i=0; i<fetchedEntries.size(); i++){
entryButton = new Button(this) //entryButton is global
// do some layout stuff
entryButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));
buttonHolder.add(entryButton);
}
}
}, 3000);
}
我不确定,但我认为使用Runnable
的处理程序将解决您的问题
private void createAndAddButtons(){
Handler h = new Handler();
//h.post(
h.postDelayed(new Runnable(){
public void run() {
// the fetched entries are taken in another method
for(int i=0; i<fetchedEntries.size(); i++){
entryButton = new Button(this) //entryButton is global
// do some layout stuff
entryButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));
buttonHolder.add(entryButton);
}
}
}, 3000);
}
但是(我认为乍一看),在添加按钮之间没有延迟,对吗?将该处理程序放在for循环中会更好吗?或者从foor循环中的处理程序调用一些延迟函数?编辑:现在请阅读您的编辑,谢谢您的澄清!好的,我完全按照你的建议做了,我从onCreate获取所有条目,从onStart()调用createAndAddButtons()。我在上面的答案中放置了一个类似的处理程序,在foor循环中放置了Thread.sleep(),但问题仍然存在。为了测试它,我放了一个Thread.sleep(1000),因为在我的测试数据库中有15个条目,所以在显示任何内容之前它会休眠15秒。15秒后,活动显示,按钮“一起”按下。我忽略的一件事是什么?如果不使用处理程序的post方法,而是使用postDelayed方法,会发生什么?请参阅编辑并注意在开始完整循环之前的3000秒延迟。好的,我们正在接近:-)将h.post(…)替换为h.postDelayed(…)确实具有显示主活动的效果,并且在3000毫秒后按钮被按下!因此,我在for循环中放置了一个线程。sleep(250)与之前的效果相同(250,因为如果我的意图有效与否,那么它真的是可见的),似乎睡眠时间的总量总计为一次睡眠,然后按下按钮,我也尝试将第二个处理程序。postdayed(…)放入for循环中(一次在for循环外实例化处理程序,一次在for循环外定义处理程序h2,并在每次迭代中实例化一个新的处理程序),但没有期望的效果,但是(我认为第一眼就知道),在单个添加按钮之间没有延迟,对吗?将该处理程序放在for循环中更好吗?或者从foor循环中的处理程序调用一些延迟函数更好吗?编辑:现在请阅读您的编辑,感谢您的澄清!好的,我完全按照您的建议做了,我从onCreate和onS获取所有条目tart()我调用createAndAddButtons()。在那里我放置了一个类似于上面答案中的处理程序,在foor循环中我放置了Thread.sleep(),但问题仍然存在。为了测试它,我放置了一个Thread.sleep(1000)在中,由于在我的测试数据库中有15个条目,所以在显示任何内容之前,它会休眠15秒。在这15秒之后,Activitiy会显示,并且按钮会“一起”按下。我可能忽略了什么?如果不使用处理程序的post方法而使用POSTDELIED方法会发生什么?请参见编辑并注意在整个循环开始之前的3000秒延迟。好的,我们越来越接近:-)用h.POSTDELIED(…)替换h.post(…)确实具有显示主活动的效果,3000毫秒后,按钮被按下!因此,我在for循环中放置了一个线程。sleep(250)与之前的效果相同(250,因为如果我的意图有效与否,那么它真的是可见的),似乎睡眠时间的总量总计为一次睡眠,然后按下按钮,我也尝试将第二个处理程序。postdayed(…)放入for循环中(一次在for循环外部实例化处理程序,一次在外部定义处理程序h2,并在每次迭代中实例化一个新的处理程序),但没有期望的效果
private void createAndAddButtons(){
Handler h = new Handler();
//h.post(
h.postDelayed(new Runnable(){
public void run() {
// the fetched entries are taken in another method
for(int i=0; i<fetchedEntries.size(); i++){
entryButton = new Button(this) //entryButton is global
// do some layout stuff
entryButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));
buttonHolder.add(entryButton);
}
}
}, 3000);
}
private void getDestinationLayout(){
setContentView(R.layout.main_all_entries);
scrollView = (ScrollView) linearLayout.findViewById(R.id.rel_all_football);
buttonHolder = (LinearLayout) scrollView.findViewById(R.id.lin_all_buttonholder);
}