Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android canvas/Paint-更改Android canvas笔刷的笔刷大小_Android_Canvas_Paint - Fatal编程技术网

Android canvas/Paint-更改Android canvas笔刷的笔刷大小

Android canvas/Paint-更改Android canvas笔刷的笔刷大小,android,canvas,paint,Android,Canvas,Paint,在“我的绘制活动”类中,当选择微调器中的项目时,我的笔刷大小将更改。但是,它也会更改以前绘制的路径的大小。我尝试为微调器中的每个选择创建一个新的绘制对象,但仍然不起作用。这里tv是eventTouchView类的一个实例,它具有draw方法。不确定出了什么问题: 在我的绘画活动课上: @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

在“我的绘制活动”类中,当选择微调器中的项目时,我的笔刷大小将更改。但是,它也会更改以前绘制的路径的大小。我尝试为微调器中的每个选择创建一个新的绘制对象,但仍然不起作用。这里tv是eventTouchView类的一个实例,它具有draw方法。不确定出了什么问题: 在我的绘画活动课上:

      @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        tv.paint= new Paint();
        tv.paint.setStyle(Paint.Style.STROKE);
       tv.paint.setStrokeJoin(Paint.Join.ROUND);

        if ( arg0.getSelectedItem().equals("10f")){

            tv.paint.setStrokeWidth(10f);

        }
        else if ( arg0.getSelectedItem().equals("20f")){

            tv.paint.setStrokeWidth(20f);
        }
        else if ( arg0.getSelectedItem().equals("40f")){

            tv.paint.setStrokeWidth(40f);
        }
        else if ( arg0.getSelectedItem().equals("50f")){

            tv.paint.setStrokeWidth(50f);
        }
        else {

            tv.paint.setStrokeWidth(30f);
        }
    }
-------------------------------- 新版本:我尝试了下面的步骤,但不确定下面的代码有什么问题-

@SuppressLint("DrawAllocation")
protected void onDraw(Canvas canvas){

    pathToGrayscale();
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(grayscaleBmp, 0, 100, null);
    for (int i =0; i < drawings.size(); i++){
        canvas.drawPath(drawings.get(i).getPath(), drawings.get(0).getPaint());

    }
    //canvas.drawPath(path, paint);
    canvas.drawPath(cursor, cursorPaint);

}

 public boolean onTouchEvent(MotionEvent event){

    float Xpos = event.getX();
    float Ypos = event.getY();
    //drawings = new Vector<Drawing>();
    if (selection == 10){
        Drawing draw1 = new Drawing();
        draw1.getPaint().setStrokeWidth(10f);
        draw1.getPaint().setStyle(Paint.Style.STROKE);
        draw1.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw1);
    }
    else if (selection == 20){
        Drawing draw2 = new Drawing();
        draw2.getPaint().setStrokeWidth(20f);
        draw2.getPaint().setStyle(Paint.Style.STROKE);
        draw2.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw2);

    }
    else if (selection == 30){
        Drawing draw3 = new Drawing();
        draw3.getPaint().setStrokeWidth(30f);
        draw3.getPaint().setStyle(Paint.Style.STROKE);
        draw3.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw3);

    }
    else if (selection == 40){
        Drawing draw4 = new Drawing();
        draw4.getPaint().setStrokeWidth(40f);
        draw4.getPaint().setStyle(Paint.Style.STROKE);
        draw4.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw4);

    }
    else if (selection == 50){
        Drawing draw5 = new Drawing();
        draw5.getPaint().setStrokeWidth(50f);
        draw5.getPaint().setStyle(Paint.Style.STROKE);
        draw5.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw5);

    }
    else{
        Drawing draw6 = new Drawing();
        draw6.getPaint().setStrokeWidth(70f);
        draw6.getPaint().setStyle(Paint.Style.STROKE);
        draw6.getPaint().setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw6);

    }


    //ArrayList <Pair<Float, Float>> pathPixels = new ArrayList <Pair<Float, Float>>();
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        drawings.get(drawings.size()-1).getPath().moveTo(Xpos, Ypos);
        xPathPixels.add(Xpos);
        yPathPixels.add(Ypos);
        //int grayPixel = grayBmp.getPixel(Math.round(Xpos), Math.round(Ypos));
        //resizedBmp.setPixel(Math.round(event.getX()),  Math.round(event.getY()), grayPixel);
        return true;


    case MotionEvent.ACTION_MOVE:
        drawings.get(drawings.size()-1).getPath().lineTo(Xpos, Ypos);
        xPathPixels.add(Xpos);
        yPathPixels.add(Ypos);
        //pathToGrayscale();

        //int grayPixel2 = grayBmp.getPixel(Math.round(Xpos), Math.round(Ypos));
        //resizedBmp.setPixel(Math.round(event.getX()),  Math.round(event.getY()), grayPixel2);
        cursor.reset();

        cursor.addCircle(Xpos, Ypos, 30, Path.Direction.CW);

        break;

    case MotionEvent.ACTION_UP:
        //pathToGrayscale();
        System.out.println("xPath : " + xPathPixels + " " + "yPath : " + yPathPixels);
        cursor.reset();

        break;

    default:
        return false;

    }

    invalidate();

    return true;

}
@SuppressLint(“DrawAllocation”)
受保护的void onDraw(画布){
pathToGrayscale();
画布。drawColor(颜色。白色);
drawBitmap(灰度位图,0,100,空);
对于(int i=0;i
您需要进行一些更改-

(1.)在项目中创建一个模型类,让我们说它是“绘制”的,并包装了Paint和Path实例

    class Drawing {
        Paint paint;
        Path path;
        //Getter & Setter
    }
(2.)在ViewTouchEvent类中创建类型图形列表

    Vector<Drawing> drawings = new Vector<Drawing>();
矢量图=新矢量();
(3.)捕获onTouchEvent()并创建图形对象及其相应的路径和绘制实例。 (4.)在这里,每张图纸都有自己的油漆特性,并不常见

(5.)现在,将此图形实例添加到drawingList

(6.)调用invalidate(),这将立即调用onDraw

(7.)在这里,您应该迭代列表,并为每个图形实例调用getPath()和getPaint() 可以使用特定的绘制和路径对象绘制所有图形


注意:您还必须维护一个当前图形,以便可视化当前绘制的路径。

遵循sdkThanks中的fingerpaint示例!youtube上的Fingerpaint示例?在android sdk/Samples下这里是Fingerpaint源代码,谢谢!!我来看看感谢您的详细回复!我试着做了以上的事情,但是由于某种原因仍然不起作用。我不确定我是否理解为什么我们要在onDraw方法()中遍历图形列表?因为每次我们创建一个图形时,它都会直接转到onDraw方法。因此,如果我们在图形向量上调用onDraw方法,那么它将重复以前绘制的一些路径?此外,我已根据上面的步骤在“新版本”行下添加了新的修订代码。非常感谢您的详细回复!我的代码正常工作了!:D@user37375很高兴知道……)
    Vector<Drawing> drawings = new Vector<Drawing>();