下面列出了android.graphics.drawable.ShapeDrawable#draw() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
//将图片裁剪为椭圆形
/* 构建ShapeDrawable对象并定义形状为椭圆 */
mShapeDrawableQQ = new ShapeDrawable(new OvalShape());
/* 设置要绘制的椭圆形的东西为ShapeDrawable图片 */
mShapeDrawableQQ.getPaint().setShader(mBitmapShader);
/* 设置显示区域 */
mShapeDrawableQQ.setBounds(0,0, BitQQwidth, BitQQheight);
/* 绘制ShapeDrawableQQ */
mShapeDrawableQQ.draw(canvas);
//绘制渐变的矩形
mPaint.setShader(mLinearGradient);
canvas.drawRect(BitQQwidth, 0, 320, 156, mPaint);
//显示混合渲染效果
mPaint.setShader(mComposeShader);
canvas.drawRect(0, 300, BitQQwidth, 300+BitQQheight, mPaint);
//绘制环形渐变
mPaint.setShader(mRadialGradient);
canvas.drawCircle(50, 200, 50, mPaint);
//绘制梯度渐变
mPaint.setShader(mSweepGradient);
canvas.drawRect(150, 160, 300, 300, mPaint);
}
private void setSpaceCorner(Canvas canvas, int trueHeight) {
int leftTop;
int rightTop;
int rightBottom;
int leftBottom;
if (mCornerRadius_leftTop == -1) {
leftTop = (int) mCornerRadius;
} else {
leftTop = (int) mCornerRadius_leftTop;
}
if (leftTop > trueHeight / 2) {
leftTop = trueHeight / 2;
}
if (mCornerRadius_rightTop == -1) {
rightTop = (int) mCornerRadius;
} else {
rightTop = (int) mCornerRadius_rightTop;
}
if (rightTop > trueHeight / 2) {
rightTop = trueHeight / 2;
}
if (mCornerRadius_rightBottom == -1) {
rightBottom = (int) mCornerRadius;
} else {
rightBottom = (int) mCornerRadius_rightBottom;
}
if (rightBottom > trueHeight / 2) {
rightBottom = trueHeight / 2;
}
if (mCornerRadius_leftBottom == -1) {
leftBottom = (int) mCornerRadius;
} else {
leftBottom = (int) mCornerRadius_leftBottom;
}
if (leftBottom > trueHeight / 2) {
leftBottom = trueHeight / 2;
}
float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上,右上,右下,左下
ShapeDrawable mDrawables = new ShapeDrawable(new RoundRectShape(outerR, null, null));
mDrawables.getPaint().setColor(paint.getColor());
mDrawables.setBounds(leftPading, topPading, getWidth() - rightPading, getHeight() - bottomPading);
mDrawables.draw(canvas);
}
/**
* Draw points in chart.
*
* @param canvas Canvas to draw the chart.
*/
private void drawPoints(Canvas canvas) {
for (ShapeDrawable pointsShape : pointsShapes) {
pointsShape.draw(canvas);
}
}