package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;
public class CircularCoverView extends View {
private int leftTopRadians = 30; //leftTopRadians
private int leftBottomRadians = 30; //leftBottomRadians
private int rightTopRadians = 30; //rightTopRadians
private int rightBottomRadians = 30; //rightBottomRadians
private int coverColor = 0xffeaeaea; //color of cover.
public CircularCoverView(Context context) {
this(context, null, 0);
}
public CircularCoverView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircularCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
}
/**
* set radians of cover.
*/
public void setRadians(int leftTopRadians, int rightTopRadians, int leftBottomRadians, int rightBottomRadians) {
this.leftTopRadians = leftTopRadians;
this.rightTopRadians = rightTopRadians;
this.leftBottomRadians = leftBottomRadians;
this.rightBottomRadians = rightBottomRadians;
}
/**
* set color of cover.
*
* @param coverColor cover's color
*/
public void setCoverColor(@ColorInt int coverColor) {
this.coverColor = coverColor;
}
/**
* create a sector-bitmap as the dst.
*
* @param w width of bitmap
* @param h height of bitmap
* @return bitmap
*/
private Bitmap drawSector(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.
c.drawArc(new RectF(0, 0, leftTopRadians * 2, leftTopRadians * 2), 180, 90, true, p);
c.drawArc(new RectF(0, getHeight() - leftBottomRadians * 2, leftBottomRadians * 2, getHeight()), 90, 90, true, p);
c.drawArc(new RectF(getWidth() - rightTopRadians * 2, 0, getWidth(), rightTopRadians * 2), 270, 90, true, p);
c.drawArc(new RectF(getWidth() - rightBottomRadians * 2, getHeight() - rightBottomRadians * 2, getWidth(), getHeight()), 0, 90, true, p);
return bm;
}
/**
* create a rect-bitmap as the src.
*
* @param w width of bitmap
* @param h height of bitmap
* @return bitmap
*/
private Bitmap drawRect(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(coverColor);
c.drawRect(new RectF(0, 0, leftTopRadians, leftTopRadians), p);
c.drawRect(new RectF(0, getHeight() - leftBottomRadians, leftBottomRadians, getHeight()), p);
c.drawRect(new RectF(getWidth() - rightTopRadians, 0, getWidth(), rightTopRadians), p);
c.drawRect(new RectF(getWidth() - rightBottomRadians, getHeight() - rightBottomRadians, getWidth(), getHeight()), p);
return bm;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setFilterBitmap(false);
paint.setStyle(Paint.Style.FILL);
//create a canvas layer to show the mix-result
int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG );
//draw sector-dst-bitmap at first.
canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);
//set Xfermode of paint.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
//then draw rect-src-bitmap
canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);
paint.setXfermode(null);
//restore the canvas
canvas.restoreToCount(sc);
}
}
values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircularCoverView">
<attr name="left_top_radius" format="dimension" />
<attr name="left_bottom_radius" format="dimension" />
<attr name="right_top_radius" format="dimension" />
<attr name="right_bottom_radius" format="dimension" />
<attr name="cover_color" format="color" />
</declare-styleable>
</resources>
方法二:
public class CorverView extends View {
public CorverView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = TabListItemViewUtil.getPicWidth();
int height = DensityUtil.dip2px(getContext(), 260);
setMeasuredDimension(width, height);
}
public CorverView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CorverView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
Path path = new Path();
paint.setStyle(Paint.Style.FILL);
paint.setColor(getResources().getColor(R.color.colorGrayLight));
paint.setStrokeWidth(5f);
paint.setAntiAlias(true);
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
int mRadius = DensityUtil.dip2px(getContext(), 8);
path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius}, Path.Direction.CW);
canvas.drawPath(path, paint);
}
}
网友评论