我正在使用自定义视图或画布创建一个圆圈。圆圈也是用边框创建的,但问题是圆圈的右下角会被截断。我通过下面提到的代码获得以下输出,
我创建上述类的自定义视图类如下,
package cl.tk.ui.iBAPView;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import cl.tkp.R;
public class RoundedView extends View {
private Paint paint,mPaintBorder;
private int color;
float borderWidth;
private String text = null;
private boolean checked = false;
private Drawable drawable;
public RoundedView(Context context) {
super(context);
bootstrap(context, null,0,0);
}
public RoundedView(Context context, AttributeSet attrs) {
super(context, attrs);
bootstrap(context, attrs, 0,0);
}
public RoundedView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
bootstrap(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RoundedView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
bootstrap(context, attrs, defStyleAttr, defStyleRes);
}
private void bootstrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedView, defStyleAttr, defStyleRes);
text = a.getString(R.styleable.RoundedView_android_text);
color = a.getColor(R.styleable.RoundedView_bgColor, Color.GRAY);
drawable=a.getDrawable(R.styleable.RoundedView_drawableCenter);
checked=a.getBoolean(R.styleable.RoundedView_checked,false);
borderWidth= a.getDimension(R.styleable.RoundedView_rborderWidth,context.getResources().getDimension(R.dimen._minus2sdp));
paint = new Paint(Paint.DITHER_FLAG);
mPaintBorder = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBorder.setColor(a.getColor(R.styleable.RoundedView_borderColor,ContextCompat.getColor(context,android.R.color.transparent)));
a.recycle();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onDraw(Canvas canvas) {
//if (getBackgroundColor(this) != 0) color = getBackgroundColor(this);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
if(borderWidth>0)
{
float radius=(getWidth()-borderWidth-2)/2;
canvas.drawCircle(radius + borderWidth,radius + borderWidth,radius + borderWidth, mPaintBorder);
canvas.drawCircle(radius + borderWidth,radius + borderWidth,radius ,paint);
}else {
float radius=getWidth()/2;
canvas.drawCircle(radius, radius, radius, paint);
}
}
}
这是我在xml中调用它的方式,
<cl.tk.ui.iBAPView.RoundedView
android:id="@+id/signup_user_rv1"
android:layout_marginRight="@dimen/_3sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="@dimen/_18sdp"
android:layout_height="@dimen/_18sdp"
app:rborderWidth="@dimen/_2sdp"
app:borderColor="@color/colorBlue"/>