我需要一个用于我的android应用的功能,以便每分钟自动刷新cctv图像的图像。我正在使用Picasso拍摄图像,在该网站上找不到解决方法。
在我的Activity2.java中:
public class Activity2 extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
{
ImageView imageView = findViewById(R.id.image_view);
String url = "http://www.trakyagumruk.gov.tr/canli_trakya_gtb/kapikule/kapikule_images/kapikule_kamera1/1.jpg";
Picasso.get().load(url).into(imageView);
}
{
ImageView imageView1 = findViewById(R.id.image_view1);
String url = "http://trakyagumruk.gov.tr/canli_trakya_gtb/kapikule/kapikule_images/kapikule_kamera6/1.jpg";
Picasso.get().load(url).into(imageView1);
}
button = (Button) findViewById(R.id.restart_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
finish();
}
});
}
答案 0 :(得分:0)
使用Handler
:
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
// download your images here
handler.postDelayed(this, 60000);
}
}, 60000); // 1 minute in ms
编辑: 另外,将此行添加到两个Picasso加载器中:
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
它将确保加载图像而不将其存储在缓存中
答案 1 :(得分:0)
您可以使用它每分钟(即60,000ms)用新图像更新imageView
int refreshIntervalMs = 60*1000; //milliseconds
final Handler handler = new Handler(); // make sure to do this on main thread.
String url = "http://www.trakyagumruk.gov.tr/canli_trakya_gtb/kapikule/kapikule_images/kapikule_kamera1/1.jpg";
Runnable imageUpdater = new Runnable() {
@Override
public void run() {
Picasso.with(this).cancelRequest(imageView); //cancel any pending/ongoing requests
Picasso.with(this)
.load(url)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
handler.postDelayed(this, refreshIntervalMs);
}
};
使用以下命令开始/继续连续更新imageView。
handler.post(imageUpdater);
使用以下内容停止更新。
handler.removeCallbacksAndMessages(null);