这个Android应用程序就好像我每次触摸屏幕时都会双击它,谁能看到为什么会发生这种情况?我有它日志“触摸#” + [触摸的数量],它并“触摸1”和“触摸2”在同一时间,同一“触摸3”和“触摸4”Android触摸次数增加了一倍?
package com.example.mondrianmaker;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
int width, height, x, y, color, touch;
ArrayList<Rectangle> rectangles, childsRect;
Bitmap bg;
Canvas canvas;
Display display;
LinearLayout ll;
Point size;
Paint paint;
Random rn;
Rectangle parentRect;
TextView text, info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//***FULL SCREEN***
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//---FULL SCREEN---
setContentView(R.layout.activity_main);
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
paint = new Paint(); //paint to color rectangle OBLI
bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
ll = (LinearLayout) findViewById(R.id.mondrian);
rectangles = new ArrayList<Rectangle>();
text = (TextView) findViewById(R.id.coordinates);
info = (TextView) findViewById(R.id.info);
parentRect = new Rectangle(0, 0, 480, 800); //create super parent rectangle
rectangles.add(parentRect); //add super parent rectangle to list
touch = 0;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Get Coordinate
x = (int) event.getX();
y = (int) event.getY();
//set text view to clicked coordinates and number of rectangles on list
text.setText(x + ", " + y + " rectangles: "+rectangles.size());
//set rectangle color *randomly
setColor();
paint.setColor(color);
//get rectangle clicked
parentRect = getRectParent(x, y);
//make children of rectangle clicked
childsRect = makeChilds(parentRect, x, y);
addChildsToRectanglesDeleteParentRect(childsRect, parentRect);
//check on childs
// text.setText(childsRect.get(0) + " " + childsRect.get(1));
//get onTouch action
switch (event.getAction()) {
// case MotionEvent.ACTION_DOWN:
// text.setText(x + ", " + y);
// ll.setBackgroundDrawable(new BitmapDrawable(bg));
// case MotionEvent.ACTION_MOVE:
// ll.setBackgroundDrawable(new BitmapDrawable(bg));
// System.out.println("22222");
case MotionEvent.ACTION_UP:
touch++;
Log.i("info", "touch # " + touch);
// ll.setBackgroundDrawable(new BitmapDrawable(bg));
// System.out.println("33333");
}
return false;
}
public void addChildsToRectanglesDeleteParentRect(ArrayList<Rectangle> childsRect, Rectangle parentRect){
rectangles.remove(parentRect);
rectangles.add(childsRect.get(0));
rectangles.add(childsRect.get(1));
int i = 0;
Log.i("info", "length= "+rectangles.size());
for(Rectangle e : rectangles){
Log.i("info", i+"--> "+ e.toString());
i++;
}
}
public ArrayList<Rectangle> makeChilds(Rectangle parent, int x, int y){
ArrayList<Rectangle> defaults = new ArrayList<Rectangle>();
defaults.add(new Rectangle(0,0,240,800));
defaults.add(new Rectangle(240,0, 480, 800));
ArrayList<Rectangle> childs = new ArrayList<Rectangle>();
for(Rectangle rect : rectangles){
if(x>=rect.getX1() && x<=rect.getX2() && y>=rect.getY1() && y<=rect.getY2()){
childs.add(new Rectangle(rect.getX1(), rect.getY1(), rect.getX2()/2, rect.getY2()/2));
childs.add(new Rectangle(rect.getX2()/2, rect.getY2()/2, rect.getX2(), rect.getY2()));
}
}
if(childs.size()>0){
return childs;
}else{
info.setText(x + ", " + y + " " + parent + "no childs for this parent");
return defaults;
}
}
public Rectangle getRectParent(int x, int y){
for(Rectangle g : rectangles){
if (x > g.getX1() && x < g.getX2() && y > g.getY1() && y < g.getY2()){
info.setText(info.getText() + "\n " + g.getX1()+"_x=" + x +"_"+g.getX2() +"\n"+g.getY1()+"_y="+y+"_"+g.getY2());
return g;
}
}
return null;
}
public void setColor(){
rn = new Random();
switch ((int) Math.floor(rn.nextDouble() * 5)) {
case 0:
color = Color.BLACK;
break;
case 1:
color = Color.RED;
break;
case 2:
color = Color.YELLOW;
break;
case 3:
color = Color.WHITE;
break;
case 4:
color = Color.GREEN;
break;
}
}
}
谢谢,我忘了休息;在开关盒上。 – 2014-10-18 14:40:34