在画布上画线并在绘制后修改位置

时间:2019-01-31 11:34:03

标签: java android drawing

这件事我需要帮助

我在画布上画线,绘制后我需要能够修改位置。我搜索并尝试了很多方法,但似乎没人能奏效

我想选择该行的一端并将其拖动到另一位置

有人可以给我和建议吗?

1 个答案:

答案 0 :(得分:-1)

请尝试此操作。在此您可以绘制和编辑线条。

class Drawing extends View{
    private Canvas mCanvas = null;
    private Path mPath = null;
    private Paint mBitmapPaint = null;
    private Bitmap mBitmap = null;
    private Bitmap bit=null;
    private Paint mPaint = null;
    private MainActivity baseMainActivity = null;


    public interface onDrawingViewSingleTap{
        void onDrawingViewTap(float x , float y);
    }

    public Drawing(Context c) {
        super(c);
        baseMainActivity=(MainActivity) c;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);//This sets the width of signature

        Display display =baseMainActivity.getWindowManager().getDefaultDisplay();

        mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);// 320*480  // For setting size of screen to draw Bitmap

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mCanvas = new Canvas(mBitmap);
        onDraw(mCanvas);
    }


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

    @Override
    protected void onDraw(Canvas canvas) {
        try{
            canvas.drawColor(Color.TRANSPARENT);//Color.WHITE //To change background color of Application
            if(bit!=null)
                canvas.drawBitmap(bit, 0, 0, mBitmapPaint);
            else
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        }catch(Exception e){}
        if(bit==null)
            canvas.drawPath(mPath, mPaint);

        bit=null;

    }
    private float mX, mY;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x+1;
        mY = y+1;

    }

    private void touch_upline(float x,float y) {
        mPath.lineTo(mX, mY);
        mCanvas.drawLine(mX, mY, x, y, mPaint);
        mPath.reset();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            eraseAll();
            touch_upline(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_upline(x,y);
            invalidate();
            break;
        }
        return true;
    }

    public void eraseAll()
    {
        mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
        mCanvas = new Canvas(mBitmap);
    }

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout relativeLayout= (RelativeLayout) (findViewById(R.id.mainLayout));
        relativeLayout.addView(new Drawing(this));
    }

}