如何画出不同颜色和大小的线条?

时间:2014-08-07 05:22:05

标签: android drawing android-canvas

当我最初选择红色并画一条线,然后选择蓝色并画一条线时,现在旧线也变为蓝色而不是红色,但是我希望红色保持红色,蓝色为这样可以帮助吗?

我也尝试在线获取此解决方案而不是解决我的问题。请帮助我。

DrawingView.java

public class DrawingView extends View {

Paint mPaint;
Path mpath;

 Canvas mCanvas;
 Bitmap myBitmap;


//initial color
 private int paintColor = 0xFF660000;
 private ArrayList<PathWithPaint> _graphics1 = new ArrayList<PathWithPaint>(); 


 private ArrayList<Path> paths = new ArrayList<Path>();
 private ArrayList<Path> undonePaths = new ArrayList<Path>(); 
// private Map<Path,Float> stoWid = new HashMap<Path,Float>();
 private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();



 public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
     Drawingsetup();
 }

 private void Drawingsetup() {
    // TODO Auto-generated method stub
      mPaint = new Paint();
      mpath = new Path();
      mCanvas = new Canvas();

      mPaint.setDither(true);  
      mPaint.setColor(0xFFFFFFFF);  
      mPaint.setStyle(Paint.Style.STROKE);  
      mPaint.setStrokeJoin(Paint.Join.BEVEL);  
      mPaint.setStrokeCap(Paint.Cap.ROUND);  
      mPaint.setStrokeWidth(3); 


     }  

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO Auto-generated method stub
    super.onSizeChanged(w, h, oldw, oldh);

      myBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);  
      mCanvas = new Canvas(myBitmap);
      mCanvas.drawBitmap(myBitmap, 20, 20, mPaint);

}



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        //return super.onTouchEvent(event);

           PathWithPaint pp = new PathWithPaint();  
           //mCanvas.drawPath(path, mPaint);  

           if (event.getAction() == MotionEvent.ACTION_DOWN) {  
            undonePaths.clear();
            mpath.moveTo(event.getX(), event.getY());  
            mpath.lineTo(event.getX(), event.getY());  

         } else if (event.getAction() == MotionEvent.ACTION_MOVE) {  
            mpath.lineTo(event.getX(), event.getY()); 

            pp.setPath(mpath);  
            pp.setmPaint(mPaint);  
            _graphics1.add(pp);


           } else if (event.getAction()==MotionEvent.ACTION_UP) {

               mpath.lineTo(event.getX(), event.getY());
                // commit the path to our offscreen
                mCanvas.drawPath(mpath, mPaint);
                // kill this so we don't double draw
                paths.add(mpath);
                mpath = new Path(); 

                colorsMap.put(mpath,paintColor); // store the color of mPath

        }
           invalidate();  
           return true;

    }



    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //canvas.drawBitmap(myBitmap, 0, 0, mPaint);

        for(Path p: paths){

             canvas.drawPath(p, mPaint);
            mPaint.setColor(colorsMap.get(p));
             }

            canvas.drawPath(mpath, mPaint);
            canvas.setColor(paintColor);
      }  

    public void CurrentDraw(){

            }

    public void StartNew(){
        //colorsMap.clear();
         undonePaths.clear();
         paths.clear();

         myBitmap.eraseColor(Color.TRANSPARENT);
         mpath.reset();
         invalidate();
       _graphics1.clear();

    }



    public void ColorChanged(String newColor){
        //set color
        // start by invalidating the View

        //parse and set the color for drawing
        paintColor = Color.parseColor(newColor);
        mPaint.setColor(paintColor);
        invalidate();

        }

    public void onClickUndo()
        {
            if (paths.size()>0) { 
                undonePaths.add(paths.remove(paths.size()-1));
                invalidate();
            }
                else
                {

                }

        }

    public void onClickRedo (){
            if (undonePaths.size()>0) { 
                paths.add(undonePaths.remove(undonePaths.size()-1)); 
                invalidate();
            } 
            else
            {

            }
        }
}

DrawingActivity.java

 public class DrwaingActivity extends Activity    {


DrawingView drawView;
Bitmap myBitmap;
private ImageButton currentpaint ;

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

        //get drawing view
         drawView = (DrawingView)findViewById(R.id.drawing);
        // drawView.setOnClickListener(this);

        //get the palette and first color button
            LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
            currentpaint = (ImageButton)paintLayout.getChildAt(0);
            currentpaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
 }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_item, menu);

        return true;
    }



    public void paintClicked(View view){


        if(view!=currentpaint)
            {
            //update color
            // retrieve the tag we set for each button in the layout, representing the chosen color
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();

            // call the new method on the custom drawing View object
            drawView.ColorChanged(color);

            // update the UI to reflect the new chosen paint and set the previous one back to normal
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
            currentpaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
            currentpaint=(ImageButton)view;
            }

     }       

0 个答案:

没有答案