如何绘制带有包含图像的边距的圆圈?

时间:2015-03-27 01:35:44

标签: android android-layout android-canvas remoteview

我只有一个包含个人视图的RemoteViews对象。我有这个RemoteViews对象的以下布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

<LinearLayout
        android:layout_width="50dp"
        android:layout_height="52dp"
        android:layout_marginLeft="10dp"
        android:gravity="center">
<ImageView
        android:contentDescription="icon"
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/dog"
        />
</LinearLayout>
<TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

我试图在图标周围画一个圆圈,中间有一些空格;我不想跟踪图标周围的圆圈,我希望它们之间有一些空白(如目标标志)。

我认为通过指定一个大于imageview尺寸的半径,我可以实现这一点,但外圈在某些部分会被切断;在这里&#34;观点&#34;是RemoteViews对象:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),    
                R.drawable.dog);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, 
                       true);
Canvas canvas = new Canvas(mutableBitmap);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(6);

int centerCoordinate = mutableBitmap.getWidth()/2;
canvas.drawCircle(centerCoordinate, centerCoordinate, 
centerCoordinate+15, paint);

// equivalent to imageView.setImageBitmap
views.setImageViewBitmap(R.id.icon, mutableBitmap);

1 个答案:

答案 0 :(得分:1)

请尝试修改此代码。我试过解释评论中的变化。对不起,如果他们不够:

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),    
                R.drawable.dog);
        //
        // vvvv Commented out vvvv
        /*
         * Reason: The new Bitmap must be larger than the bitmap around
         * which the circle must be drawn. 
         */
//        Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
//        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, 
//                true);
//        Canvas canvas = new Canvas(mutableBitmap);
        // ^^^^ Commented out ^^^^
        //
        // vvvv Added vvvv
        // This is the total (Right + left) extra space on the sides;
        int padding = 30;
        // Since the Paint is going to draw a noticeably thick line, the thickness must be included in the calculations
        int strokeWidth = 6;
        /*
         * Calculating single dimension since the bitmap must have a square shape for the circle to fit.
         * Also account for the padding and the stroke width;
         */
        int bitmapSize = Math.max(bitmap.getWidth(), bitmap.getHeight()) + padding + strokeWidth;
        Bitmap workingBitmap = Bitmap.createBitmap(bitmapSize, bitmapSize,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(workingBitmap);
        // ^^^^ Added ^^^^
        //
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        //
        // paint.setStrokeWidth(6);
        paint.setStrokeWidth(strokeWidth);
        //
        // canvas.drawCircle(centerCoordinate, centerCoordinate, 
        //         centerCoordinate+15, paint);
        /*
         * Calculate exact top left position in the result Bitmap to draw the original Bitmap
         */
        canvas.drawBitmap(bitmap, (bitmapSize - bitmap.getWidth()) / 2.0f,
                (bitmapSize - bitmap.getHeight()) / 2.0f, paint);
        //
        // int centerCoordinate = mutableBitmap.getWidth()/2;
        int centerCoordinate = bitmapSize / 2;
        //
        //canvas.drawCircle(centerCoordinate, centerCoordinate, 
        // centerCoordinate+15, paint);
        /*
         * Draw the circle but account for the stroke width of the paint or else the circle will flatten on the edges of the Bitmap.
         */
        canvas.drawCircle(centerCoordinate, centerCoordinate,
                centerCoordinate - (strokeWidth/2.0f), paint);
        // equivalent to imageView.setImageBitmap
        // views.setImageViewBitmap(R.id.icon, mutableBitmap);
        views.setImageViewBitmap(R.id.icon, workingBitmap);

同样在ImageView添加的布局中:

android:scaleType="fitXY"

编辑: 为了保持内部位图大小不变并仅改变圆圈大小,首先,ImageView及其LinearLayout容器不能具有固定大小。将布局中的所有布局宽度和高度值更改为"wrap_content"

其次,由于"bitmap"的图像资源大小未知,因此必须使用符合最大允许维度bitmap >对于位图,在您的情况下 30px 。这可以通过替换:

来完成
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),    
                    R.drawable.dog);

使用以下代码:

//
// Value to hold the required image dimension;
int requiredImageDimension = 30;
// Decode the Bitmap resource with the set options.
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.dog);
// Scaled bitmap reference;
Bitmap bitmap = null;
// Check if the largest dimension is the width;
if (originalBitmap.getWidth() > originalBitmap.getHeight()) {
    // Force the width to the maximum allowable size and calculate
    // the scaled height of the Bitmap;
    bitmap = Bitmap.createScaledBitmap(originalBitmap,
            requiredImageDimension,
            originalBitmap.getHeight() * requiredImageDimension
                    / originalBitmap.getWidth(), true);
}
// If the width and height are equal;
else if(originalBitmap.getWidth() == originalBitmap.getHeight()){
    // Force the width and height to the maximum allowable size;
    bitmap = Bitmap.createScaledBitmap(originalBitmap,
            requiredImageDimension,
            requiredImageDimension, true);
}
// If the largest dimension is the height;
else {
    // Force the height to the maximum allowable size and calculate
    // the scaled width of the Bitmap;
    bitmap = Bitmap.createScaledBitmap(originalBitmap,
            originalBitmap.getWidth() * requiredImageDimension
                    / originalBitmap.getHeight(),
            requiredImageDimension, true);
}

现在,您将获得原始位图资源的缩小版本,该资源将粘贴到较大的workingBitmap上并加载到ImageView中,后者将自行调整大小以适应没有缩放的位图。