我对Android开发相当新,但我遇到以下问题。我试图以编程方式创建几个ImageViews并使用Picasso加载图像,我偶然发现了以下问题。
编辑:我想要实现的是我希望使用width = FILL_PARENT和height = WRAP_CONTENT
创建ImageView请参阅以下代码摘录:
此示例有效,但不是我想要的(加载图像)
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
ImageView articleTitleImageView = (ImageView) findViewById(R.id.articleTitleImageView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300,300);
params.gravity= Gravity.CENTER_HORIZONTAL;
[...]
ImageView iv = new ImageView(this);
setImage(iv, imageURL);
articleBodyLL.addView(iv, params);
[...]
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(ArticleProvider.getContext())
.load(imgURL)
.fit().centerCrop()
.error(R.drawable.default_image)
.into(imageView);
}
此示例不起作用(图像未显示)
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
ImageView articleTitleImageView = (ImageView) findViewById(R.id.articleTitleImageView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER_HORIZONTAL;
[...]
ImageView iv = new ImageView(this);
setImage(iv, imageURL);
articleBodyLL.addView(iv, params);
[...]
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(ArticleProvider.getContext())
.load(imgURL)
.fit().centerCrop()
.error(R.drawable.default_image)
.into(imageView);
}
答案 0 :(得分:1)
看看这段代码。这很好用。一个建议是,如果您要尝试形成某种列表,请查看RecyclerView以避免例外(许多例外:p)。
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
setImage(iv, "http://square.github.io/picasso/static/sample.png");
articleBodyLL.addView(iv, params);
//////////////////////////////////////////////
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(this)
.load(imgURL)
.error(R.mipmap.ic_launcher)
.into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.e("loaded", "onBitmapLoaded: loaded" );
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}
@Override public void onBitmapFailed(Drawable errorDrawable) {
Log.e("loaded", "onBitmapFailed: load failed" );
}
@Override public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
答案 1 :(得分:0)
myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.VERTICAL);
for (int z = 0; z < zCont.length; z++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
params.gravity= Gravity.LEFT;//CENTER_HORIZONTAL
params.setMargins(-20, 0, 0, -10);
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
setImage(iv, "http://sait.ru/fotota/rua.jpeg");
myLinearLayout.addView(iv, params);
}
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(this)
.load(imgURL)
.fit().centerCrop()
.error(R.mipmap.ic_launcher)
.into(imageView);
}