在Processing 2.0.3中将弃用的motionPressure替换为MotionEvent时找不到符号

时间:2014-02-10 18:36:10

标签: android processing motionevent

我正在阅读一本关于“快速Android开发”的书,其中包含Processing 2.0.3 windows 7 64bit,仅安装了api 10级(android 2.3.3)。

我所使用的示例,使用的“motionPressure”被2.0b7 http://wiki.processing.org/w/Android#Mouse.2C_Motion.2C_Keys.2C_and_Input删除 “motionX,motionY,motionPressure等变量都被删除了。这有点像kludge,后来的触摸API会更好地处理它。应该很容易用十几行代码把它们带回来在你的草图中,如果你依赖它们。相反,现在只使用mouseX / Y.“

我在这里找到了一个修复https://forums.pragprog.com/forums/209/topics/11348,但代码似乎不起作用。当它到达mouseDragged()时“MotionEvent me =(MotionEvent)mouseEvent.getNative();”它停在这条线上,说无法找到符号。

如何以简单的方式解决这个问题?我只是在学习编码。

编辑:我看到在整本书中越来越多地使用了motionPressure,因此我需要一种复制该功能的方法。

import android.view.MotionEvent;

float maxPressure;
float motionPressure;

void setup()
{
  noStroke();
  background(0);
}

void draw()
{
  fill(motionPressure/maxPressure * 255);

  ellipse(mouseX, mouseY, mouseX-pmouseX, mouseY-pmouseY);

  println(motionPressure);

  if(motionPressure > maxPressure)
    maxPressure = motionPressure;
}

void mouseDragged() {
  MotionEvent me = (MotionEvent) mouseEvent.getNative();
  motionPressure = me.getPressure();
  println (motionPressure);  // can do stuff with this float now -- mine ranges .4 - 3.6 -- not 0 -1
}

1 个答案:

答案 0 :(得分:0)

这是从他们的wiki中看起来有效的代码的精简版本。感谢您向我指出kevinsa5

import android.view.MotionEvent;

// fix prerequisite
String touchEvent = "";
float pressure = 0.0;

void setup()
{
    noStroke();
    background(0);
}

void draw()  { }

void mouseDragged()
{
    fill(pressure * 255);
    ellipse(mouseX, mouseY, mouseX-pmouseX, mouseY-pmouseY);
    println(pressure);
}

// fix
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    pressure = event.getPressure();
    int action = event.getActionMasked();
    pressure = event.getPressure();
    switch (action) {case MotionEvent.ACTION_UP: pressure = 0.0; break; }
    return super.dispatchTouchEvent(event);
}