PaintView rendernig不平滑

时间:2018-09-23 09:12:57

标签: java android bitmap

这是android中paintView的代码,我打算用自己的服装图片绘制,我只选择使用CPU而不是GPU,我的问题是在开始的几秒钟内效果很好,但是在几秒钟后开始落后。 这是我的代码

public class PaintView extends View {
  Bitmap bitmap;
  Canvas mCanvas;
  Paint paintDraw;
  ArrayList<Path> array = new ArrayList<>();
  Path path;
  Bitmap circleBitMap;
  private float y;
  private float x;
  private float pastX;
  private float pastY;

  public PaintView(Context context) {
    super(context);
    initi();
  }

  public PaintView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initi();
  }

  public PaintView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initi();
  }

  private void initi() {
    path = new Path();
    bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    Toast.makeText(getContext(), "" + bitmap.getWidth(), Toast.LENGTH_LONG).show();
    mCanvas = new Canvas(bitmap);
    paintDraw = new Paint();
    paintDraw.setStyle(Paint.Style.FILL);
    paintDraw.setAntiAlias(true);
    paintDraw.setColor(Color.BLUE);
    paintDraw.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN));
    //prepare custom image for drawing onView
    for (int i = 0; i < 10; i++) {
      for (int i1 = 0; i1 < 40; i1++) {
        mCanvas.drawCircle(i * 20, i1 * 20, 9, paintDraw);
      }
    }
    setBackgroundColor(Color.WHITE);
    //make circular custom image
    circleBitMap = getCroppedBitmap(bitmap);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    for (Path path : array) {
      drawBitmapAlongPath(path, circleBitMap, canvas);
    }
    super.onDraw(canvas);
  }
//make bitmap circular
  public Bitmap getCroppedBitmap(Bitmap bitmap) {
    float x = bitmap.getWidth();
    float y = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap((int) x, (int) y, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    canvas.drawCircle(x / 2, y / 2, x / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return output;
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    x = event.getX();
    y = event.getY();
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        path = new Path();
        path.moveTo(x, y);
        break;
      case MotionEvent.ACTION_MOVE:
        path.quadTo(pastX, pastY, (pastX + x) / 2, (pastY + y) / 2);
        array.add(path);
        invalidate();
        break;
    }
    pastX = x;
    pastY = y;
    return true;
  }
  // draw array of image along path
  void drawBitmapAlongPath(Path p, Bitmap b, Canvas canvas) {
    Matrix m = new Matrix();
    PathMeasure meas = new PathMeasure(p, false);
    final float length = meas.getLength();
    float distance = 0;
    final int flags = PathMeasure.POSITION_MATRIX_FLAG |
      PathMeasure.TANGENT_MATRIX_FLAG;

    while (distance < length) {
      meas.getMatrix(distance, m, flags);
      canvas.drawBitmap(b, m, paintDraw);
      // 40=200/5
      distance += 40;
    }
  }
}

这是此问题的视频 enter image description here

0 个答案:

没有答案