Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

bitmap - How to make an image fit into a circular frame in android

I have a ListView in which there is an ImageView, the image in the ImageView gets loaded dynamically after its fetched from the server. Now, I want these images, of any size, to fit into a circular frame, how to do that? Here's a sample pic of what I want

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With the help of previous answer I came up with this solution.Hope it help others:

import android.app.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;



public class CircleImage extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.circle_layout);
    ImageView img1 = (ImageView) findViewById(R.id.imageView1);
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
            R.drawable.hair_four);
    Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
    Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
    img1.setImageBitmap(conv_bm);
    // TODO Auto-generated method stub
}

public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
    Bitmap result = null;
    try {
        result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);

        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 200, 200);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(50, 50, 50, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

    } catch (NullPointerException e) {
    } catch (OutOfMemoryError o) {
    }
    return result;
}

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...