Android view随触碰滑动效果

所属分类: 软件编程 / Android 阅读数: 42
收藏 0 赞 0 分享

主要思路是通过父布局的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.getX(), 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随触碰滑动效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多