Android-从OnTouchEvent()访问成员变量



首先,我确切地看到了很多问题,但没有人出于我的确切情况,所以我必须问自己的问题。

我正在尝试创建一个在用户触摸视图时收集笔触的应用程序。我创建了一个名为"写入视图"的类,该类别扩展了视图。在此课程中,我覆盖了onTouchevent((方法来绘制笔触并在名为CurrentStroke的阵列列表中收集坐标。它有效,直到我尝试访问Mainactivity中包含坐标的数组的内容,当我单击一个按钮时,它始终为空。

这是写作视图类的代码,我将全部付诸实践,但我认为问题来自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;
}
}

和主动脉的代码:

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));
    }
}
}

正如我所说,当我尝试从主攻击性访问它时,数组似乎是空的,但是我知道(我对其进行了测试(将坐标放入ontouchevent方法中。

感谢您的帮助。

此外,这是XML文件,以显示活动的显示以及如何在其上添加写作视图:

<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"/>

似乎有2个写作视图。首先,您在布局文件中有一个,也可以在活动中创建一个新的。

尝试以下操作:

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

另外,您可以从布局文件中删除写作视图,然后使用AddView((将其添加到主活动中。

最新更新