second day android

  artboard

Today I learned the drawing board, which is mainly divided into three parts (Color, Paint, Canvas), namely (color, brush, canvas).

 

 

Here's a simple artboard I made today:


 

 

 

 

The implementation of the specific code is implemented by modifying the existing code. The original code can only draw one stroke, and then the drawing will refresh the original drawing, and I have roughly implemented the following newly added functions

 

 

Continuous painting:

The original code is an array of newly created storage points each time, so I changed it to use an array to store the points, that is

private ArrayList<Point> pointall=new ArrayList<Point>();

However, just changing this one will cause all the points to be connected into a line, so I rewrite the onDraw function. The modified code is as follows (when the mouse is clicked, a (0, 0) point is added as the mark, judge whether it is another stroke):

protected void onDraw(Canvas canvas){  

        if(pointall.size()>1){  

            Iterator<Point> iter=pointall.iterator();// Take out the array of storage points

            Point first=null;  

            Point last=null;  

            while(iter.hasNext()){  

                if(first==null){  

                while(iter.hasNext()){ //Get the beginning of a line that is not (0, 0)

                first=(Point)iter.next(); 

                if(first.x!=0||first.y!=0) break;

                } 

                }  

                else{   

                    if(last!=null){  

                    first=last; //Assign the next coordinate point to the above  

                   }  

                last=(Point)iter.next(); //Get the next point

 

                if(last.x==0&&last.y==0){     //判断

                while(iter.hasNext()){

                first=(Point)iter.next(); 

                if(first.x!=0||first.y!=0) break;

                }

 

                if(iter.hasNext()){

                last=(Point)iter.next();  

                }

                else break;

                }

                canvas.drawLine(first.x, first.y, last.x, last.y,pa);           

                }  

            }  

        }  

    }

Although the code is simple, it still encounters various errors when writing it, sometimes it crashes, sometimes it connects (0, 0) and other points into a straight line, etc... Now this code, It's roughly okay

 

 

Modify the color and size of the brush:

目前做的是可以改颜色和大小的,但是一改,以前画的也会改变,这个是因为代码中用的是重构,即重新画,而不是在现有的画上添加,而在重新画的时候,是以你现在的颜色和大小来画的,会丢失前面的颜色和大小这些信息,如果要解决,则要新建一些空间来存放这些信息,比如类和数组,之后在根据这些信息来画,这样就不会改变以前的画的了,但是目前水平还一点欠缺,没有完成,现在只能统一改变颜色和大小,下面是这部分的代码:

 

public static void change_big(String string) {

 pa.setStrokeWidth(Integer.parseInt(string));      //修改画笔大小的函数(pa是我定义为全局变量的画笔)

}  

 

 

  static public void change(String a) {

 

 if(a.equals("BLACK"))     pa.setColor(Color.BLACK);    //修改画笔颜色的函数

 if(a.equals("DKGRAY"))    pa.setColor(Color.DKGRAY);

 if(a.equals("GRAY"))      pa.setColor(Color.GRAY);

 if(a.equals("LTGRAY"))    pa.setColor(Color.LTGRAY);

 if(a.equals("RED"))       pa.setColor(Color.RED);

 pa.setStrokeWidth(22);

}

 

 

//绑定多选框和与其对应的函数--------颜色

        Spinner spinner = (Spinner) findViewById(R.id.spinner2);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, 

                    int pos, long id) {

           

                String[] languages = getResources().getStringArray(R.array.languages);

                Toast.makeText(draw_main.this, "你点击的是:"+languages[pos], 2000).show();

                draw.change(languages[pos]);

 

            }

 

            public void onNothingSelected(AdapterView<?> parent) {

            }

        }

        );

        

        

        

//绑定多选框和与其对应的函数--------大小

        Spinner big = (Spinner) findViewById(R.id.spinner1);

        big.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, 

                    int pos, long id) {

                String[] big = getResources().getStringArray(R.array.big);

                Toast.makeText(draw_main.this, "你点击的是:"+big[pos], 2000).show();        

                draw.change_big(big[pos]);

            }

            @Override

            public void onNothingSelected(AdapterView<?> parent) {

                // Another interface callback

            }

        }

        

        );

 

除此之外,我还用到了多选框的一些东西(样式很丑,但不知道怎么改)(虽然简单,但还是记录一下):

多选框:

    <Spinner

        android:id="@+id/spinner1"    //id

        android:layout_width="90dp"

        android:layout_height="50dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

 

        android:layout_marginLeft="100dp"     //控制位置的

        android:entries="@array/big"     //对应存放数据的位置

         />

 

存放数据,在res--values下面,可以自己建立一个新的xml来存放:

        <string-array name="big">   //名称,要和前面对应

        <item>10</item>      //数据(字符串格式)

        <item>20 </item>

        <item>30</item>

        <item>40</item>

        <item>50</item>

    </string-array>

 

 

ps:刚刚遇到的不能不同线条不同颜色和粗细的问题,已经找到解决方法了,

思路:因为颜色和粗细也i是int类型的,所以可以再加一个点来存储该信息,不过要注意类型的转换和大小!!!

具体见

http://sixsixccff.iteye.com/blog/2382143

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326158543&siteId=291194637