我正在创建一个测试游戏,当触摸时,一个圆圈将从点阵列列表中移动到选定位置。但是,单击时似乎无法移动到点的下一个位置。您能帮我找出问题出在哪里以及可以使用什么解决方案吗?
public class EyeTestActivity extends AppCompatActivity {
private GestureDetectorCompat mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new TestView(this));
// get the gesture detector
mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector());
}
public boolean onTouchEvent(MotionEvent motionEvent) {
this.mDetector.onTouchEvent(motionEvent);
return super.onTouchEvent(motionEvent);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}
public class TestView extends View {
public ArrayList<Point> pointlist;
Paint paint;
public TestView(Context context) {
super(context);
init();
setFocusable(true);
setFocusableInTouchMode(true);
createPointList();
}
public void init() {
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
}
public void createPointList() {
pointlist = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
float a = 100 * i;
float b = 100 * i;
for (int j = 1; j <= 24; j++) {
float x = (float) (a * Math.sin(Math.toRadians(15 * j)));
float y = (float) (b * Math.cos(Math.toRadians(15 * j)));
for (int k = 0; k < 120; k++) {
pointlist.add(new Point(x, y));
//Add the x and y coordinates to the Point
}
}
}
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(Color.BLACK);
Point point2 = pointlist.get(z);
canvas.drawCircle(point2.getX() + canvas.getWidth() / 2, point2.getY() + canvas.getHeight()/ 2, 15, paint);
}
}
int z = 0;
public class SwipeGestureDetector implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (e1.getAction() == MotionEvent.ACTION_MOVE) {
z++;
if (z > 120) {
z = 0;
}
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1 == null || e2 == null)
return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1)
return false;
else {
try {
float diffX = e2.getX() - e1.getX();
float diffY = e2.getY() - e1.getY();
if(Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > 100 && Math.abs(velocityX) > 1000) {
if ((diffX > 0) || (diffX < 0)) {
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
}
我希望打印时画下一个圆圈。
答案 0 :(得分:0)
首先,您应该参考自己的if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
///check image Size
let imgData = NSData(data: UIImageJPEGRepresentation((pickedImage), 1)!)
let imageSize: Int = imgData.count
print("size of image in KB: %f ", Double(imageSize) / 1024.0)
print("size of image in MB: %f ", Double(imageSize) / 1024.0 / 1024)
}
布局。
import React, { Component } from 'react';
import { Stage, Layer, Rect } from 'react-konva';
import './Canvas.css';
class Canvas extends Component {
render() {
const { floorPlan, blocks, samples } = this.props.floorplane;
const sampleData = samples.length
? samples[this.props.canvasstate].placedModules
: [];
return (
<Stage width={floorPlan.width} height={floorPlan.height}>
<Layer className="kontent">
{blocks.map((block, index) => (
<Rect
x={block.x}
y={block.y}
width={block.width}
height={block.height}
stroke="red"
strokeWidth="1"
key={index}
/>
))}
</Layer>
</Stage>
);
}
}
然后,在您的TestView
事件中,您应该测试public class EyeTestActivity extends AppCompatActivity {
private GestureDetectorCompat mDetector;
private TestView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
tv = new TestView(this);
setContentView(tv);
// get the gesture detector
mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector());
}
而不是onScroll
e2
我认为您的e1
并未执行您想要的操作。
您正在同一时间创建120倍!总计120 * 5 * 24 = 14.400点!
应该是
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (e2.getAction() == MotionEvent.ACTION_MOVE) {
z++;
if (z >= 120) { // zero based arraylist, so, >= 120
z = 0;
}
tv.invalidate; // this to redraw the point
}
return true;
}