0

我正在使用这个应用程序在 android 中绘制自由线

抽屉视图.java

public class DrawerView extends View {

public static  Paint paint;
Paint paint2 = new Paint();
public Path path = new Path();
public Path circlePath = new Path();

public static int lineCol = Color.BLUE;

public static boolean isTouchable = false;

public LayoutParams params;


public DrawerView(Context context, AttributeSet attrs){
    super(context, attrs);

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(lineCol);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(4f);

}


public void onButtonPress(){
    // resets the screen
    path.reset();

    // Calls the onDraw() method
    postInvalidate();
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (!isTouchable){ 
        return false;
    } else {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(pointX, pointY);

        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(pointX, pointY);
        circlePath.reset();

        circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
        break;

    case MotionEvent.ACTION_UP:
        circlePath.reset();

        break;
    default:
        return false;
    }

    postInvalidate();
    return true;
    }
  }
}

画线.java

public class DrawLines extends Activity {

DrawerView myView;
public Button btnReset;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.draw_line);

    myView = (DrawerView)findViewById(R.id.custView);

    btnReset = (Button) findViewById(R.id.button1);

    btnReset.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            // resets the screen
            myView.path.reset();

            // Calls the onDraw() method
            myView.postInvalidate();

        }
    });
}

}

draw_line.xml

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Clear" />

<com.example.drawline.DrawerView
    android:id="@+id/custView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_margin="5dp"
    custom:lineColor="#ff0099" />

它对我很有效,但我希望能够选择线条的颜色,所以我在 draw_line.xml 中添加了一个按钮,当按下它时会显示一个对话框,要求用户选择颜色。这是对话框的代码

public void openSelectColorDialog()
{
    final Dialog d = new Dialog(this);
    d.setTitle("Select Color");
    d.setContentView(R.layout.military_list);

    int image[] = { R.drawable.black, R.drawable.blue, R.drawable.yellow};

    ArrayList<HashMap<String, String>> objArayList = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i < image.length; i++) {
        HashMap<String, String> listData = new HashMap<String, String>();
        listData.put("image", Integer.toString(image[i]));

        objArayList.add(listData);

    }

    String[] from = { "image"};
    int[] to = { R.id.list_image };

    SimpleAdapter listAdapter = new SimpleAdapter(this, objArayList,
            R.layout.military_list_item, from, to);
    ListView lst1 = (ListView) d.findViewById(android.R.id.list);

    lst1.setAdapter(listAdapter);

    lst1.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {

            if (position == 0) {
                // the code of color line

                d.dismiss();

            } else if (position == 1) {
                // the code of blue line

                d.dismiss();

            } else {
                // the code of yellow line
                d.dismiss();

            }

            myView.postInvalidate();

        }
    });

    d.show();

}

我必须添加什么代码才能使颜色像我选择的那样?

考虑我是否编写此代码 DrawerView.paint.setColor(Color.BLACK); 整条线将其颜色更改为所选颜色,这不是我想要的。我希望新线的颜色与我选择的颜色一样,并保持旧线的颜色。

希望有人明白我的意思。

提前致谢。

编辑

新代码是:ColoredPath.java

public class ColoredPath {

private Paint paint;
private Path path = new Path();
private int color;

public ColoredPath() {
    paint = new Paint();
    color = Color.BLACK;

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(4f);
    paint.setAntiAlias(true);
    paint.setColor(color);
}

public ColoredPath(int color) {
    paint = new Paint();

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(4f);
    paint.setAntiAlias(true);
    paint.setColor(color);
}

public void setColor(int color){
    this.color = color;
}

public int getColor(){
    return color;
}


public void setPaint(Paint paint){
    this.paint = paint;
}

public Paint getPaint() {
    return paint;
}

public void setPath(Path path){
    this.path = path;
}

public Path getPath() {
    return path;
}
}

抽屉视图.java

    public class DrawerView extends View {

public Path circlePath = new Path();

public static int LINE_COLOR = 0;

public static boolean isTouchable = false;


public List<ColoredPath> paths = new ArrayList<ColoredPath>();

public ColoredPath currentPath;

public DrawerView(Context context, AttributeSet attrs){
    super(context, attrs);

    currentPath = new ColoredPath();
    paths.add(currentPath);


}   

public void newLine(int color){
    System.out.println("in newLine method");
    currentPath = new ColoredPath(color);
    paths.add(currentPath);
    //postInvalidate();
}

public void onButtonPress(){
    // resets the screen
    currentPath.getPath().reset();

    // Calls the onDraw() method
    postInvalidate();
}

@Override
protected void onDraw(Canvas canvas) {

    System.out.println("size of paths: "+paths.size());

    canvas.drawPath(currentPath.getPath(), currentPath.getPaint());
    for(int i = 0; i < paths.size(); ++i) {
          //ColoredPath coloredPath = paths.get(i);
          canvas.drawPath(currentPath.getPath(), currentPath.getPaint());
        }
}


  public Path getLastPath() {
    Path path = new Path();
    for(int i = 0; i < paths.size(); ++i) {
      path.addPath(paths.get(i).getPath());
    }
    return path;
  }

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (!isTouchable){ 
        return false;
    } else {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        currentPath.getPath().moveTo(pointX, pointY);
        //getLastPath().moveTo(pointX, pointY);

        return true;
    case MotionEvent.ACTION_MOVE:
        currentPath.getPath().lineTo(pointX, pointY);
        //getLastPath().lineTo(pointX, pointY);
        circlePath.reset();

        circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
        break;

    case MotionEvent.ACTION_UP:
        circlePath.reset();

        break;
    default:
        return false;
    }

    postInvalidate();
    return true;
    }
}
   }

并在 MainActivity.java

 if (position == 1) {
  // blue
 myView.newLine(Color.BLUE);
 d.dismiss();
}

这很好用并且为线条着色,但是当我选择任何颜色时,它会导致删除最后一行,我希望所有线条都带有颜色。

4

1 回答 1

1

因为 Path 本身不包含有关其颜色的任何信息(颜色由 Canvas 管理),所以我建议您为每个具有不同绘制的新路径创建新的 (Path, Paint) 对,并在 onTouch 事件中仅使用当前对。所以这里有一些代码可以说明这个想法

public class ColoredPath {

    private Paint paint;
    private Path path;

    public ColoredPath(Paint paint, Path path) {
        this.paint = paint;
        this.path = path;
    }

    public Paint getPaint() {
        return paint;
    }

    public Path getPath() {
        return path;
    }
}

private List<ColoredPath> paths = new ArrayList<ColoredPath>();

public DrawerView(Context context, AttributeSet attrs){
    super(context, attrs);

    //custom paint can be used here
    paths.add(new ColoredPath(new Paint(), new Path()));
}

/////
@Override
protected void onDraw(Canvas canvas) {
    for(ColoredPath coloredPath : paths) {
        canvas.drawPath(coloredPath.getPath(), coloredPath.getPaint());
    }
}

路径在哪里List<ColoredPath>。而且你还需要像ColoredPath currentPath.

于 2013-07-20T13:09:29.467 回答