增长视图和MotionEvent.getX()/ MotionEvent.getRawX()

时间:2012-09-14 10:50:48

标签: android math touch motionevent

我有以下代码可以展开磁贴。但是我有多个这样的图块,我需要代码对每个人都是通用的。

启动时的用户界面:http://s14.postimage.org/9wn8b2my9/Screenshot_2012_09_14_12_03_14.png

我有两种选择:

第一个是使用原始的X,Y值,这意味着我的手指在屏幕上的确切位置。这样我就可以很容易地计算到几个点的准确宽度,高度和距离。一个问题是该值不适用于所有图块。例如,左上角的图块不会展开,左下角的图块会扩展到超出应有的水平。实际上似乎听的唯一一块是中间的那块。

else if(event.getAction() == MotionEvent.ACTION_MOVE) {         
        if((newPos.x > this.leftParent.getX()) && (newPos.x < this.rightParent.getX())){
            //reposition the touch indicator
            this.child.setX(newPos.x-42); //-42 because pic = 84dp
            this.child.setVisibility(VISIBLE);
        }
        if((newPos.y < this.bottomParent.getY()) && (newPos.y > this.topParent.getY())){
            this.child.setY(newPos.y-42);
            this.child.setVisibility(VISIBLE);
        }

        if((this.rightParent.getX() - newPos.x) < this.originalSize.x){
            // X too narrow
            this.leftParent.setX((float) (this.rightParent.getX()-this.originalSize.x));
            Log.e("loc", "X TOO NARROW");
        } else if((this.rightParent.getX() - newPos.x) > this.rightParent.getX()-this.originalSize.x*1.6){
            // X too wide
            Log.e("loc", "X TOO WIDE newPos: "+newPos.x+", "+newPos.y+" new width:"+(this.rightParent.getX() - newPos.x)+ " old width:"+(this.rightParent.getX()-this.originalSize.x*1.6));
        } else if(((this.rightParent.getX() - newPos.x) > this.originalSize.x) && ((this.rightParent.getX() - newPos.x) < this.rightParent.getX()-this.originalSize.x*1.6)){ 
            // X inbetween required values
            elasticPosition.x = (int) Math.pow(Math.E, -newPos.x);
            this.leftParent.setX(newPos.x);
            Log.e("loc", "X OK");
        }

        if((this.bottomParent.getY() - newPos.y) < this.originalSize.y){
            // Y too narrow
            this.topParent.setY(this.bottomParent.getY()-this.originalSize.y);
            Log.e("loc", "Y TOO NARROW");
        } else if((this.bottomParent.getY() - newPos.y) > this.bottomParent.getY()-this.originalSize.y*1.6){
            // Y too wide
            Log.e("loc", "Y TOO WIDE");
        } else if(((this.bottomParent.getY() - newPos.y) > this.originalSize.y) && ((this.bottomParent.getY() - newPos.y) < this.bottomParent.getY()-this.originalSize.y*1.6)){
            // Y inbetween required values
            //elasticPosition.y = (int) Math.pow(Math.E, -newPos.y);        //shaving this for a rainy day
            this.topParent.setY(newPos.y);

            Log.e("loc", "Y OK");
        }

瓷砖拒绝拉伸:http://s12.postimage.org/ltunmbt7x/Screenshot_2012_09_14_12_03_31.png(记录瓷砖已超出最大边界)

第二个是使用X,Y值与父对象的位置进行比较。这个产生更简单的公式,目前我的偏好。但是,由于瓷砖碰巧会膨胀,因此价值也会增加。在图块的左上角,值为(0,0),如果我越过该角,则值将下降到负值。然而,如果我再次收缩瓷砖,我会突然得到一个积极的价值,它会使一切都变得摇摇欲坠,错误和毛躁等等。

if(event.getX() > 0 && event.getX() < this.getX() + this.getAccurateSize().x){
            //inbetween original values
            this.leftParent.setX((float) (this.rightParent.getX()-this.originalSize.x));
            Log.e("touch x", "inbetween original values");
        } else if(event.getX() < (0 - originalSize.x*0.6)){
            //too wide
            this.leftParent.setX((float) (this.rightParent.getX()-this.originalSize.x*1.6));
            Log.e("touch x", "too wide");
        } else if(event.getX() < 0 && event.getX() > (0 - originalSize.x*0.6)){
            //inbetween stretched values
            this.leftParent.setX(newPos.x);
            Log.e("touch x", "inbetween stretched values");
        }

        if(event.getY() > 0 && event.getY() < this.getY() + this.getAccurateSize().y){
            //inbetween original values
            this.topParent.setY(this.bottomParent.getY()-this.originalSize.y);
            Log.e("touch y", "inbetween original values");
        } else if(event.getY() < (0 - originalSize.y*0.6)){
            //too wide
            this.topParent.setY((float) (this.bottomParent.getY()-(this.originalSize.y*1.6)));
            Log.e("touch y", "too wide");
        } else if(event.getY() < 0 && event.getY() > (0 - originalSize.y*0.6)){
            //inbetween stretched values
            this.topParent.setY(newPos.y);
            Log.e("touch y", "inbetween stretched values");
        }

如果有人能帮我修复这个烦人的bug,我会很高兴,这样我就可以继续开发这个应用程序了。提前谢谢!

0 个答案:

没有答案