程序化图像视图和毕加索

Programmatic imageview and picasso

我是 Android 开发的新手,但遇到以下问题。我正在尝试以编程方式创建多个 ImageView 并使用 Picasso 加载图像,但我无意中发现了以下问题。

编辑:我想要实现的是我希望使用 width=FILL_PARENT 和 height=WRAP_CONTENT[=12 创建 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);
}

看看这段代码。这很好用。一个建议是,如果您试图形成某种列表,请查看 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) {

              }
            });
      }
    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);
            }