java画的圆如何填充_java – 绘制外面填充的透明圆圈

我的答案很晚,但可能有助于某人实现这一目标.

下面是一个如何制作半透明视图的示例,其中透明圆圈匹配最小尺寸的尺寸,放置在中心,边距较小.它可以作为叠加放置在任何视图上.

/*

* Copyright (c) 2015 Singularex Inc.

*/

package your_package.ui.widget;

import android.annotation.TargetApi;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.RectF;

import android.os.Build;

import android.util.AttributeSet;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import your_package.R;

/**

* @author Victor Kosenko

*/

public class RadiusOverlayView extends LinearLayout {

private Bitmap windowFrame;

public RadiusOverlayView(Context context) {

super(context);

}

public RadiusOverlayView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public RadiusOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public RadiusOverlayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

@Override

protected void dispatchDraw(Canvas canvas) {

super.dispatchDraw(canvas);

if (windowFrame == null) {

createWindowFrame(); // Lazy creation of the window frame, this is needed as we don't know the width & height of the screen until draw time

}

canvas.drawBitmap(windowFrame, 0, 0, null);

}

protected void createWindowFrame() {

windowFrame = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); // Create a new image we will draw over the map

Canvas osCanvas = new Canvas(windowFrame); // Create a canvas to draw onto the new image

RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // Anti alias allows for smooth corners

paint.setColor(getResources().getColor(R.color.map_radius_outer)); // This is the color of your activity background

paint.setAlpha(84);

osCanvas.drawRect(outerRectangle, paint);

paint.setColor(Color.TRANSPARENT); // An obvious color to help debugging

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // A out B http://en.wikipedia.org/wiki/File:Alpha_compositing.svg

float centerX = getWidth() / 2;

float centerY = getHeight() / 2;

float radius = Math.min(getWidth(), getHeight()) / 2 - getResources().getDimensionPixelSize(R.dimen.view_margin_small2);

osCanvas.drawCircle(centerX, centerY, radius, paint);

}

@Override

public boolean isInEditMode() {

return true;

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

super.onLayout(changed, l, t, r, b);

windowFrame = null; // If the layout changes null our frame so it can be recreated with the new width and height

}

}

在这里我的情况看起来如何:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值