这是我用来绘制虚线的活动。但我想在线条中绘制渐变或某种风格..
DragObserverLayout.java
//class for draw line from one place to another
public class DragObserverLayout extends RelativeLayout {
float startX, startY, stopX, stopY;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private List<Rect> lines = new ArrayList<Rect>();
public List<Path> linePath = new ArrayList<Path>();
//class for dragging
public DragObserverLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//setting color stroke and effect
mPaint.setColor(Color.RED);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setPathEffect(new DashPathEffect(new float[] {5,5}, 0));
mPaint.setStrokeWidth(10.0f);
}
//for canvas
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
final int count = lines.size();
for (int i = 0; i < count; i++) {
final Rect r = lines.get(i);
canvas.drawLine(r.left, r.top, r.right, r.bottom, mPaint);
}
}