我在Java编程方面全新。我为Android制作了一个应用程序,根据IOIO板上4个数字输入的组合显示16张可信图片中的1张。我使用IF句子做了这个(也许这不是最好的方式但是有效),现在我的问题是:我想要不仅显示一张图片而且要显示图片(4或5)的图像以显示动画。
这样的事情:
if ((((val1 == false)&& (val2 == true)) && (val3 == true)) && (val4 == true)){
imagen.setImageResource(R.drawable.in1);
Thread.sleep(100);
imagen.setImageResource(R.drawable.in2);
Thread.sleep(100);
imagen.setImageResource(R.drawable.in3);
Thread.sleep(100);
imagen.setImageResource(R.drawable.in4);
Thread.sleep(100);
}
我尝试:
Thread.sleep(100);
但是当我尝试编译时出错了 也可以试试:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但该应用程序不起作用。任何消化???
这是我的应用的代码:
package com.example.mycar;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends IOIOActivity {
boolean val1, val2, val3, val4;
ImageView imagen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen = (ImageView) findViewById(R.id.estado);
}
class Looper extends BaseIOIOLooper{
DigitalInput entrada1, entrada2, entrada3, entrada4;
@Override
protected void setup() throws ConnectionLostException,
InterruptedException {
// TODO Auto-generated method stub
entrada1 = ioio_.openDigitalInput(37, DigitalInput.Spec.Mode.PULL_UP);
entrada2 = ioio_.openDigitalInput(38, DigitalInput.Spec.Mode.PULL_UP);
entrada3 = ioio_.openDigitalInput(39, DigitalInput.Spec.Mode.PULL_UP);
entrada4 = ioio_.openDigitalInput(40, DigitalInput.Spec.Mode.PULL_UP);
}
@Override
public void loop() throws ConnectionLostException, InterruptedException {
// TODO Auto-generated method stub
val1 = entrada1.read();
val2 = entrada2.read();
val3 = entrada3.read();
val4 = entrada4.read();
mostrarentradas(val1, val2, val3, val4);
Thread.sleep(20);
}
}
@Override
protected IOIOLooper createIOIOLooper() {
// TODO Auto-generated method stub
return new Looper();
}
private void mostrarentradas(final boolean val1, final boolean val2, final boolean val3,
final boolean val4) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
if ((((val1 == false)&& (val2 == true)) && (val3 == true)) && (val4 == true)){
imagen.setImageResource(R.drawable.in1);
}
if ((((val1 == false)&& (val2 == false)) && (val3 == true)) && (val4 == true)){
imagen.setImageResource(R.drawable.in8);
}
if ((((val1 == false)&& (val2 == false)) && (val3 == false)) && (val4 == true)){
imagen.setImageResource(R.drawable.in11);
}
if ((((val1 == false)&& (val2 == false)) && (val3 == false)) && (val4 == false)){
imagen.setImageResource(R.drawable.in14);
}
if ((((val1 == false)&& (val2 == true)) && (val3 == false)) && (val4 == true)){
imagen.setImageResource(R.drawable.in16);
}
if ((((val1 == false)&& (val2 == true)) && (val3 == true)) && (val4 == false)){
imagen.setImageResource(R.drawable.in22);
}
if ((((val1 == false)&& (val2 == false)) && (val3 == true)) && (val4 == false)){
imagen.setImageResource(R.drawable.in25);
}
if ((((val1 == false)&& (val2 == true)) && (val3 == false)) && (val4 == false)){
imagen.setImageResource(R.drawable.in29);
}
if ((((val1 == true)&& (val2 == false)) && (val3 == true)) && (val4 == true)){
imagen.setImageResource(R.drawable.in2);
//Thread.sleep(100);
imagen.setImageResource(R.drawable.in3);
}
if ((((val1 == true)&& (val2 == false)) && (val3 == false)) && (val4 == true)){
imagen.setImageResource(R.drawable.in18);
}
if ((((val1 == true)&& (val2 == false)) && (val3 == true)) && (val4 == false)){
imagen.setImageResource(R.drawable.in20);
}
if ((((val1 == true)&& (val2 == false)) && (val3 == false)) && (val4 == false)){
imagen.setImageResource(R.drawable.in27);
}
if ((((val1 == true)&& (val2 == true)) && (val3 == false)) && (val4 == true)){
imagen.setImageResource(R.drawable.in4);
}
if ((((val1 == true)&& (val2 == true)) && (val3 == false)) && (val4 == false)){
imagen.setImageResource(R.drawable.in30);
}
if ((((val1 == true)&& (val2 == true)) && (val3 == true)) && (val4 == false)){
imagen.setImageResource(R.drawable.in5);
}
if ((((val1 == true)&& (val2 == true)) && (val3 == true)) && (val4 == true)){
imagen.setImageResource(R.drawable.vwg0);
}
}
}
);
}
}
答案 0 :(得分:0)
可能会使用postDelayed
方法和Runnable
吗?为它创建分离的线程对此来说太多了,也无法更新UI外部的线程
imagen.postDelayed(new Runnable(){
@Override
public void run(){
imagen.setImageResource(R.drawable.in1);
}
}, 1000); //1000ms delay
在run
内{p>您可能会再次设置postDelayed
方法