View滑动---学习笔记

学习来源

新知识

视图坐标系

View的相关坐标
  • View获取自身宽高
    • getHeight():获取View自身高度
    • getWidth():获取View自身宽度
  • View自身坐标
    • 通过如下方法可以获得View到其父控件(ViewGroup)的距离:
      • getTop():获取View自身顶边到其父布局顶边的距离
      • getLeft():获取View自身左边到其父布局左边的距离
      • getRight():获取View自身右边到其父布局左边的距离
      • getBottom():获取View自身底边到其父布局顶边的距离
  • 屏幕左上角为Android坐标系的原点以为x轴的正方向以为y轴的正方向
    • x和y是View左上角的坐标
    • translationX和translationY是View左上角相对于父容器的偏移量
    • translationX和translationY的默认值是0
    • x = left + translationX
    • y = top + translationY
MotionEvent===>无论是View还是ViewGroup,最终的点击事件都会由onTouchEvent(MotionEvent event)方法来处理
  • MotionEvent
    • MotionEvent.ACTION_MOVE手指刚接触屏幕
    • MotionEvent.ACTION_DOWN手指在屏幕上移动
    • MotionEvent.ACTION_UP手指从屏幕上松开的一瞬间
  • 正常情况下 一次手指触摸屏幕的行为会触发一系列点击事件
    • 点击屏幕离开松开===》事件序列为DOWN->UP
    • 点击屏幕滑动一会再松开===》事件序列为DOWN->MOVE->…->MOVE->UP
  • 上图那个深蓝色的点,假设就是我们触摸的点,MotionEvent也提供了各种获取焦点坐标的方法:
    • getX():相当于当前View左上角的x坐标,即视图坐标
    • getY():相当于当前View左上角的y坐标,即视图坐标
    • getRawX():相当于手机屏幕左上角的x坐标,即绝对坐标
    • getRawY():相当于手机屏幕左上角的y坐标,即绝对坐标
activity_main中多添加了Button控件
 <Button
     android:id="@+id/btn"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

 <com.example.axtonsun.axtonroid_view.CustomView
     android:layout_width="80dp"
     android:layout_height="80dp"
     android:id="@+id/customview"
     android:layout_margin="50dp"
     android:background="@android:color/holo_red_light"/>
CustomViewonTouchEvent的方法中case MotionEvent.ACTION_MOVE:
    //计算移动的距离
                int offsetX = x - lastX;
                int offsetY = y - lastY;

                //调用layout方法来重新放置它的位置
                //Button不滑动    自定义的滑动
  /*            layout(getLeft()+offsetX, getTop()+offsetY,getRight()+offsetX , getBottom()+offsetY);*/

                //Button不滑动    自定义的滑动
                //对left和right进行偏移
  /*            offsetLeftAndRight(offsetX);
                //对top和bottom进行偏移
                offsetTopAndBottom(offsetY);*/

                //使用MarginLayoutParams
                //Button不滑动 自定义的滑动
                LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin = getLeft() + offsetX;
                layoutParams.topMargin = getTop() + offsetY;
                setLayoutParams(layoutParams);

  /*            //使用scrollBy
                //Button和自定义一起滑动 相对位置不变化
                ((View)getParent()).scrollBy(-offsetX,-offsetY);*/
res下新建文件夹然后在创建xml文件
  1. 新建文件夹的Resource type选择transtion
  2. xml内的代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--android:fillAfter="true">保证View动画在结束动画时是否回到原来的位置-->

    <translate
        android:fromXDelta="0"
        android:toXDelta="300"
        android:duration="1000"/>
</set>
MainActivity
private CustomView customView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        customView = (CustomView) findViewById(R.id.customview);
        //View动画
        //如果不设置android:fillAfter="true"  会发生滑动后的复原
        //customView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.translate));
        //属性动画
        //解决了上述View动画的问题    可以解决==》新位置的单击事件的问题
        //ObjectAnimator.ofFloat(customView,"translationX",0,300).setDuration(1000).start();
        //customView.smoothScrollTo(-400,0);
    }

截图===>Button与自定义控件相对变化的对比

Github地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值