Android view随触碰滑动效果

Android view随触碰滑动效果,第1张

概述主要思路是通过父布局的onTouch(),方法,获取滑动到的位置和点击下的位置,再去设置子view的位置。我的代码中考虑了在边缘情况。需要注意的是,使用RelativeLayout,以imageView为例。从测试结果来看,bottomMargin和

主要思路是通过父布局的ontouch(),方法,获取滑动到的位置和点击下的位置,再去设置子vIEw的位置。我的代码中考虑了在边缘情况。需要注意的是,使用relativeLayout,以imageVIEw为例。从测试结果来看,bottommargin 和rightmargin 性能非常差,最好还是用leftmargin与topmargin定位。

下面是运行效果

布局文件里面就是一个relativelayout中有一个ImageVIEw。如下

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/relativeLayout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.xingyi.movevIEwwithtouch.MainActivity"><ImageVIEw androID:ID="@+ID/imageVIEw" androID:layout_wIDth="40dp" androID:layout_height="40dp" androID:background="@androID:color/black"/></relativeLayout>

Java代码如下,这里考虑了边缘位置滑动的效果。如果考虑,在最左边缘imageVIEw会有一半在屏幕之外,在最右边缘会缩小,直到看不见。

package com.xingyi.movevIEwwithtouch;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;import androID.Widget.relativeLayout;public class MainActivity extends AppCompatActivity { ImageVIEw imageVIEw; relativeLayout relativeLayout; int heightRL,wIDthRL; int halfheight,halfWIDth; boolean first=true; private int wIDthimg; private int heightimg; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  initVIEw(); } //初始化视图 private voID initVIEw() {  imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imageVIEw);  relativeLayout = (relativeLayout) findVIEwByID(R.ID.relativeLayout);  //获取滑动瞬间位置和点击瞬间位置,并移动imagevIEw  relativeLayout.setontouchListener(new VIEw.OntouchListener() {   @OverrIDe   public boolean ontouch(VIEw vIEw,MotionEvent motionEvent) {    switch (motionEvent.getAction()) {     case MotionEvent.ACTION_MOVE:      moveVIEw(imageVIEw,motionEvent.getX(),motionEvent.getY());      break;     case MotionEvent.ACTION_DOWN:      getWIDthAndHeight();      moveVIEw(imageVIEw,motionEvent.getY());      break;     default:      break;    }    return true;   }  }); } //因为不能在初始化视图时获得长宽,而每次计算一次长宽又影响性能 private voID getWIDthAndHeight(){  if(first){   wIDthRL=relativeLayout.getWIDth();   heightRL=relativeLayout.getHeight();   wIDthimg=imageVIEw.getWIDth();   heightimg=imageVIEw.getHeight();   halfWIDth = imageVIEw.getWIDth() / 2;//imageVIEw宽度的一半   halfheight = imageVIEw.getHeight() / 2;//imageVIEw高度的一半   first=false;  } } //滑动瞬间,将x和y分别作imageVIEw的中心点到relativeLayout最左和顶端距离 private voID moveVIEw(VIEw vIEw,float x,float y) {  relativeLayout.LayoutParams params = (relativeLayout.LayoutParams) vIEw.getLayoutParams();  //设置水平位置  if (x < halfWIDth) {//左边缘   params.leftmargin = 0;//设置imagevIEw到左端距离为0  } else if (x > wIDthRL- halfWIDth) {   params.leftmargin = wIDthRL-wIDthimg;//设置imagevIEw左端到左端端距离(params.rightmargin的性能非常糟糕)  } else {   params.leftmargin = (int) (x - halfWIDth);//imagevIEw左端到relativelayout左端距离  }  //设置竖直位置  if (y < halfheight) {   params.topmargin = 0;  } else if (y > heightRL - halfheight) {   params.topmargin = heightRL-wIDthimg;//params.bottommargin的性能非常糟糕  } else {   params.topmargin = (int) (y - halfheight);  }  vIEw.setLayoutParams(params); }}

总结

以上所述是小编给大家介绍的AndroID vIEw随触碰滑动效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android view随触碰滑动效果全部内容,希望文章能够帮你解决Android view随触碰滑动效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144591.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存