Android自定义视图绘图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自定义视图绘图相关的知识,希望对你有一定的参考价值。

android自定义视图绘图:屏幕截图:

enter image description here

这不能保存最后一个跟踪,我想重绘一个位图,但效果不是很好。

截图:

enter image description here

码:

public class CustomView extends View 

private float sX, sY, eX, eY;

private Paint paint = new Paint();
private Canvas canvas = new Canvas();
private Bitmap bitmap;

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    super.onSizeChanged(w, h, oldw, oldh);
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    setBackgroundColor(Color.WHITE);


@Override
public boolean onTouchEvent(MotionEvent event) 
    switch (event.getAction()) 
        case MotionEvent.ACTION_MOVE:
            eX = event.getX();
            eY = event.getY();
            canvas.drawLine(sX, sY, eX, eY, paint);
            break;
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            break;
    
    invalidate();
    return true;


@Override
protected void onDraw(Canvas canvas) 
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, getMatrix(), null);


你遇到过同样的问题吗?

答案

我找到了解决方案,但我不想以这种方式解决问题。我的目标是只用新画布来解决问题。

screenshot

public class CustomView extends View 

private float sX, sY, eX, eY;

private Paint paint = new Paint();
private Canvas canvas = new Canvas();
private Bitmap bitmap;

public CustomView(Context context) 
    super(context);
    init();


public CustomView(Context context, AttributeSet attrs) 
    super(context, attrs);
    init();


public CustomView(Context context, AttributeSet attrs, int defStyleAttr) 
    super(context, attrs, defStyleAttr);
    init();


private void init() 
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.STROKE);


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    super.onSizeChanged(w, h, oldw, oldh);
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    setBackgroundColor(Color.WHITE);


@Override
public boolean onTouchEvent(MotionEvent event) 
    switch (event.getAction()) 
        case MotionEvent.ACTION_MOVE:
            eX = event.getX();
            eY = event.getY();
            break;
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            canvas.drawLine(sX, sY, eX, eY, paint);
            break;
    
    invalidate();
    return true;


@Override
protected void onDraw(Canvas canvas) 
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, getMatrix(), null);
    canvas.drawLine(sX, sY, eX, eY, paint);


要么

public class CustomView extends View 

private Paint paint = new Paint();
private List<Path> cache = new ArrayList<>();
private Path currentPath;

public CustomView(Context context) 
    super(context);
    init();


public CustomView(Context context, AttributeSet attrs) 
    super(context, attrs);
    init();


public CustomView(Context context, AttributeSet attrs, int defStyleAttr) 
    super(context, attrs, defStyleAttr);
    init();


private void init() 
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.STROKE);


@Override
public boolean onTouchEvent(MotionEvent event) 
    switch (event.getAction()) 
        case MotionEvent.ACTION_MOVE:
            if (currentPath != null) 
                currentPath.eX = event.getX();
                currentPath.eY = event.getY();
            
            break;
        case MotionEvent.ACTION_DOWN:
            currentPath = new Path();
            currentPath.sX = event.getX();
            currentPath.sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            if (currentPath != null) 
                cache.add(currentPath);
                currentPath = null;
            
            break;

    
    invalidate();
    return true;


@Override
protected void onDraw(Canvas canvas) 
    super.onDraw(canvas);
    for (Path path : cache) 
        canvas.drawLine(path.sX, path.sY, path.eX, path.eY, paint);
    
    if (currentPath == null) return;
    canvas.drawLine(currentPath.sX, currentPath.sY, currentPath.eX, currentPath.eY, paint);


static class Path 
    float sX;
    float sY;
    float eX;
    float eY;


另一答案

当你的手指移动时,你应该更新sX sY变量

  1. ACTION_DOWN点是[100,100] sX = 100sY = 100
  2. ACTION_MOVE现在手指位于[130,150] eY = 130eY = 150
  3. invalidate()所以将触发draw()画线[100,100]到[130,150]
  4. 手指继续移动
  5. ACTION_MOVE现在开始是[130,150] sX = 130sY = 150不是ACTION_DOWN点值
  6. ACTION_MOVE现在手指位于[160,180]
  7. invalidate()所以将触发draw()画线[130,150]到[160,180]
  8. 继续做同样的事情.....
@Override
public boolean onTouchEvent(MotionEvent event) 
    switch (event.getAction()) 
        case MotionEvent.ACTION_MOVE:
            eX = event.getX();
            eY = event.getY();
            invalidate()
            sX = eX;
            sY = eY;
            break;
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            canvas.drawLine(sX, sY, eX, eY, paint);
            break;
    
    //invalidate();
    return true;


我没有运行代码,但也许你已经明白了我的意思。

你应该更新startX startYendX endY

以上是关于Android自定义视图绘图的主要内容,如果未能解决你的问题,请参考以下文章

需要帮助在Android中使用多个按钮自定义视图

Android:在片段内膨胀自定义视图

[Android自定义控件] Android自定义控件

Android自定义控件

android自定义控件怎么用

当应用程序在后台时,Finger Paint 自定义视图绘图消失