1

我需要一个类似下面的绘图应用程序 -

用户在 A 点触摸 (ACTION_DOWN) 屏幕,然后将手指悬停 (ACTION_MOVE) 屏幕上。假设当前的触摸点是 B 点。应用程序应始终在 A 和 B 之间绘制一条直线。也就是说,线的 A 点将是静止的,而线的 B 点将随着手指(触摸点)保持移动。一旦用户抬起手指,线路就会变得稳定。用户可以像这样绘制多条线。此外,用户可以选择(在选择模式下)任何行并更改其位置。

请提出前进的方向。我应该画什么样的画,线/点/路径/精灵/其他?如果您可以提供任何链接,那将非常有帮助。它应该适用于 v3.2 及更高版本。

问候, 普拉松

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.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 回答
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 回答