如何避免android上的路径重叠?

时间:2015-07-25 14:40:47

标签: android path overlapping

我想绘制多个渐变路径。 (我必须为每条路径使用单独的渐变。)

但是,每条路径都与另一条路径重叠。

如何避免(清除)重叠并绘制自然路径?

(PorterDuff.Mode.CLEAR无效:<)

请帮忙!

代码:

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;

public class AnimatingLineView extends View {
private Paint paint;
private Path path;

@SuppressLint("NewApi")
public AnimatingLineView(Context context, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initLinewView();
}

public AnimatingLineView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initLinewView();
}

public AnimatingLineView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initLinewView();
}

public AnimatingLineView(Context context) {
    super(context);
    initLinewView();
}
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);

    PorterDuffXfermode xfermode  = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);

    path.reset();
    path.moveTo(337, 658);
    path.lineTo(324, 643);
    path.lineTo(312, 625);
    path.lineTo(301, 606);
    path.lineTo(289, 588);

    paint.setShader(new LinearGradient(337, 658, 289, 588,
            Color.argb(0, 255, 255, 255), Color.argb(70, 255, 255, 255), TileMode.CLAMP));
    canvas.drawPath(path, paint);

    path.reset();
    path.moveTo(289, 588);
    path.lineTo(279, 564);
    path.lineTo(273, 514);
    path.lineTo(273, 518);
    path.lineTo(274, 495);

    paint.setShader(new LinearGradient(289, 588, 274, 495,
            Color.argb(70, 255, 255, 255), Color.argb(140, 255, 255, 255), TileMode.CLAMP));        
    paint.setXfermode(xfermode);
    canvas.drawPath(path, paint);
    paint.setXfermode(null);
    canvas.drawPath(path, paint);

    path.reset();
    path.moveTo(274, 495);
    path.lineTo(272, 471);
    path.lineTo(272, 447);
    path.lineTo(277, 423);
    path.lineTo(278, 401);

    paint.setShader(new LinearGradient(274, 495, 278, 401,
            Color.argb(140, 255, 255, 255), Color.argb(70, 255, 255, 255), TileMode.CLAMP));
    paint.setXfermode(xfermode);
    canvas.drawPath(path, paint);
    paint.setXfermode(null);
    canvas.drawPath(path, paint);

    path.reset();
    path.moveTo(278, 401);
    path.lineTo(278, 385);
    path.lineTo(278, 370);
    path.lineTo(278, 355);
    path.lineTo(278, 340);

    paint.setShader(new LinearGradient(278, 401, 278, 340,
            Color.argb(70, 255, 255, 255), Color.argb(0, 255, 255, 255), TileMode.CLAMP));
    paint.setXfermode(xfermode);
    canvas.drawPath(path, paint);
    paint.setXfermode(null);
    canvas.drawPath(path, paint);
}

private void initLinewView() {
    path= new Path();
    paint = new Paint();

    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(28);
    paint.setAntiAlias(true);
}
}

0 个答案:

没有答案