Android的画布/油漆 - Android的画布上刷改变笔刷大小 [英] Android canvas / Paint - Changing brush size of Android canvas brush

查看:334
本文介绍了Android的画布/油漆 - Android的画布上刷改变笔刷大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的抽签活动课的时候从微调一个项目被选中,我的画笔大小的变化。然而,它也改变了previously drawed路径的大小。我试图创建一个新的油漆对象从微调每一个选择,但仍然无法正常工作。这里电视是具有拉伸方法的eventTouchView类的一个实例。不知道什么是错的: 在我的抽奖活动类:

  @覆盖
    公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,
            长ARG3){
        // TODO自动生成方法存根
        tv.paint =新的油漆();
        tv.paint.setStyle(Paint.Style.STROKE);
       tv.paint.setStrokeJoin(Paint.Join.ROUND);

        如果(arg0.getSelectedItem()。等于(10F)){

            tv.paint.setStrokeWidth(10F);

        }
        否则,如果(arg0.getSelectedItem()。等于(20F)){

            tv.paint.setStrokeWidth(20F);
        }
        否则,如果(arg0.getSelectedItem()。等于(40F)){

            tv.paint.setStrokeWidth(40F);
        }
        否则,如果(arg0.getSelectedItem()。等于(50F)​​){

            tv.paint.setStrokeWidth(50F);
        }
        其他 {

            tv.paint.setStrokeWidth(30F);
        }
    }
 

在我ViewTouchEvent类:

 公共类ViewTouchEvent扩展视图{

    涂料粉刷;
    路径path =新路径();

    保护无效的OnDraw(帆布油画){
        pathToGrayscale();
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(grayscaleBmp,0,100,NULL);
        canvas.drawPath(路径,油漆);
        canvas.drawPath(光标,cursorPaint);

    }
 

--------------------------------

新版本:我想下面的步骤,但不知道什么是错低于code -

  @燮pressLint(DrawAllocation)
保护无效的OnDraw(帆布油画){

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

    }
    //canvas.drawPath(path,油漆);
    canvas.drawPath(光标,cursorPaint);

}

 公共布尔的onTouchEvent(MotionEvent事件){

    浮XPOS = event.getX();
    浮Ypos = event.getY();
    //图纸=新矢量<制图与GT;();
    如果(选择== 10){
        绘图DRAW1 =新图();
        draw1.getPaint()setStrokeWidth(10F)。
        。draw1.getPaint()的setStyle(Paint.Style.STROKE);
        。draw1.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(DRAW1);
    }
    否则,如果(选择== 20){
        绘图DRAW2 =新图();
        draw2.getPaint()setStrokeWidth(20F)。
        。draw2.getPaint()的setStyle(Paint.Style.STROKE);
        。draw2.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(DRAW2);

    }
    否则,如果(选择== 30){
        绘图draw3 =新图();
        draw3.getPaint()setStrokeWidth(30°F)。
        。draw3.getPaint()的setStyle(Paint.Style.STROKE);
        。draw3.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw3);

    }
    否则,如果(选择== 40){
        绘图draw4 =新图();
        draw4.getPaint()setStrokeWidth(40F)。
        。draw4.getPaint()的setStyle(Paint.Style.STROKE);
        。draw4.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw4);

    }
    否则,如果(选择== 50){
        绘图draw5 =新图();
        draw5.getPaint()setStrokeWidth(50F)。
        。draw5.getPaint()的setStyle(Paint.Style.STROKE);
        。draw5.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw5);

    }
    其他{
        绘图draw6 =新图();
        draw6.getPaint()setStrokeWidth(70F)。
        。draw6.getPaint()的setStyle(Paint.Style.STROKE);
        。draw6.getPaint()setStrokeJoin(Paint.Join.ROUND);
        drawings.add(draw6);

    }


    // ArrayList的<对<浮球,浮球>> pathPixels =新的ArrayList<对<浮球,浮球>>();
    开关(event.getAction()){
    案例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);
        返回true;


    案例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);

        打破;

    案例MotionEvent.ACTION_UP:
        // pathToGrayscale();
        的System.out.println(XPATH:+ xPathPixels ++yPath:+ yPathPixels);
        cursor.reset();

        打破;

    默认:
        返回false;

    }

    无效();

    返回true;

}
 

解决方案

您将需要作出一些改变 -

(1)在项目中创建一个模型类,您说绘图有包裹油漆和路径实例

 类绘制{
        涂料粉刷;
        路径道路;
        //吸气和放大器;二传手
    }
 

(2)创建型绘图列表中ViewTouchEvent类。

 矢量<制图与GT;图=新的矢量<制图与GT;();
 

(3)捕获的onTouchEvent()和与其相应的路径和油漆的情况下创建图形对象。 (4)在这里,每个绘图将拥有自己的涂料性能,而不是常见的。

(5)。现在,这个绘图实例添加到drawingList

(6)呼叫无效(),它将会调用OnDraw的。

(7)。在这里,您应该遍历列表并调用getPath()和getPaint()为每个绘图实例     您可以绘制所有的绘图与他们特定的油漆和路径的对象。

注意:您将不得不维护一个当前图形中也为当前路径的可视化正在制定

In my Draw Activity class when an item from the spinner is selected, my brush size changes. However it also changes the size of the previously drawed path. I tried to create a new paint object for every selection from the spinner, but still doesn't work. Here tv is an instance of the eventTouchView class which has the draw method. Not sure what is wrong: In my Draw Activity class :

      @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);
        }
    }

In my ViewTouchEvent class:

public class ViewTouchEvent extends View{

    Paint paint;
    Path path = new Path();

    protected void onDraw(Canvas canvas){
        pathToGrayscale();
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(grayscaleBmp, 0, 100, null);
        canvas.drawPath(path, paint);
        canvas.drawPath(cursor, cursorPaint);

    }

--------------------------------

New Version: I tried the steps below, but not sure what is wrong with the code below -

@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;

}

解决方案

You will need to make some changes -

(1.) Create a model class in your project lets say it "Drawing" having Paint and Path instances wrapped

    class Drawing {
        Paint paint;
        Path path;
        //Getter & Setter
    }

(2.) Create a list of type Drawing in ViewTouchEvent class.

    Vector<Drawing> drawings = new Vector<Drawing>();

(3.) Capture onTouchEvent() and Create your Drawing object with its corresponding Path and paint instances. (4.) Here each Drawing will have its own paint properties and not common.

(5.) Now, add this drawing instance to drawingList.

(6.) Call invalidate(), this will call onDraw now.

(7.) Here you should iterate over the list and calling getPath() and getPaint() for each drawing instance you can draw all your drawing with their specific paint and path objects.

NOTE : You will have to maintain one current drawing also for visualisation of current path being drawn.

这篇关于Android的画布/油漆 - Android的画布上刷改变笔刷大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆