2013-10-23 17 views
7

最近、ImageViewから拡張してCircularImageViewクラスを作成しました。これは、渡されたキャンバスに描くことにより、onDraw(キャンバス)メソッドを介して行われます:描画可能ビットマップを介して画像を設定する際カスタムImageViewクラスがピカソ画像ダウンロードライブラリと連携していません

//load the bitmap 
    loadBitmap(); 

    // init shader 
    if(image !=null) 
    { 
     shader = new BitmapShader(Bitmap.createScaledBitmap(image, viewWidth + (borderWidth * 2), viewHeight + (borderWidth * 2), true), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
     paint.setShader(shader); 

     int circleCenter = viewWidth/2; 

     // circleCenter is the x or y of the view's center 
     // radius is the radius in pixels of the cirle to be drawn 
     // paint contains the shader that will texture the shape 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder); 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paintBackground); 
     canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint); 
    } 

だから、このビットは動作します。私はそれを拡張して、GoogleのVolley NetworkImageViewでも使用できるようにしました。

私の問題は、ピカソ画像のダウンロードライブラリと一緒にCircularImageViewクラスを試してみると、私はVolleyの代わりにそれを見ています。何が起こるかは、BitmapDrawableを取得するときの最初の行のloadBitmap()関数のClassCastExceptionです。

private void loadBitmap() 
{ 
    BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable(); 

    if(bitmapDrawable != null) 
     image = bitmapDrawable.getBitmap(); 
} 

ピカソが画像をダウンロードする前に、最初にプレースホルダ画像を丸めます。しかしPicassoによって画像がダウンロードされるとすぐに、BitmapDrawableではなくgetDrawable()が返すようにClassCastExceptionが発生し、PicassoDrawableが失敗します。

CircularImageViewクラスのonDraw(canvas)メソッドのイメージを丸くする作業をしたいのですが、ImageViewをピカソでImageViewを設定する際に、 。これは可能ですか?

ありがとうございます。

答えて

14

ピカソを使用した円形画像の場合、変換を実装するthisクラスを使用してください。

Picasso.with(context).load(url).transform(new RoundedTransformation(radius, margin)).into(imageview); 
+0

これにより、毎回新しいビットマップが作成されます。 – secureboot

+0

DesertIvy、droidx、Jake Whartonに感謝します。これが目的だったプロジェクトは保留になり、問題に直ちに戻ります。私はちょうど完璧な作品で上記を試した。 –

5

ピカソでこれをやったとき、あなたがいずれかをする必要があります

  1. はカスタムImageViewサブクラスでシェーダでキャンバスを丸めたビットマップがメモリにキャッシュされるようにTransformationとして丸めを適用するか、
  2. クランプ。 ImageViewのうち、基礎となるBitmapを引くしようとすると、この技術の詳細ragin' cagin'で概説されたロマン・ガイon his blog

はアンチパターンです。 Bitmapへのアクセスが必要な場合は、Targetを実装して、onBitmapSuccessコールバックが提供するビューに実装してください。

関連する問題