3

我创建了一个圆形ImageView,但我需要在图像外添加一个边框。

这是代码:

Bitmap circleBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);   
BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);

Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);      
paint.setColor(Color.parseColor("#BAB399"));

Canvas c = new Canvas(circleBitmap);        
c.drawARGB(0, 0, 0, 0);
c.drawCircle(50, 40,40, paint);

有人可以帮我在圆形图像之外创建一个边框吗?

4

3 回答 3

9

首先你创建一个像这样的圆形,

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#333440" android:endColor="#333440"
        android:angle="270"/>
</shape>

然后添加一个相对布局并为其添加一个imageview。将其排列到相对布局的中心。并将这个圆形设置为Imageview的背景。然后将您的圆形imageview放在之前添加的imageview之上。也将它排列到中心。通过改变你的圆形imageview margin 你会得到想要的边框效果。

最终代码,

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView14"
            android:background="@drawable/circ"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView15"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/linked"
            android:layout_margin="5dp" />
    </RelativeLayout>
</LinearLayout>

在此处输入图像描述

于 2013-05-27T10:19:34.157 回答
0

嗨,伙计们,我遇到了同样的麻烦,但我设法解决了这样的问题。我正在分享可能会有所帮助。

public static LayerDrawable borderImageCircleBorder(Drawable draw, Activity activity) {
    int xCordinate = returnImageDrawableSize(draw);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(xCordinate);
    biggerCircle.setIntrinsicWidth(xCordinate);
    biggerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
    biggerCircle.getPaint().setColor(Color.WHITE);
    biggerCircle.setPadding(4, 4, 4, 4);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(xCordinate);
    smallerCircle.setIntrinsicWidth(xCordinate);
    smallerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
    smallerCircle.getPaint().setColor(Color.LTGRAY);
    smallerCircle.setPadding(2, 2, 2, 2);

    Drawable dd = getRoundedCornerBitmap(draw, activity);
    Drawable[] d = { smallerCircle, biggerCircle, dd };

    LayerDrawable composite = new LayerDrawable(d);
    return composite;
}

public static int returnImageDrawableSize(Drawable image) {
        Bitmap original = ((BitmapDrawable) image).getBitmap();
        Bitmap bitmap = original;
        //Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
        int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
        cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
        croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
        xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
        float cordinates = bitmap.getWidth() / 2;
        // Find out ratio until we get a square bitmap
        while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
            devideBy += 1;
            xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
            if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
                break;
        }

        return xCoordinate;

    }



public static Drawable getRoundedCornerBitmap(Drawable image, Activity activity) {

        Bitmap original = ((BitmapDrawable) image).getBitmap();
        ;
        Bitmap bitmap = original;
        //Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
        int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
        cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
        croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
        xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
        float cordinates = bitmap.getWidth() / 2;
        // Find out ratio until we get a square bitmap
        while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
            devideBy += 1;
            xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
            if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
                break;
        }
        if (bitmap.getWidth() >= bitmap.getHeight()) {
            cordinates = bitmap.getHeight() / 2;
            bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getHeight(), bitmap.getHeight());
        } else {
            bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getWidth(), bitmap.getWidth());
        }
        //Log.i("bitmap after crop", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

        int color = 0xff424242;
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);

        Canvas canvas = new Canvas(output);
        canvas.drawARGB(0, 0, 0, 0);
        //Log.i("coordinates", "?????????" + cordinates);
        canvas.drawCircle(cordinates, cordinates, cordinates, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, rect, rect, paint);

        // output =
        // ShadowView.getCircleWhiteBorderBitmap(original,output,convertDpToPixel(12,activity));
        Drawable d = new BitmapDrawable(activity.getResources(), output);
        return d;
    }
于 2013-09-19T06:28:22.943 回答
0

我找到了一个完全按照您的意愿进行操作的库,对我来说效果很好。看看这个。https://android-arsenal.com/details/1/932

于 2015-09-07T06:53:02.783 回答