1

I need a drawing application something like below -

User touches (ACTION_DOWN) the screen at Point A and then hovers (ACTION_MOVE) the finger on screen. Let's say current touch point is Point B. Application should always draw a straight line between A and B. That is, Point A of line will be static, while Point B of line will keep moving with the finger (touch-point). Once user lifts the finger, line becomes stable. User can draw multiple lines like this. Also, user can select (in selection mode) any line and change it's location.

Please suggest the way forward. What kind of drawing should I do, line/point/path/sprite/anything else? It would be really helpful, if you can provide any link. It should work on v3.2 and above.

Regards, Prasoon

4

2 に答える 2

0

上記のコードでは、新しい線を引くと古い線が消えるので、これを使用してこの問題を解決してください.........。

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
Drawing drawing;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout linearLayout1= (LinearLayout) (findViewById(R.id.layout1));
        drawing=new Drawing(this);
        linearLayout1.addView(drawing);
        LinearLayout linearLayout2=(LinearLayout) (findViewById(R.id.layout2));
        linearLayout2.addView(new Drawing(this));
    }



class Drawing extends View{
    private Canvas mCanvas = null;
    private Path mPath = null;
    private Paint mBitmapPaint = null;
    private Bitmap mBitmap = null;
    private Bitmap bit=null;
    private Paint mPaint = null;
    private MainActivity baseMainActivity = null;

    public Drawing(Context c) {
        super(c);
        baseMainActivity=(MainActivity) c;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);//This sets the width of signature

        Display display =baseMainActivity.getWindowManager().getDefaultDisplay();

        mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);// 320*480  // For setting size of screen to draw Bitmap

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mCanvas = new Canvas(mBitmap);
        onDraw(mCanvas);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try{
            canvas.drawColor(Color.TRANSPARENT);//Color.WHITE //To change background color of Application
            if(bit!=null)
                canvas.drawBitmap(bit, 0, 0, mBitmapPaint);
            else
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        }catch(Exception e){}
        if(bit==null)
            canvas.drawPath(mPath, mPaint);

        bit=null;

    }
    private float mX, mY;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x+1;
        mY = y+1;

    }

    private void touch_upline(float x,float y) {
        mPath.lineTo(mX, mY);
        mCanvas.drawLine(mX, mY, x, y, mPaint);
        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:
            eraseAll();
            touch_upline(x, y);
            invalidate();


            break;
        case MotionEvent.ACTION_UP:
            touch_upline(x,y);
            invalidate();
            drawLineOnLayout1(x,y);
            break;
        }
        return true;
    }
private void drawLineOnLayout1(float x,float y)
{
    drawing.mPath.lineTo(mX, mY);
    drawing.mCanvas.drawLine(mX, mY, x, y, mPaint);
    drawing.mPath.reset();  
}
    public void eraseAll()
    {
        mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
        mCanvas = new Canvas(mBitmap);
    }

    public void startErasing()
    {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }
    public void initializePaintOnject()
    {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);
    }

}
}  

そしてmain.xmlはこれです

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>
     <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>

</RelativeLayout>
于 2012-08-21T11:26:17.493 に答える
0

これを使用してください。

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout relativeLayout= (RelativeLayout) (findViewById(R.id.mainLayout));
        relativeLayout.addView(new Drawing(this));
    }

}  

class Drawing extends View{
    private Canvas mCanvas = null;
    private Path mPath = null;
    private Paint mBitmapPaint = null;
    private Bitmap mBitmap = null;
    private Bitmap bit=null;
    private Paint mPaint = null;
    private MainActivity baseMainActivity = null;


    public interface onDrawingViewSingleTap{
        void onDrawingViewTap(float x , float y);
    }

    public Drawing(Context c) {
        super(c);
        baseMainActivity=(MainActivity) c;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);//This sets the width of signature

        Display display =baseMainActivity.getWindowManager().getDefaultDisplay();

        mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);// 320*480  // For setting size of screen to draw Bitmap

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mCanvas = new Canvas(mBitmap);
        onDraw(mCanvas);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try{
            canvas.drawColor(Color.TRANSPARENT);//Color.WHITE //To change background color of Application
            if(bit!=null)
                canvas.drawBitmap(bit, 0, 0, mBitmapPaint);
            else
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        }catch(Exception e){}
        if(bit==null)
            canvas.drawPath(mPath, mPaint);

        bit=null;

    }
    private float mX, mY;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x+1;
        mY = y+1;

    }

    private void touch_upline(float x,float y) {
        mPath.lineTo(mX, mY);
        mCanvas.drawLine(mX, mY, x, y, mPaint);
        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:
            eraseAll();
            touch_upline(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_upline(x,y);
            invalidate();
            break;
        }
        return true;
    }

    public void eraseAll()
    {
        mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
        mCanvas = new Canvas(mBitmap);
    }



}

そしてmain.xmlはこれです

<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:id="@+id/mainLayout" >



</RelativeLayout>
于 2012-08-21T10:34:05.140 に答える