Android使用ontouch在画布上绘制矩形

Posted

技术标签:

【中文标题】Android使用ontouch在画布上绘制矩形【英文标题】:Android Draw rectangle on canvas using ontouch 【发布时间】:2013-08-08 09:21:50 【问题描述】:

我需要在画布上绘制一个矩形,其中包含来自 ontouch 事件的点。我得到了矩形形状,但是当我绘制另一个矩形时,前一个矩形消失了。我想我需要将矩形点存储在某个数组中。请给我一些建议。

到目前为止,我的代码如下。

视图类中的ontouch方法中的第一个

private void onTouchEvent(MotionEvent event) 
    xTouchCoordinate = event.getX();
    yTouchCoordinate = event.getY();
 if (event.getAction() == MotionEvent.ACTION_DOWN) 
        isActionUp = false;
        pushState ();

        startX = event.getX();
        startY = event.getY();
 
else if (event.getAction() == MotionEvent.ACTION_UP) 
isActionUp = true; 

else if (event.getAction() == MotionEvent.ACTION_MOVE)
        isActionUp = false;
if(mTouchMode == TouchModes.RECTANGLE )
        
            updateRectPath(startX,startY , xTouchCoordinate , yTouchCoordinate);

        


而onDraw方法是

 protected void onDraw(Canvas canvas) 
    ImageObject.setInteractiveMode(true);
    int sc = canvas.save();
    canvas.drawColor(Color.BLACK);
    canvas.scale(mCanvasScale, mCanvasScale);
    canvas.translate(mCanvasOffset.x, mCanvasOffset.y);
    canvas.clipRect(mCanvasLimits);
    canvas.drawColor(Color.WHITE);

    if (currentState.drawGrid)
        drawGridLines (canvas);
    drawImages (canvas, true);
    drawLines (canvas);
    drawRectangles(canvas);
    drawCircles(canvas);
    drawOvals(canvas);
    drawImages (canvas, false);
    

DrawRectangles 方法

 private void drawRectangles(Canvas canvas) 
    try
    
        if(path!=null)
        
            if(mTouchMode == TouchModes.RECTANGLE)
            

                    finishPath(canvas);
            
        
    catch(Exception e)
    

    


完成路径方法

void finishPath(Canvas canvas) 




    if(mTouchMode == TouchModes.RECTANGLE)
    
        paint = new Paint();

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(currentState.currentColor);
        paint.setFlags(Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(currentStrokeWidth);

        canvas.drawPath(path, paint);

    



 void updateRectPath(float x1, float y1, float x2, float y2) 
    path.rewind();
    x1 = x1 - mCanvasOffset.x;
    y1 = y1 - mCanvasOffset.y;
    x2 = x2 - mCanvasOffset.x;
    y2 = y2 - mCanvasOffset.y;
    translate(x1, y1); 
    Log.d ("x1 is "+x1, "y1 is "+y1);


    if (x1 < x2 && y1 > y2) 
        path.addRect(x1, y2, x2, y1, Path.Direction.CW);
    
    if (x1 < x2 && y1 < y2) 
        path.addRect(x1, y1, x2, y2, Path.Direction.CW);
    
    if (x1 > x2 && y1 > y2) 
        path.addRect(x2, y2, x1, y1, Path.Direction.CW);
    
    if (x1 > x2 && y1 < y2) 
        path.addRect(x2, y1, x1, y2, Path.Direction.CW);
    




【问题讨论】:

【参考方案1】:
Hi if you have the source code of apidemos (or can find on internet), it has a fingerpaint example code. you can refer that: In any case i am pasting the code here..

/*
 * Copyright (C) 2007 The android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.graphics;

import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

public class FingerPaint extends GraphicsActivity
        implements ColorPickerDialog.OnColorChangedListener 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        mEmboss = new EmbossMaskFilter(new float[]  1, 1, 1 ,
                                       0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
    

    private Paint       mPaint;
    private MaskFilter  mEmboss;
    private MaskFilter  mBlur;

    public void colorChanged(int color) 
        mPaint.setColor(color);
    

    public class MyView extends View 

        private static final float MINP = 0.25f;
        private static final float MAXP = 0.75f;

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;

        public MyView(Context c) 
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) 
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        

        @Override
        protected void onDraw(Canvas canvas) 
            canvas.drawColor(0xFFAAAAAA);

            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            canvas.drawPath(mPath, mPaint);
        

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) 
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        
        private void touch_move(float x, float y) 
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) 
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            
        
        private void touch_up() 
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        

        @Override
        public boolean onTouchEvent(MotionEvent event) 
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) 
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            
            return true;
        
    

    private static final int COLOR_MENU_ID = Menu.FIRST;
    private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
    private static final int BLUR_MENU_ID = Menu.FIRST + 2;
    private static final int ERASE_MENU_ID = Menu.FIRST + 3;
    private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        super.onCreateOptionsMenu(menu);

        menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
        menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
        menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
        menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
        menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');

        /****   Is this the mechanism to extend with filter effects?
        Intent intent = new Intent(null, getIntent().getData());
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        menu.addIntentOptions(
                              Menu.ALTERNATIVE, 0,
                              new ComponentName(this, NotesList.class),
                              null, intent, 0, null);
        *****/
        return true;
    

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
        super.onPrepareOptionsMenu(menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);

        switch (item.getItemId()) 
            case COLOR_MENU_ID:
                new ColorPickerDialog(this, this, mPaint.getColor()).show();
                return true;
            case EMBOSS_MENU_ID:
                if (mPaint.getMaskFilter() != mEmboss) 
                    mPaint.setMaskFilter(mEmboss);
                 else 
                    mPaint.setMaskFilter(null);
                
                return true;
            case BLUR_MENU_ID:
                if (mPaint.getMaskFilter() != mBlur) 
                    mPaint.setMaskFilter(mBlur);
                 else 
                    mPaint.setMaskFilter(null);
                
                return true;
            case ERASE_MENU_ID:
                mPaint.setXfermode(new PorterDuffXfermode(
                                                        PorterDuff.Mode.CLEAR));
                return true;
            case SRCATOP_MENU_ID:
                mPaint.setXfermode(new PorterDuffXfermode(
                                                    PorterDuff.Mode.SRC_ATOP));
                mPaint.setAlpha(0x80);
                return true;
        
        return super.onOptionsItemSelected(item);
    

【讨论】:

嗨,如果这个答案符合您的目的,请接受它,以便其他人可以知道正确的答案并使用它..谢谢

以上是关于Android使用ontouch在画布上绘制矩形的主要内容,如果未能解决你的问题,请参考以下文章

android 画布区域,android:canvas绘制一个透明矩形并填充其余区域

如何在 android 画布上绘制圆角多边形?

Android UICanvas 画布 ⑦ ( Canvas 绘制显示区域 | Canvas 绘制矩形源码分析 )

通过按钮将矩形绘制到画布上

在画布上用鼠标绘制矩形与鼠标坐标不同步

如何从我的主类在画布上绘制一个矩形?