Android - 从onTouchEvent()

时间:2017-04-24 09:59:14

标签: android ontouchevent

首先我确切地说,我看到了很多关于类似问题的问题,但是对于我的确切情况没有人,所以我不得不问自己的问题。

我尝试创建一个用户在触摸视图时收集笔划的应用程序。我创建了一个名为WritingView的类,它扩展了View。在这个类中,我重写了onTouchEvent()方法来绘制笔划并收集名为currentStroke的ArrayList中的坐标。它工作,直到我尝试访问包含MainActivity中的coords的数组的内容,当我单击一个按钮时,它始终为空。

这是WritingView类的代码,我把它全部放在一边但是我认为问题来自onTouchEvent,其余的是在这里绘制收集画布和屏幕上的笔画,它起作用:

public class WritingView extends View{
private Canvas drawCanvas;
private Bitmap bitmapCanvas;
private Path inkPath;
private Paint ink, inkCanvas;
private Context context;
private ArrayList<Float> currentStroke;

public WritingView(Context c) {
    super(c);
    init(c);
}

public WritingView(Context c, AttributeSet attrs) {
    super(c, attrs);
    init(c);
}

public WritingView(Context c, AttributeSet attrs, int defStyleAttr) {
    super(c, attrs, defStyleAttr);
    init(c);
}

public void init(Context c) {
    this.inkPath = new Path();
    this.ink = new Paint();
    this.context = c;
    this.currentStroke = new ArrayList<Float>();

    // Get the screen size
    WindowManager wm = (WindowManager) this.context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    // Set ink parameters
    ink.setColor(0xFF660000);
    ink.setAntiAlias(true);
    ink.setStrokeWidth(15);
    ink.setStyle(Paint.Style.STROKE);
    ink.setStrokeJoin(Paint.Join.ROUND);
    ink.setStrokeCap(Paint.Cap.ROUND);

    this.inkCanvas = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    this.bitmapCanvas = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    this.drawCanvas = new Canvas(bitmapCanvas);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmapCanvas, 0, 0, inkCanvas);
    canvas.drawPath(inkPath, ink);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
            inkPath.moveTo(touchX, touchY);
            Log.d(TAG, "onTouchEvent: ACTION_DOWN_EVENT - X=" + touchX + ", Y=" + touchY );
            break;
        case MotionEvent.ACTION_MOVE:
            inkPath.lineTo(touchX, touchY);
            this.currentStroke.add(touchX);
            this.currentStroke.add(touchY);
            Log.d(TAG, "onTouchEvent: ACTION_MOVE_EVENT - X=" + touchX + ", Y=" + touchY );
            break;
        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(inkPath, ink);
            Log.d(TAG, "onTouchEvent: ACTION_UP_EVENT - X=" + touchX + ", Y=" + touchY );
            inkPath.reset();
            break;
        default:
            return false;
    }

    invalidate();
    return true;
}

public ArrayList<Float> getCurrentStroke() {
    Log.d(TAG, "getCurrentStroke: size : " + this.currentStroke.size());
    return this.currentStroke;
}
}

MainActivity的代码:

public class MainActivity extends AppCompatActivity {
static final int RECO_REQUEST = 0;
private WritingView writingView;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    writingView = new WritingView(this);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.recognizeButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startReco();
        }
    });
}

public void startReco() {
    ArrayList<Float> coords = writingView.getCurrentStroke();

    Log.d(TAG, "onClick: size : " + coords.size());
    for(int i = 0; i < coords.size(); i++) {
        Log.d(TAG, "onClick: " + i + " : " + coords.get(i));
    }
}
}

正如我所说,当我尝试从MainActivity访问它时,数组似乎是空的,但我知道(我测试过)在onTouchEvent方法中将坐标放入其中。

感谢您的帮助。

此外,这里是xml文件,用于显示活动的显示,以及如何在其上添加WritingView:

<TextView
    android:id="@+id/textResult"
    android:layout_width="578dp"
    android:layout_height="146dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="Results :"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/recognizeButton"
    android:text="Start recognition"
    android:layout_width="300dp"
    android:layout_height="80dp"
    app:layout_constraintTop_toBottomOf="@+id/textResult"
    app:layout_constraintBottom_toTopOf="@+id/writingView" />

<com.myscript.testapplication.WritingView
    android:id="@+id/writingView"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:layout_weight="1"
    android:background="#E3EAE7"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/recognizeButton"/>

1 个答案:

答案 0 :(得分:1)

好像你有2个WritingViews。首先,您在布局文件中有一个,并且您还在活动中创建了一个新文件。

试试这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    writingView = (WritingView)findViewById(R.id.writingView);
    ....
}

或者,您可以从布局文件中删除WritingView,并使用addView()将其添加到主活动中。