当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现View滑动的6种方式

Android实现View滑动的6种方式

2019年07月24日  | 移动技术网IT编程  | 我要评论

抵抗 惩罚,黄真伊下载,ynzk

本文实例为大家分享了android实现view滑动的具体方法,供大家参考,具体内容如下

1.view的滑动简介

view的滑动是android实现自定义控件的基础,同时在开发中我们也难免会遇到view的滑动的处理。其实不管是那种滑动的方式基本思想都是类似的:当触摸事件传到view时,系统记下触摸点的坐标,手指移动时系统记下移动后的触摸的坐标并算出偏移量,并通过偏移量来修改view的坐标。
实现view滑动有很多种方法,这篇文章主要讲解六种滑动的方法,分别是:layout()、offsetleftandright()与offsettopandbottom()、layoutparams、动画、scollto与scollby和scroller;在下一篇文章我们会详细介绍属性动画。

2.实现view滑动的六种方法

1.layout()

view进行绘制的时候会调用onlayout()方法来设置显示的位置,因此我们同样也可以通过修改view的left、top、right、bottom这四种属性来控制view的坐标。首先我们要自定义一个view,在ontouchevent()方法中获取触摸点的坐标:

public boolean ontouchevent(motionevent event) {
  //获取到手指处的横坐标和纵坐标
  int x = 0, newx = 0;
  int y = 0, newy = 0;
  switch (event.getaction()) {
    case motionevent.action_down:
      log.d("hahahhaha", "屏幕位置被按下");
      x = (int) event.getx();
      y = (int) event.gety();
   
      break;

接下来我们在action_move事件中计算偏移量,再调用layout()方法重新放置这个自定义view的位置就好了:

public boolean ontouchevent(motionevent event) {
  //获取到手指处的横坐标和纵坐标
  int x = 0, newx = 0;
  int y = 0, newy = 0;
  switch (event.getaction()) {
    case motionevent.action_down:
      log.d("hahahhaha", "屏幕位置被按下");
      x = (int) event.getx();
      y = (int) event.gety();

      break;
    case motionevent.action_move:
      log.d("hahahhaha", "屏幕位置在移動");
      newx = (int) event.getx();
      newy = (int) event.gety();
    
      int offsetx = newx - x;
      int offsety = newy - y;
      log.d("hahaha", "offsetx=" + offsetx + ",offsety=" + offsety);
      //1.调用layout方法来重新放置它的位置
      layout(getleft()+offsetx, gettop()+offsety,
          getright()+offsetx , getbottom()+offsety);

当我们每次移动时都会调用layout()方法来对自己重新布局,从而达到移动view的效果。

2.offsetleftandright()与offsettopandbottom()

这两种方法和layout()方法效果方法差不多,使用也差不多,我们将action_move中的代码替换成如下代码:

      case motionevent.action_move:
        //计算移动的距离
        int offsetx = x - lastx;
        int offsety = y - lasty;
        //对left和right进行偏移
        offsetleftandright(offsetx);
        //对top和bottom进行偏移
        offsettopandbottom(offsety);
        break;

3.layoutparams(改变布局参数)

layoutparams主要保存了一个view的布局参数,因此我们可以通过layoutparams来改变view的布局的参数从而达到了改变view的位置的效果。同样的我们将action_move中的代码替换成如下代码:

linearlayout.layoutparams layoutparams= (linearlayout.layoutparams) getlayoutparams();
 layoutparams.leftmargin = getleft() + offsetx;
 layoutparams.topmargin = gettop() + offsety;
 setlayoutparams(layoutparams);

因为父控件是linearlayout,所以我们用了linearlayout.layoutparams,如果父控件是relativelayout则要使用relativelayout.layoutparams。除了使用布局的layoutparams外,我们还可以用viewgroup.marginlayoutparams来实现:

viewgroup.marginlayoutparams layoutparams = (viewgroup.marginlayoutparams) getlayoutparams();
layoutparams.leftmargin = getleft() + offsetx;
layoutparams.topmargin = gettop() + offsety;
        setlayoutparams(layoutparams);

4.scollto与scollby

scollto(x,y)表示移动到一个具体的坐标点,而scollby(dx,dy)则表示移动的增量为dx、dy。其中scollby最终也是要调用scollto的。scollto、scollby移动的是view的内容,如果在viewgroup中使用则是移动他所有的子view。我们将action_move中的代码替换成如下代码:

((view)getparent()).scrollby(-offsetx,-offsety);

这里要实现customview随着我们手指移动的效果的话,我们就需要将偏移量设置为负值。

5.scroller

我们用scollto/scollby方法来进行滑动时,这个过程是瞬间完成的,所以用户体验不大好。这里我们可以使用scroller来实现有过度效果的滑动,这个过程不是瞬间完成的,而是在一定的时间间隔完成的。scroller本身是不能实现view的滑动的,它需要配合view的computescroll()方法才能弹性滑动的效果。
在这里我们实现customview平滑的向右移动。

首先我们要初始化scroller:

 public customview(context context, attributeset attrs) {
    super(context, attrs);
    mscroller = new scroller(context);
  }

接下来重写computescroll()方法,系统会在绘制view的时候在draw()方法中调用该方法,这个方法中我们调用父类的scrollto()方法并通过scroller来不断获取当前的滚动值,每滑动一小段距离我们就调用invalidate()方法不断的进行重绘,重绘就会调用computescroll()方法,这样我们就通过不断的移动一个小的距离并连贯起来就实现了平滑移动的效果:

  @override
  public void computescroll() {
    super.computescroll();
    if(mscroller.computescrolloffset()){
      ((view) getparent()).scrollto(mscroller.getcurrx(),mscroller.getcurry());
       //通过不断的重绘不断的调用computescroll方法
       invalidate();
    } 
  }

调用scroller.startscroll()方法。我们在customview中写一个smoothscrollto()方法,调用scroller.startscroll()方法,在2000毫秒内沿x轴平移delta像素: 

 public void smoothscrollto(int destx,int desty){
    int scrollx=getscrollx();
    int delta=destx-scrollx;
    //1000秒内滑向destx
    mscroller.startscroll(scrollx,0,delta,0,2000);
    invalidate();
  }

最后我们在viewslideactivity.java中调用customview的smoothscrollto()方法:
          //使用scroll来进行平滑移动
          mcustomview.smoothscrollto(-400,0);
这里我们是设定customview沿着x轴向右平移400像素。

6.动画

可以采用view动画来移动,在res目录新建anim文件夹并创建translate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromxdelta="0" android:toxdelta="300" android:duration="1000"/>
</set>

在java代码中引用:

 mcustomview.setanimation(animationutils.loadanimation(this, r.anim.translate));

当然使用属性动画移动那就更简单了,我们让customview在1000毫秒内沿着x轴像右平移300像素:

objectanimator.offloat(mcustomview,"translationx",0,300).setduration(1000).start();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网