2016-04-14 14 views
-1

CircularImageViewでイメージをロードしようとしていますが、outOfMemoryエラーが発生しています。 私はCircularImageViewのために次のコードを使用しています:CircularImageViewでイメージをロード中にメモリ不足エラーが発生しました

CircularImageView.java

public class CircularImageView extends ImageView { 

    public CircularImageView(Context ctx, AttributeSet attrs) { 
     super(ctx, attrs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     Drawable drawable = getDrawable(); 

     if (drawable == null) { 
      return; 
     } 

     if (getWidth() == 0 || getHeight() == 0) { 
      return; 
     } 
     Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 
     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

     int w = getWidth(), h = getHeight(); 

     Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w); 
     canvas.drawBitmap(roundBitmap, 0, 0, null); 

    } 

    public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { 
     Bitmap 
       finalBitmap; 
     if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) 
      finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, 
        false); 
     else 
      finalBitmap = bitmap; 
     Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), 
       finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
       finalBitmap.getHeight()); 

     paint.setAntiAlias(true); 
     paint.setFilterBitmap(true); 
     paint.setDither(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(Color.parseColor("#BAB399")); 
     canvas.drawCircle(finalBitmap.getWidth()/2 + 0.7f, 
       finalBitmap.getHeight()/2 + 0.7f, 
       finalBitmap.getWidth()/2 + 0.1f, paint); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(finalBitmap, rect, rect, paint); 

     return output; 
    } 

} 

私はデザインでそれを使用して下記のとおり

<com.almabay.almachat.circularImageView.CircularImageView 
    android:id="@+id/img_group" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_alignParentLeft="true" /> 

私は、サーバーから画像を取得し、しようとしていますアダプタを使用してリストビューに表示します。下のコードはここで使用されています:

Picasso.with(context).load(image_url).error(R.drawable.default_avatar).into(viewHolder.imgGroup); 

ここで、image_urlは循環画像ビューで読み込む画像のURLです.ViewHolder.imgGroupはcircularImageViewです。

問題を解決するのを手伝ってください。

+0

[メモリ不足の不具合eをBitmapオブジェクトにロードしている間に発生する](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) –

+0

他の画像を試してみてください..サイズが小さいです..もしそれがまだ関係していれば、 'CircularImageView'をチェックする必要があります –

答えて

1

通常、私はエラーがどこにでもある可能性があり、おそらく漏れていると言います。しかし、あなたはonDrawで2つの新しいビットマップを作っていますか?そして3つはすべて記憶にありますか?それはひどく無駄ですが、あなたの演奏はうまくいくでしょう。あなたはonDrawでnewを使うことを避け、ビットマップの割り当てをずっと少なくする必要があります。

これを投げて完全に書き換えます。あなたはここでdrawableを扱うべきではありません(とにかくビットマップであると仮定しているので、特に悪い考えです)、ビットマップのファイル名やリソースIDを取得して、1のcreateBitmapコマンドでそれを読み込み/スケールする必要があります。 BitmapOptionsを使用します。ビットマップが設定され、結果を保存するときにこれを行う必要があります。常に呼び出されるonDrawで拡大/縮小しないでください。ポッターダフモードと2番目のキャンバスを使用するのではなく、円形のクリッピングパスを使用して3番目のビットマップを回避できます。あなたはこれを1ビットマップだけで行うことができます。

2

ピカソ画像変換を使用しない理由画像の読み込みにピカソを使用しています。

ImageView im = (ImageView) findViewById(R.id.img1); 

    Picasso.with(MainActivity2.this).load(R.drawable.kitten) 
      .transform(new CropCircleTransformation()).into(im); 

、それはあなただけ

import com.squareup.picasso.Transformation; 

import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.Paint; 

public class CropCircleTransformation implements Transformation { 

    @Override public Bitmap transform(Bitmap source) { 
    int size = Math.min(source.getWidth(), source.getHeight()); 

    int width = (source.getWidth() - size)/2; 
    int height = (source.getHeight() - size)/2; 

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(bitmap); 
    Paint paint = new Paint(); 
    BitmapShader shader = 
     new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
    if (width != 0 || height != 0) { 
     // source isn't square, move viewport to center 
     Matrix matrix = new Matrix(); 
     matrix.setTranslate(-width, -height); 
     shader.setLocalMatrix(matrix); 
    } 
    paint.setShader(shader); 
    paint.setAntiAlias(true); 

    float r = size/2f; 
    canvas.drawCircle(r, r, r, paint); 

    source.recycle(); 

    return bitmap; 
    } 

    @Override public String key() { 
    return "CropCircleTransformation()"; 
    } 
} 

複数の変換は、我々がここにhttps://github.com/wasabeef/picasso-transformations

を見つけることができます変換クラスを追加する必要があり、この

enter image description here

のように見える - :それは実装するのは非常に簡単です

+0

CircularImageView.javaのコードを確認してください。この行はビットマップです。bitmap = b.copy(Bitmap.Config.ARGB_8888、true);私に例外を与えている。 –

+0

この変換クラスを使用してください。https://github.com/wasabeef/picasso-transformations/blob/master/transformations/src/main/java/jp/wasabeef/picasso/transformations/CropCircleTransformation.java –

関連する問題