2015-09-14 22 views
5

Picassoライブラリを使用してURLからビットマップに画像を読み込むのですが、ほとんどの例ではImageViewなどにビットマップを読み込む方法があります。ピカソを使用して画像をビットマップに読み込む

コードは、ドキュメントに従ってこのようにする必要があります。

public void loadImage() { 

     Picasso.with(getBaseContext()).load("image url").into(new Target() { 

      @Override 
      public void onPrepareLoad(Drawable arg0) { 
      } 
      @Override 
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) { 
       Bitmap bitImage = Bitmap(getApplicationContext(),bitmap); 
      } 
      @Override 
      public void onBitmapFailed(Drawable arg0) { 
      } 
     }); 
    } 

しかしBitmap bitImage = Bitmap(getApplicationContext(),bitmap);はi`mは、メソッドの呼び出し予想エラーを取得しているため、正確ではないようです。

答えて

2

あなたが適切にビットマップを作成していないように見えますが、私はあなたの位置にあった場合、私はそうのようなスケールのビットマップを作成します。

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // CREATE A MATRIX FOR THE MANIPULATION 
    Matrix matrix = new Matrix(); 
    // RESIZE THE BIT MAP 
    matrix.postScale(scaleWidth, scaleHeight); 

    // "RECREATE" THE NEW BITMAP 
    Bitmap resizedBitmap = Bitmap.createBitmap(
    bm, 0, 0, width, height, matrix, false); 
    bm.recycle(); 
    return resizedBitmap; 
} 

そして、そのようなImageViewのに設定します。

mImg.setImageBitmap(img); 

全体的に、それは次のようになります。

public void loadImage() { 

    Picasso.with(getBaseContext()).load("image url").into(new Target() { 
      // .... 

      @Override 
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) { 
       // Pick arbitrary values for width and height 
       Bitmap resizedBitmap = getResizedBitmap(bitmap, newWidth, newHeight); 
       mImageView.setBitmap(resizedBitmap); 
      } 

      // .... 
     }); 
    } 
} 

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // CREATE A MATRIX FOR THE MANIPULATION 
    Matrix matrix = new Matrix(); 
    // RESIZE THE BIT MAP 
    matrix.postScale(scaleWidth, scaleHeight); 

    // "RECREATE" THE NEW BITMAP 
    Bitmap resizedBitmap = Bitmap.createBitmap(
    bm, 0, 0, width, height, matrix, false); 
    bm.recycle(); 
    return resizedBitmap; 
} 

しかし、私はあなたがを使用して質問全体は、通常は非常に特殊なケースです。イメージを表示する同じクラスのシングルトンをPicassoと呼ぶべきです。

Picasso.with(mContext) 
    .load("image url") 
    .into(mImageView); 
+0

おかげで、私はすでに(リサイズのための場所でメソッドを持ってhttp://developer.android.com/training/displaying-bitmaps/load-bitmap:通常、これはそうのようAdapter(多分RecyclerViewアダプタ)であります.html)。私はちょうどImageViewに直接ではなく、ビットマップにURLから画像をロードする必要があり、上記のコメントからそれを考え出した。どうもありがとう。 – Alex

+0

なぜ非同期タスクを使用して画像をロードしていますか? .into(mImageView、new Callback <> {...})にコールバックを実装するだけで済みます。 – AndyRoid

+0

実際、リソースフォルダから読み込むのではなく、実際にアダプタ(RecycleView)に渡されるビットマップにイメージを読み込むための簡単な方法を試していました。私はここで説明した問題をデバッグしようとしています。http://stackoverflow.com/questions/32554358/encountering-lag-when-updating-a-cardview-item-in-a-recycleview私はおそらくそれが表示されるクラスでイメージを読み込もうとします。しかし、私はピカソはアクティビティクラスでのみ使用できますが、少なくともそれは私がいくつかの投稿から理解したものです。そして私はContextをAdapterクラスに渡す必要があると思います。 – Alex

関連する問題