一张一张地设置图像
Set Images one by one
我是开发新手所以想问这个问题,如有冒犯请见谅
我有 6 个不同的 ImageView
s 和 6 张图像,我如何将图像设置为 ImageView
s 以便它们在点击时一张一张地出现,但有一些延迟,比如 1 秒?现在他们同时出现
private int[] images;
private int[] containers;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new int[] {R.drawable.wale,
R.drawable.btrfly,
R.drawable.cat,
R.drawable.dog,
R.drawable.fish,
R.drawable.dolphin};
containers = new int[] {R.id.container1,
R.id.container2,
R.id.container3,
R.id.container4,
R.id.container5,
R.id.container6};
for (int i = 0; i<containers.length; i++) {
final ImageView temp = findViewById(containers[i]);
temp.setImageResource(R.drawable.placeholder);
}
Button load = findViewById(R.id.btnLoad);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for ( int i =0; i<containers.length; i++) {
final ImageView temp = findViewById(containers[i]);
temp.setImageResource(images[i]);
}
}
});
}
您可以像这样使用 postDelayed()
或 android.os.Handler:
final int baseDelay = 1000;
final Handler handler = new Handler();
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < containers.length; i++) {
final int ifinal = i;
handler.postDelayed(new Runnable(){
public void run(){
final ImageView temp = findViewById(containers[ifinal]);
temp.setImageResource(images[ifinal]);
}
}, i * baseDelay);
}
}
});
我是开发新手所以想问这个问题,如有冒犯请见谅
我有 6 个不同的 ImageView
s 和 6 张图像,我如何将图像设置为 ImageView
s 以便它们在点击时一张一张地出现,但有一些延迟,比如 1 秒?现在他们同时出现
private int[] images;
private int[] containers;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new int[] {R.drawable.wale,
R.drawable.btrfly,
R.drawable.cat,
R.drawable.dog,
R.drawable.fish,
R.drawable.dolphin};
containers = new int[] {R.id.container1,
R.id.container2,
R.id.container3,
R.id.container4,
R.id.container5,
R.id.container6};
for (int i = 0; i<containers.length; i++) {
final ImageView temp = findViewById(containers[i]);
temp.setImageResource(R.drawable.placeholder);
}
Button load = findViewById(R.id.btnLoad);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for ( int i =0; i<containers.length; i++) {
final ImageView temp = findViewById(containers[i]);
temp.setImageResource(images[i]);
}
}
});
}
您可以像这样使用 postDelayed()
或 android.os.Handler:
final int baseDelay = 1000;
final Handler handler = new Handler();
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < containers.length; i++) {
final int ifinal = i;
handler.postDelayed(new Runnable(){
public void run(){
final ImageView temp = findViewById(containers[ifinal]);
temp.setImageResource(images[ifinal]);
}
}, i * baseDelay);
}
}
});