Android自定义滑动显示隐藏布局
方式一:
上下左右滑动显示隐藏布局
总结代码地址: http://git.oschina.net/anan9303/customView
参考例子: http://www.jianshu.com/p/fce48921d086
代码:https://github.com/xujiaji/ScrollMenuDemo
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.Scroller;
/**
* 可滑动的相对布局
*
* @author LangK
*
*/
public class ScrollRelativeLayout extends RelativeLayout {
/**
* 滑动监听
*/
private OnScrollListener onScrollListener;
public ScrollRelativeLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ScrollRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ScrollRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
* scroll in from right
*/
public void beginScrollFromRight() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.rightMargin = 0;
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll out hide right
*/
public void beginScrollHideRight() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.rightMargin = -getWidth();
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll in from left
*/
public void beginScrollFromLeft() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.leftMargin = 0;
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll out hide left
*/
public void beginScrollHideLeft() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.leftMargin = -getWidth();
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll in from bottom
*/
public void beginScrollFromBottom() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.bottomMargin = 0;
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll out hide bottom
*/
public void beginScrollHideBottom() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.bottomMargin = -getHeight();
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll in from bottom
*/
public void beginScrollFromTop() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.topMargin = 0;
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
/**
* scroll out hide top
*/
public void beginScrollHideTop() {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.topMargin = -getHeight();
setLayoutParams(params);
}
});
translateAnimation.setDuration(300);
startAnimation(translateAnimation);
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
super.computeScroll();
if (onScrollListener != null) {
onScrollListener.computeScroll();
}
}
public OnScrollListener getOnScrollListener() {
return onScrollListener;
}
/**
* 滑动监听
*/
public void setOnScrollListener(OnScrollListener listener) {
this.onScrollListener = listener;
}
/**
* 滑动监听
*
* @author LangK
*
*/
public interface OnScrollListener {
public void computeScroll();
}
}
Android自定义滑动显示隐藏布局的更多相关文章
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- Android 自定义View及其在布局文件中的使用示例(二)
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...
- jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏
1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...
- Android上实现各种风格的隐藏菜单,比如左右滑动菜单、上下滑动显示隐藏菜单
Android上的菜单展示风格目前是各式各样的,但用的最多且体验最好的莫过于左右滑动来显示隐藏的菜单本示例实现了各种方式的菜单展示效果,只有你想不到的源码:https://github.com/Sim ...
- 自动显示隐藏布局的listView
借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- Android 自定义 ListView 显示网络上 JSON 格式歌曲列表
本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶 ...
- Android自定义之流式布局
流式布局,好处就是父类布局可以自动的判断子孩子是不是需要换行,什么时候需要换行,可以做到网页版的标签的效果.今天就是简单的做了自定义的流式布局. 具体效果: 原理: 其实很简单,Measure La ...
- Android 自定义View及其在布局文件中的使用示例
前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...
随机推荐
- hdu 4995(离散化下标+模拟)
Revenge of kNN Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- AC日记——线段树练习三 codevs 1082 (分块尝试)
线段树练习 3 思路: 分块: 来,上代码: #include <cmath> #include <cstdio> #include <cstring> #incl ...
- Codeforces Gym101063 F.Bandejao (2016 USP-ICMC)
F.Bandejao It is lunch time on Mars! Everyone has got that big smile on their faces, all eager to se ...
- 服务器出现大量close_wait,我们来说说到底是怎么回事?(以tomcat为例)
一.问题描述 最近一直忙得很,好久没写博客.前两天,微信收到个好友申请,说是想问问close_wait的事情. 找他问了些详细信息,大概了解到,他们后端服务是tomcat 7, jdk 7,cento ...
- Oracle并发控制、事务管理学习笔记
(a)基本概念 锁的2种最基本.最简单的类型:排他锁(eXclusive lock,即X锁).共享锁(Share lock,即S锁). 不同级别的锁定协议及其作用: 申请的锁 及其作用 锁定协议 修改 ...
- vs2013 x64 编译汇编代码
x64不再支持__asm, 只能单独放在一个.asm中. xxx.asm--------属性 应用 会出现 自定义生成工具. 下面黑体是我们修改的内容.
- mysql null与not null
http://blog.csdn.net/fwkjdaghappy1/article/details/7703974/ NULL表示 值 可为NULL,并非空的意思, NOT NULL表示 值不能为N ...
- django博客开发
找一文件夹作为项目文件夹1 django-admin.py startproject mysite建立工程2 cd mysite python manage.py startapp blog 建立第一 ...
- Flutter开发记录part3
(1) 获取当前屏幕宽度 width: MediaQuery.of(context).size.width, (1) pull_to_refresh,smartrefresh 自定义文字: new S ...
- git工程迁移(修改提交服务器地址)方法
git remote set-url [--push] <name> <newurl> [<oldurl>]git remote set-url --add [-- ...