Android 詳解Canvas與Paint的關係並附有程式碼例項
阿新 • • 發佈:2019-01-09
ShapesView extends View {
private final Paint mNormalPaint;
private final Paint mStrokePaint;
private final Paint mFillPaint;
private final RectF mRect;
private final RectF mOval;
private final RectF mArc;
private final Path mTriangle;
private TextPaint mSubTitlePaint;
Paint paint ;
public ShapesView(Context c) {
super(c);
mRect = new RectF(0.0f, 0.0f, 160.0f, 90.0f);
mNormalPaint = new Paint();
mNormalPaint.setAntiAlias(true);
mNormalPaint.setColor(0xff0000ff);
mNormalPaint.setStrokeWidth(6.0f);
mNormalPaint.setStyle(Paint.Style.FILL_AND_STROKE) ;
mStrokePaint = new Paint();
mStrokePaint.setAntiAlias(true);
mStrokePaint.setColor(0xff0000ff);
mStrokePaint.setStrokeWidth(6.0f);
mStrokePaint.setStyle(Paint.Style.STROKE);
mFillPaint = new Paint();
mFillPaint.setAntiAlias(true);
mFillPaint .setColor(0xff0000ff);
mFillPaint.setStyle(Paint.Style.FILL);
mOval = new RectF(0.0f, 0.0f, 80.0f, 45.0f);
mArc = new RectF(0.0f, 0.0f, 100.0f, 120.0f);
mTriangle = new Path();
mTriangle.moveTo(0.0f, 90.0f);
mTriangle.lineTo(45.0f, 0.0f);
mTriangle.lineTo(90.0f, 90.0f);
mTriangle.close();
mSubTitlePaint = new TextPaint();
// 設定paint顏色
mSubTitlePaint.setColor(Color.RED);
// 設定字型大小DP
mSubTitlePaint.setTextSize(36);
// 設定抗鋸齒
mSubTitlePaint.setAntiAlias(true);
// 設定字型居中
mSubTitlePaint.setTextAlign(Paint.Align.CENTER);
mSubTitlePaint.setStrikeThruText(true);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setAlpha(80);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
paint.setShadowLayer(10, 10, 10, Color.BLACK);
paint.setTextSize(30);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(50.0f, 50.0f);
canvas.drawRoundRect(mRect, 6.0f, 6.0f, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawRoundRect(mRect, 6.0f, 6.0f, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawRoundRect(mRect, 6.0f, 6.0f, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(250.0f, 50.0f);
canvas.drawCircle(80.0f, 45.0f, 45.0f, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawCircle(80.0f, 45.0f, 45.0f, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawCircle(80.0f, 45.0f, 45.0f, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(450.0f, 50.0f);
canvas.drawOval(mOval, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawOval(mOval, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawOval(mOval, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(625.0f, 50.0f);
canvas.drawRect(0.0f, 0.0f, 160.0f, 90.0f, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawRect(0.0f, 0.0f, 160.0f, 90.0f, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawRect(0.0f, 0.0f, 160.0f, 90.0f, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(825.0f, 50.0f);
canvas.drawArc(mArc, -30.0f, 70.0f, true, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawArc(mArc, -30.0f, 70.0f, true, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawArc(mArc, -30.0f, 70.0f, true, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(950.0f, 50.0f);
canvas.drawArc(mArc, 30.0f, 100.0f, false, mNormalPaint);
canvas.translate(0.0f, 110.0f);
canvas.drawArc(mArc, 30.0f, 100.0f, false, mStrokePaint);
canvas.translate(0.0f, 110.0f);
canvas.drawArc(mArc, 30.0f, 100.0f, false, mFillPaint);
canvas.restore();
canvas.save();
canvas.translate(50.0f, 400.0f);
canvas.drawPath(mTriangle, mNormalPaint);
canvas.translate(110.0f, 0.0f);
canvas.drawPath(mTriangle, mStrokePaint);
canvas.translate(110.0f, 0.0f);
canvas.drawPath(mTriangle, mFillPaint);
canvas.restore();
canvas.drawText("哈哈哈", 100, 600, mSubTitlePaint);
canvas.drawText("畫一個很大的圓", 50, 850, paint);
//cx和cy為圓點的座標
int radius = 80;
int offest = 40;
int startX = radius + offest;
int startY = radius + offest + 40;
canvas.drawCircle(startX*3, startY*5, radius, paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setShadowLayer(20, -20, 700, Color.BLACK);
canvas.drawCircle(startX + radius * 2 + offest, startY, radius, paint);
}
}