28

私はそのようなことをしたいです。

リストビューの画像

これは、ユーザーの名前と画像を含むリスト ビューの行です。

私はいくつかの検索を行い、画像を円形にしましたが、完璧な解決策ではありません. どんな助けも私を助けます。

私のコードが Image Loader クラスに追加されました

public Bitmap processBitmap(Bitmap bitmap) {
    int pixels = 0;
    if (mRound == 0)
        pixels = 120;
    else
        pixels = mRound;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

ありがとう。

4

3 に答える 3

47

この機能も使用できます...私にとってはうまくいきます..

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        (Math.min(((float) targetWidth), 
        ((float) targetHeight)) / 2),
        Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
        new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
        new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}
于 2013-08-22T11:15:00.090 に答える
10

これはおそらくあなたの質問には答えませんが、代わりに、FrameLayoutたとえば2 つの ImageView を使用してその効果を模倣することもできます。中央に円のある四角形、および円の「本体」が透明になるようにします。

これにより、ビットマップ処理を行う必要がなくなります。ただし、ニーズにより適したものを選択してください。

于 2013-08-22T11:14:27.943 に答える
6

返信ありがとうございます。

Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setStrokeWidth(5);
Canvas c = new Canvas(circleBitmap);
 //This draw a circle of Gerycolor which will be the border of image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setAntiAlias(true);
paint.setShader(shader);
// This will draw the image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2-2, paint);

ここに画像の説明を入力

imageLoader を使用した丸みを帯びた画像 github.com/nostra13/Android-Universal-Image-Loader オプションを作成します。

 DisplayImageOptions options = new DisplayImageOptions.Builder()
 // this will make circle, pass the width of image 
.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_‌​menu))) 
.cacheOnDisc(true) 
.build(); 
imageLoader.displayImage(url_for_image,ImageView,options);

または、squre から Picasso Library を使用することもできます。

Picasso.with(mContext).load(URL+b.image)
 .placeholder(R.drawable.profile) 
    .error(R.drawable.profile) 
    .transform(new RoundedTransformation(50, 4)) 
    .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) 
    .centerCrop() 
    .into(v.im_user);

ここからRoundedTrasformationファイルをダウンロードできます

重要: UIL の使用時に 1 つの問題が見つかりました。xml 属性 android:contentDescription="TODO" を ImageView に配置しなかった場合。次に、UIL は簡単なイメージを表示します。すべてが理解できることを願っています

于 2013-08-22T13:32:47.077 に答える