【发布时间】:2016-01-22 22:47:28
【问题描述】:
先看下图。
package com.syncfusion.rating;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.view.View;
/**
* Created by chozarajan.pandiyarajan on 10/9/2015.
*/
public class SfRatingItem extends View {
private int fillColor,minDim,topXPoint,topYPoint;
private double bigHypot,bigA,bigB,littleHypot,littleA,littleB,value;
private Paint starPaint;
private Path path;
public SfRatingItem(Context context) {
super(context);
starPaint=new Paint();
fillPaint=new Paint();
path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
starPaint.setStyle(Paint.Style.FILL_AND_STROKE);
starPaint.setAntiAlias(true);
minDim = Math.min(this.getWidth() - this.getPaddingLeft() -this.getPaddingRight(), this.getHeight() - this.getPaddingTop() - this.getPaddingBottom());
bigHypot = (minDim / Math.cos(Math.toRadians(18)));
bigB = minDim;
bigA = Math.tan(Math.toRadians(18)) * bigB;
littleHypot = bigHypot / (2 + Math.cos(Math.toRadians(72)) + Math.cos(Math.toRadians(72)));
littleA = Math.cos(Math.toRadians(72)) * littleHypot;
littleB = Math.sin(Math.toRadians(72)) * littleHypot;
topXPoint = (this.getWidth() - this.getPaddingLeft() -this.getPaddingRight()) / 2;
topYPoint =this.getPaddingTop();
path.moveTo(topXPoint, topYPoint);
path.lineTo((int) (topXPoint + bigA), (int) (topYPoint + bigB));
path.lineTo((int) (topXPoint - littleA - littleB), (int) (topYPoint + littleB));
path.lineTo((int) (topXPoint + littleA + littleB), (int) (topYPoint + littleB));
path.lineTo((int) (topXPoint - bigA), (int) (topYPoint + bigB));
path.lineTo(topXPoint, topYPoint);
path.close();
starPaint.setColor(Color.RED);
//Use below code to paint the star
canvas.drawPath(path, starPaint);
// Use below code to clip the star path.
// canvas.clipPath(path, Region.Op.DIFFERENCE);
// canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
}
如果你看到上面的图片,你就知道这两张图片的区别了。
第一个是剪辑图像。边缘被剪掉的星形不清晰。
第二个是画图。边缘的彩绘星形清晰。
因为在绘制的图像中,我习惯于将 setAntiAlise() 属性设置为 true。
我的问题是如何让剪裁的图像边缘清晰?
【问题讨论】:
标签: android path android-canvas paint clipping