Android侧滑效果1-学习记录
来源:互联网 发布:炒股软件免费下载 编辑:程序博客网 时间:2024/05/16 15:40
侧滑原理分析:
淘汰的思路:
第一:试图:ViewGroup
第二:事件:onTouchEvent
第三:思路:当手指触动屏幕时,根据touch的事件,去改变ViewGroup的左右试图的宽度。
第四:实现:(略)
这种思路我们要处理touch的move事件和up事件,并且对于冲突的处理并不理想。所以我使用HorizontalScrollView(水平滚动条),太晚了,我就不写了,明天再写。
今晚继续写:
首先说个思路哈,不知道对不对,觉得是这样的,当自定义视图时,要在构造方法中获得屏幕的宽度,其次重写onMeasure方法,确定子布局和自身的宽高,再次重写onLayout方法,确定子布局的位置,最后可以重写onTouchEvent方法,根据手势的改变去改变子布局的相对位置。下面我就带着这个思路去实现一款最简单的侧滑效果:
视图代码如下:
package com.imooc.cehua;import android.annotation.SuppressLint;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.MotionEvent;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;/** * @author 2015年7月23日,自定义试图。 * * 一般分为三部分: 第一步:重写onMeasure方法,去确定自己和子布局的宽和高 第二步:重写onLayout方法,确定子布局放置的位置 * 第三步:重写onTouchEvent方法,确定手势的改变,对布局的影响(有时也不用重写他,根据需要选择重写) * */public class MyView extends HorizontalScrollView {private LinearLayout mLayout;private ViewGroup mMenu;private ViewGroup mContent;private boolean once;// 确保onMeasure不被重复调用private int screenWidth;// 屏幕的宽度private int mMenuPaddingRight;// 左侧菜单的左边距离手机屏幕左边的距离private int mMenuWidth;// 因为菜单的宽度根据手势会有变化,所以把他的距离抽取出来public MyView(Context context) {super(context);}public MyView(Context context, AttributeSet attrs) {super(context, attrs);WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(outMetrics);screenWidth = outMetrics.widthPixels;// 获取屏幕的像素值// 把50像素转换为了50dipmMenuPaddingRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());}public MyView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}/** * 测量自己和子布局的宽和高的 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {if (!once) {mLayout = (LinearLayout) getChildAt(0);mMenu = (ViewGroup) mLayout.getChildAt(0);mContent = (ViewGroup) mLayout.getChildAt(1);mContent.getLayoutParams().width = screenWidth;mMenuWidth = mMenu.getLayoutParams().width = screenWidth- mMenuPaddingRight;once = true;}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}/** * 这个是设置子布局的布局位置,因此首先把menu隐藏 */@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);if (changed) {// 他的意思是,把菜单的左边距离屏幕的左边mMenuWidth(只不过这个是不显示的),而此时菜单的宽度刚好是这个值,因此就被隐藏了scrollTo(mMenuWidth, 0);}}/** * 由于HorizontalScrollView,对移动和冲突处理的很好了,只需要判断手指抬起时,菜单左边距离屏幕左边的值和菜单宽度比较即可,如果, * 大于就隐藏,小于就显示 */@SuppressLint("ClickableViewAccessibility")@Overridepublic boolean onTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_UP:int x = getScrollX();// 这个就是判断整个视图的左边距离屏幕的左边的距离,而恰巧菜单在布局的左侧,因此也就是菜单左边距离屏幕左边的距离if (x >= mMenuWidth / 2) {smoothScrollTo(mMenuWidth, 0);} else {smoothScrollTo(0, 0);}return true;}// 不能让他一直返回true,不然没有滑动效果,具体因为啥,我不知道呀,可以查看onTouchEvent返回值的区别return super.onTouchEvent(ev);// return true;}}
两个布局文件如下:
主布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.imooc.cehua.MyView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <include layout="@layout/left_menu" /> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/content" android:orientation="vertical" > </LinearLayout> </LinearLayout> </com.imooc.cehua.MyView></RelativeLayout>
菜单布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFD883" android:gravity="center" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/id_img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/id_img2" android:text="第二个item" android:textColor="#CEDDED" android:textSize="30sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/id_img3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/id_img3" android:text="第三个item" android:textColor="#CEDDED" android:textSize="30sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/id_img4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/id_img4" android:text="第四个item" android:textColor="#CEDDED" android:textSize="30sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/id_img5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/id_img5" android:text="第五个item" android:textColor="#CEDDED" android:textSize="30sp" /> </RelativeLayout> </LinearLayout></RelativeLayout>工程目录结构如图所示:
0 0
- Android侧滑效果1-学习记录
- Android学习记录1
- Android学习记录1
- android学习记录(1)
- Android 学习记录<1>
- android:listView学习记录1
- android-JNI学习记录(1)
- Android开发学习记录1
- Android基础知识学习记录[1]
- Android-学习记录(1)
- android学习记录1:android自定义Dialog
- android 动画效果学习
- cocos2d学习记录(九)-尾巴效果
- js小案例效果学习记录
- js小案例效果学习记录
- js小案例效果学习记录--倒计时
- angularjs学习记录--tab选项卡效果
- 天天记录 - Android TextView跑马灯效果
- block的基本用法
- RecyclerView 添加头部和尾部布局
- 从零开始写一个发送h264的rtsp服务器(下)
- 局域网内ip冲突引起的怪异现象
- 新标准c++程序设计教程chapter2_2015.7.22
- Android侧滑效果1-学习记录
- Spark WARN cluster.ClusterScheduler: Initial job has not accepted any resources;check your cluster
- Android之同一个TextView设置不同样式的文字
- 青年歌手大奖赛_评委会打分 (一道比较简单的题目)
- 常用哈希函数的比较及其C语言实现
- 2015072203 - 要求别人的同时更得严格要求自己
- 如何给树莓派的无线网卡设置静态IP地址
- utf-8/gbk 的不可映射字符 打包后的效果:zip包 名称包含svn号
- 垂直翻页的Viewpager