Android中setPadding()之后的等待
我有以下问题:我的应用程序中有一个图像,位置将动态变化。以下代码已起作用:Android中setPadding()之后的等待,android,Android,我有以下问题:我的应用程序中有一个图像,位置将动态变化。以下代码已起作用: RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); image.setLayoutParams(layoutParams); image.setPaddi
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
image.setLayoutParams(layoutParams);
image.setPadding(startX, startY, 0, 0);
image.setVisibility(View.VISIBLE);
for (int i=0; i<10; i++) {
startX+=10;
image.setPadding(startX, startY, 0, 0);
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
image.setLayoutParams(layoutParams);
设置填充(startX,startY,0,0);
image.setVisibility(View.VISIBLE);
对于(int i=0;i您可以使用Handler
和Runnable
的组合来实现此行为:
步骤1:定义处理程序并运行
Handler handler = new Handler();
Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
refreshView();
}
}
步骤2:以下是您的refreshView功能:
private void refreshView() {
count ++; //Variable initialized at 0 in your class
if (count < 10) {
startX += 10;
dotGreen.setPadding(startX, startY, 0, 0);
dotGreen.invalidate();
handler.postDelayed(refreshRunnable, 1000);
}
}
private void refreshView(){
count++;//变量在类中初始化为0
如果(计数<10){
startX+=10;
设置填充(startX,startY,0,0);
dotGreen.invalidate();
handler.postDelayed(refreshRunnable,1000);
}
}
有关格式化代码的信息,请参阅。嗯,我得到了“在行中找不到符号变量runnable”处理程序。postDelayed(runnable,1000)