我有这个ImageView,它显示来自URL的图片。我希望有一个按钮,单击该按钮将显示URL中的图像。谁能教我怎么做?
这是我的代码:
String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
loadImageFromUrl(url);
}
private void loadImageFromUrl(String url) {
Picasso.with(this) .load(url) .placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
}
});
}

毕加索:
compile 'com.squareup.picasso:picasso:2.5.2'

答案 0 :(得分:0)
点击视图时调用。
MyPassword
答案 1 :(得分:0)
在xml文件中添加一个按钮。让它与id:btn_load;
在活动内添加
Button btn = findViewById(R.id.btn_load);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadImageFromUrl(url);
}
});
答案 2 :(得分:0)
在xml布局文件中创建一个按钮。然后在onCreate调用:
final String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadImageFromUrl(url);
}
});
}
注意:要使其工作,您必须将您的url String声明为 final ,因为您在匿名类(OnClickListener)中使用它。
注意2:您拥有的图片网址可能无效。请改用此https://www.enterprise.co.uk/content/dam/global-vehicle-images/cars/VAUX_INSI_2014.png