Android - 画一条直线

时间:2017-06-21 18:08:03

标签: java android

我想画一条直线。我希望它能够工作,所以你将手指拖过屏幕并将其拉直。我的代码现在开始绘制最后一行结束的行。我该怎么办?谢谢! 这是代码:

package barnquilt.nkc.com.barnquiltmaker.Qulits;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import barnquilt.nkc.com.barnquiltmaker.BitMap.blue;
import barnquilt.nkc.com.barnquiltmaker.BitMap.red;
import barnquilt.nkc.com.barnquiltmaker.R;
import barnquilt.nkc.com.barnquiltmaker.UI.Line;
import barnquilt.nkc.com.barnquiltmaker.BitMap.bitmapInfo;
/**
 * Created by pokem on 6/1/2017.
 */

    public class MainDrawingView extends View  {

  Path path = new Path();
    public float eventX;
    public float eventY;

   public List<Point>redPointList = new ArrayList<Point>();
    public List<Point>bluePointList = new ArrayList<Point>();




    Context context = getContext();
   //Bitmaps
   public Bitmap redSquare;
   public Bitmap blueSquare;



    public int width = context.getResources().getDisplayMetrics().widthPixels;
    public int height = context.getResources().getDisplayMetrics().heightPixels;
    public int center  = height/2;
    public int widthDiv = width/2;


    private Paint redPaint  = new Paint();
    private Paint bluePaint = new Paint();
    private Paint black = new Paint();




    public MainDrawingView(final Context context, AttributeSet attrs){
        super(context, attrs);
        redPaint.setAntiAlias(true);
        redPaint.setStrokeWidth(5f);
        redPaint.setColor(Color.RED);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setStrokeJoin(Paint.Join.ROUND);


        bluePaint.setAntiAlias(true);
        bluePaint.setStrokeWidth(5f);
        bluePaint.setColor(Color.BLUE);
        bluePaint.setStyle(Paint.Style.STROKE);
        bluePaint.setStrokeJoin(Paint.Join.ROUND);


        black.setAntiAlias(true);
        black.setStrokeWidth(5f);
        black.setColor(Color.BLACK);
        black.setStyle(Paint.Style.STROKE);
        black.setStrokeJoin(Paint.Join.ROUND);


        //define bitmap
        redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        blueSquare = BitmapFactory.decodeResource(getResources(), R.drawable.blue);





    }

    @Override
    public void onDraw(Canvas canvas){

       //Makes a straight line go through the center of the screen.
       canvas.drawLine(0,height/2.5f, width,height/2.5f,black);
       //Makes a straight line go up and down.
        canvas.drawLine(width/2.3f,0, width/2.3f, height,black);


             canvas.drawPath(path, bluePaint);

             for(Point point : redPointList){

                new red(redSquare,canvas,point.x, point.y);

             }

            for(Point pointt : bluePointList){
                  new blue(blueSquare,canvas,pointt.x, pointt.y);
            }










    }



    @Override
    public boolean onTouchEvent( MotionEvent event){



        bitmapInfo red = new bitmapInfo(redSquare);

        String height = String.valueOf(red.getBitmapHeight());
        String width = String.valueOf(red.getBitmapWidth());
        Toast.makeText(getContext(), height, Toast.LENGTH_SHORT).show();




         eventX = event.getX();
         eventY = event.getY();

















        switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:

                path.lineTo(eventX  ,eventY );
            break;

            case MotionEvent.ACTION_MOVE:

                break;

            case MotionEvent.ACTION_UP:
               //  alertMessage();
                break;




            default:
                return false;
        }
        invalidate();
        return true;
    }


 public void alertMessage(){
     AlertDialog.Builder message = new AlertDialog.Builder(context);
     message.setTitle("choose a color");
     message.setMessage("Colors:");
     message.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

             invalidate();
         }
     });
     message.setPositiveButton("Change color to red", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
           Point redPointt  =new Point();
           redPointt.x = (int)eventX;
           redPointt.y = (int)eventY;
           redPointList.add(redPointt);
             previewLine.removeAll(previewLine);
             invalidate();
         }
     });
     message.setNeutralButton("Change color to blue", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {


             Point bluePointt  =new Point();
             bluePointt.x = (int)eventX;
             bluePointt.y = (int)eventY;
             bluePointList.add(bluePointt);
             previewLine.removeAll(previewLine);
             invalidate();
         }
     });
     message.create();
     message.show();
 }





}

1 个答案:

答案 0 :(得分:0)

在此处更改您的代码逻辑。我没有测试过这段代码。但是你可以使用这个逻辑来实现

        boolean hasStartPoint= false;
        int startPointX =0;
        int startPointY=0;
        switch(event.getAction()){
            float eventX = event.getX();
            float eventY = event.getY();

            case MotionEvent.ACTION_DOWN:
                if(hasStartPoint) {
                    path.lineTo(eventX, eventY);
                }
                hasStartPoint=false;
                break;

            case MotionEvent.ACTION_MOVE:

                break;

            case MotionEvent.ACTION_UP:

                if(!hasStartPoint) {
                    startPointX = eventX;
                    startPointY = eventY;
                }
                hasStartPoint = true;

                break;




            default:
                return false;
        }