我是android领域的新手。我正在定期在main.xml中心显示两个图像。代码运行成功,但没有显示任何图像。这是代码。
package com.example.radio;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
public class Radio extends Activity
{
private ImageView _imagView;
private Timer _timer;
private int _index;
private MyHandler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
handler= new MyHandler();
_imagView=(ImageView) findViewById(R.id.speak);
_index=0;
_timer= new Timer();
_timer.schedule(new TickClass(), 500, 200);
}
private class TickClass extends TimerTask
{
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(_index);
_index++;
}
}
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
try {
Bitmap bmp= BitmapFactory.decodeStream(Radio.this.getAssets().open("drum_"+_index+".png"));
_imagView.setImageBitmap(bmp);
Log.v("Loaing Image: ",_index+"");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Exception in Handler ",e.getMessage());
}
}}}
答案 0 :(得分:0)
我在这里发布了一个示例代码。您可以像这样使用>>
public void onCreate() {
ImageView backgroundImageView = findViewById(R.id.background);
backgroundImageView.postDelayed(new Runnable() {
static int i = 0;
public void run() {
ImageView.this.setImageResource(
i++ % 2 == 0 ?
R.drawable.background_image1 :
R.drawable.background_image2);
ImageView.this.postDelayed(this, 1000);
}
}, 1000);
}
答案 1 :(得分:0)
您可以在应用程序中使用以下示例代码并替换drawables ...它可以满足您的要求......
int cargallery[] = { R.drawable.ford_suv_black,
R.drawable.lincoln_town_car_black };
int carIndex = -1;
public static final int UPDATE_IMAGE = 1000;
ImageView carimage = null;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == UPDATE_IMAGE) {
if (carIndex >= cargallery.length - 1) {
carIndex = -1;
}
carIndex++;
carimage.setImageResource(cargallery[carIndex]);
reloadCarImage();
}
};
};
private void reloadCarImage() {
Message msg = new Message();
msg.what = UPDATE_IMAGE;
mHandler.sendMessageDelayed(msg, 1000);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
carimage = (ImageView) findViewById(R.id.carview);
reloadCarImage();
}
答案 2 :(得分:0)
int []imageArray={R.drawable.imgone,R.drawable.imgtwo,R.drawable.imgthree};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 100); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
}