Android中View的getX,getY...

这里写图片描述
上面的那张图很好的说明了每个方法获得的坐标值或者距离都是从哪里到哪里的。说明一下,图中有三个黑色的框框,最外层的是手机屏幕,

中间层的是ViewGroup,最内层的是ViewGroup中放置的view。

 其实上图中标注的方法可以分为两类,一类是View提供的方法,一类是MotionEvent提供的方法。分别说明如下:

View提供的获取的坐标以及距离的方法:

getTop() 获取到的是view自身的顶边到其父布局顶边的距离

getLeft() 获取到的是view自身的左边到其父布局左边的距离

getRight() 获取到的是view自身的右边到其父布局左边的距离

getBottom() 获取到的是view自身底边到其父布局顶边的距离

MotionEvent提供的方法:

getX() 获取点击事件距离控件左边的距离,即视图坐标

getY() 获取点击事件距离控件顶边的距离,即视图坐标

getRawX() 获取到的是点击事件距离整个屏幕左边的距离,即绝对坐标

getRawY() 获取到的是点击事件距离整个屏幕顶边的距离,即绝对坐标

getScrollY() 获取 视图坐标原点 到 视图 滚出屏幕 的距离

getScrollX() 类似 同上

源码理解:

 public float getX() {
        return mLeft + getTranslationX();
    }

 public float getY() {
        return mTop + getTranslationY();
    }

view.getTranslationX计算的是该view的偏移量。初始值为0,向左偏移值为负,向右偏移值为正;view.getTranslationY计算的是该view的偏移量。初始值为0,向上偏移值为负,向下偏移值为正。

原文地址: http://blog.csdn.net/zxwd2015/article/details/53055703
原文地址:http://blog.csdn.net/chdjj/article/details/42293753

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android之泡泡效果bubble package com.ray.bubble; import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnTouchListener; import android.widget.FrameLayout; import android.widget.ImageView; public class BubbleExplosion extends Activity { private FrameLayout fl; private ExplosionView exv1; private AnimationDrawable exa1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set full screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); fl = new FrameLayout(this); fl.setBackgroundResource(R.drawable.bg); exv1 = new ExplosionView(this); exv1.setVisibility(View.INVISIBLE); exv1.setBackgroundResource(R.anim.explosion); exa1 = (AnimationDrawable) exv1.getBackground(); fl.addView(exv1); fl.setOnTouchListener(new LayoutListener()); setContentView(fl); } class ExplosionView extends ImageView { public ExplosionView(Context context) { super(context); } // handle the location of the explosion public void setLocation(int top, int left) { this.setFrame(left, top, left + 40, top + 40); } } class LayoutListener implements OnTouchListener { public boolean onTouch(View v, MotionEvent event) { // first u have to stop the animation,or if the animation // is starting ,u can start it again! exv1.setVisibility(View.INVISIBLE); exa1.stop(); float x = event.getX(); float y = event.getY(); exv1.setLocation((int) y - 20, (int) x - 20); exv1.setVisibility(View.VISIBLE); exa1.start(); return false; } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值