沿路径

时间:2015-11-01 14:07:11

标签: android path drawing

我正在尝试从路径上的资源重复图像,就像PathEffect中的形状一样。下面是我所拥有的两张图片与我想要实现的内容。

  • 使用带有三角形形状的PathDashPathEffect取得的成就: Screenshot
  • 我想要的结果应该是这样的: Desired

这是我的代码:

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ImageLineView extends View {

    private Paint mPaint;

    private Bitmap mDrawingBitmap;
    private Canvas mDrawingCanvas;

    private Path    mPath;
    private Paint   mBitmapPaint;

    private static final int BACKGROUND_COLOR = Color.WHITE;

    private static final int CENTER_STROKE_WIDTH = 10;

    public ImageLineView(Context context) {
        super(context);
        initialize();
    }

    public ImageLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public ImageLineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

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

    private void initialize() {
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setFilterBitmap(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(10);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setPathEffect(getTrianglePathEffect(CENTER_STROKE_WIDTH));

        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // Foreground Bitmap
        if (mDrawingBitmap == null) {
            mDrawingBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        } else {
            mDrawingBitmap = Bitmap.createScaledBitmap(mDrawingBitmap, w, h, false);
        }
        mDrawingCanvas = new Canvas(mDrawingBitmap);

        drawInitialView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mDrawingBitmap, 0, 0, mBitmapPaint);
    }

    private void drawInitialView() {
        mDrawingCanvas.drawColor(BACKGROUND_COLOR);
    }


    private void touchDraw(float x, float y) {

        mDrawingCanvas.drawColor(BACKGROUND_COLOR);

        mPath.reset();
        mPath.moveTo(x, y);

        int canvasWidth = mDrawingCanvas.getWidth();
        int canvasHeight = mDrawingCanvas.getHeight();

        mPath.moveTo(x,y);
        mPath.lineTo(0,0);
        mPath.moveTo(x,y);
        mPath.lineTo(0,canvasHeight);
        mPath.moveTo(x,y);
        mPath.lineTo(canvasWidth,0);
        mPath.moveTo(x,y);
        mPath.lineTo(canvasWidth,canvasHeight);


        mDrawingCanvas.drawPath(mPath, mPaint);
        mPath.reset();

    }

    private PathEffect getTrianglePathEffect(int strokeWidth) {
          return new PathDashPathEffect(
                  getTriangle(strokeWidth),
                  strokeWidth,
                  0.0f,
                  PathDashPathEffect.Style.ROTATE);
    }

    private Path getTriangle(float size) {
      Path path = new Path();
      float half = size / 2;
      path.moveTo(-half, -half);
      path.lineTo(half, -half);
      path.lineTo(0, half);
      path.close();
      return path;
    }

    private void touchDown(float x, float y) {
        touchDraw(x, y);
    }

    private void touchMove(float x, float y) {
        touchDraw(x, y);
    }

    private void touchUp() {
        resetCanvas();
    }

    private void resetCanvas() {
        drawInitialView();

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        int motionAction = event.getAction();

        switch (motionAction) {
        case MotionEvent.ACTION_DOWN:

            touchDown(x, y);
            invalidate();

            break;

        case MotionEvent.ACTION_MOVE:

            touchMove(x, y);
            invalidate();

            break;

        case MotionEvent.ACTION_UP:
            touchUp();
            invalidate();
            break;
        default:
            break;
        }

        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试以下代码

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;

public class Tiling extends View {
    Rect rect;
    Bitmap mBitmap;
    BitmapDrawable mDrawable;

public Tiling(Context mContext) {
    super(mContext);
    rect = new Rect(0, 0, 100, 100);
    mBitmap = loadBitmap();
    mDrawable = new BitmapDrawable(mContext.getResources(), mBitmap);
    mDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
    mDrawable.setBounds(rect);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mDrawable.draw(canvas);
}

public Bitmap loadBitmap() {

    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    canvas.drawRect(0, 0, 10, 10, paint);
    paint.setStyle(Style.STROKE);
    paint.setColor(Color.WHITE);
    canvas.drawRect(0, 0, 9, 9, paint);
    return bm;
}

}