This little artboard of mine has two functions: changing the color of the brush and changing the thickness of the brush. As for how to change the color of the brush, my idea is to first control the color of the brush through a few small buttons, so that we can know how to color the line in the artboard component we wrote. Then change the way to change the brush color (using a drag bar, etc.). For the thickness of the brush, I determined it by adding a button to an input box. The final effect is as follows:
1. DrewView class;
This class inherits the View class. Two methods are opened, changecolor() and changewidth(). These two functions are used to change the color and thickness of the brush, respectively.
package com.example.study; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; @SuppressLint("ClickableViewAccessibility") public class Drawview extends View{ private List<Point> pointall=new ArrayList<Point>(); int color; float width; public Drawview(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub super.setBackgroundColor(Color.WHITE); super.setOnTouchListener(new Touch()); color=Color.BLACK; } public void changecolor(int color)//Modify the color of the brush { this.color=color; } public void changewidth(float width)//Modify the thickness of the brush { this.width=width; } private class Touch implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent e) { // TODO Auto-generated method stub Point p=new Point((int)e.getX(),(int)e.getY()); if(e.getAction()==e.ACTION_DOWN){ //When pressed pointall=new ArrayList<Point>(); pointall.add(p); } else if(e.getAction()==e.ACTION_UP){//When lifted pointall.add(p); Drawview.this.postInvalidate(); //Redraw } else if(e.getAction()==e.ACTION_MOVE){ pointall.add(p); //When moving Drawview.this.postInvalidate(); //Redraw } return true; } } protected void onDraw(Canvas canvas){ Paint p=new Paint(); p.setColor(color);//Define the color p.setStrokeWidth(width); if(pointall.size()>1){ Iterator<Point> iter=pointall.iterator();// Now you can start drawing when there are coordinate points saved Point first=null; Point last=null; while(iter.hasNext()){ if(first==null){ first=(Point)iter.next(); } else{ if(last!=null){ first=last; //Assign the next coordinate point to the above } last=(Point)iter.next(); //Do not stop pointing canvas.drawLine(first.x, first.y, last.x, last.y,p); } } } } }
二、activity_home.xml
This is the page of the drawing board, which uses the newly created DrewView class (this is because the DrewView inherits the View class, so it is used in the same way as TextView).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.study.Home12" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="34dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Color of your choice:" /> <TextView android:id="@+id/test" android:layout_width="50dp" android:layout_height="fill_parent" /> <Button android:id="@+id/standard_color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="chose_color" android:text="Choose standard color" /> </LinearLayout> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="16777215" android:progress="0" /> <LinearLayout android:id="@+id/linearLayout2" android:layout_below="@id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="34dp" > <EditText android:id="@+id/width" android:layout_width="200dp" android:layout_height="wrap_content" android:inputType="numberDecimal" android:text="1.0" /> <Button android:id="@+id/changewidth" android:text="@string/width" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="width" android:textSize="12sp" /> </LinearLayout> <com.example.study.Drawview android:id="@+id/paintview" android:layout_width="fill_parent" android:layout_height="500dp" android:layout_below="@id/linearLayout2" /> </RelativeLayout>
3. home12.java
This is the activity that matches activity_home.xml.
package com.example.study; import com.example.study.Drawview; import com.example.study.R; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; public class Home12 extends Activity { Drawview dv=null;//Used to store canvas view variables EditText wid=null; SeekBar sb=null; TextView test=null; int color=0x00000000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_home); dv=(Drawview)super.findViewById(R.id.paintview);//Get the canvas view wid=(EditText)super.findViewById(R.id.width);//Get the input brush width value sb=(SeekBar)super.findViewById(R.id.seekBar1);//Get the drag bar view sb.setOnSeekBarChangeListener(new seekbar());//Add a listener for the drag bar view //test=(TextView)super.findViewById(R.id.test); } private class seekbar implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub dv.changecolor(progress+0xff000000); test.setBackgroundColor(progress+0xff000000); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void width(View view) { float width; if(wid.getText()!=null) { width=Float.parseFloat(wid.getText().toString().trim()); dv.changewidth(width); } } public void chose_color(View view) { String[] items={"black","red","green","blue"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("Select standard color"); builder.setIcon(R.drawable.ic_launcher); builder.setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub int color=0x000000; switch(which) { case 0: color=0; break; case 1: color=16711680; break; case 2: color=65280; break; case 3: color=255; break; } dv.changecolor(color+0xff000000); test.setBackgroundColor(color+0xff000000); sb.setProgress(color); Toast t=Toast.makeText(Home12.this, "color is:"+color, Toast.LENGTH_SHORT); t.show(); } }); builder.setPositiveButton("yes", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.setNeutralButton("temp2", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.setNegativeButton("no", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); //setPositiveButton(builder); builder.show(); } }