在Android中动画移动一个View的位置,采用Scroller类实现Android动画之 View移动
在Android中动画移动一个View的位置,采用Scroller类实现
这个具体的情况是,需要做一个SlidingMenu的app,之前找了一个开源的,但不知道为什么,用起来app的运行效率很低,会有卡顿的现象。无奈只要自己写了。
SlidingMenu核心的就是可以滑动拉开左侧和右侧的菜单。刚开始考虑用TranslationAnimation来做。不过TranslationAnimation并不是真的移动一个View的坐标,在网上找了找,需要在Animation结束的时候,重新去layout下View的坐标,经过测试,这个方式可以达到预期效果。代码如下:
final int xOffset = leftFrameLayout.getWidth();
TranslateAnimation translateAnimation = new TranslateAnimation(0, xOffset, 0, 0); translateAnimation.setDuration(200);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
} @Override
public void onAnimationEnd(Animation animation) {
int left = xOffset;
int top = centerFrameLayout.getTop();
int width = centerFrameLayout.getWidth();
int height = centerFrameLayout.getHeight();
centerFrameLayout.clearAnimation();
centerFrameLayout.layout(left, top, left + width, top + height); leftFrameLayout.bringToFront();
} @Override
public void onAnimationRepeat(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
centerFrameLayout.startAnimation(translateAnimation);
貌似没什么问题了,不过我的界面中,当显示SldingMenu的侧边栏的时候,里面有个输入框,点击输入框弹出键盘的时候,会导致界面重新layout,这时候中间被移动的view,就又给自动移动回来了,看来用
centerFrameLayout.layout
无法解决键盘弹出时候的重绘。
这时考虑到使用Scroller类来进行动画移动。也是参考了网上的例子,将我中间部分的FrameLayout搞成一个自定义类,代码如下:
public class ScrollableFrameLayout extends FrameLayout {
private Scroller scroller;
public ScrollableFrameLayout(Context context) {
super(context);
init();
}
public ScrollableFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ScrollableFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
scroller = new Scroller(getContext());
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
postInvalidate();
}
@Override
public void computeScroll() {
if (!scroller.isFinished()) {
if (scroller.computeScrollOffset()) {
int oldX = getScrollX();
int oldY = getScrollY();
int x = scroller.getCurrX();
int y = scroller.getCurrY();
if (oldX != x || oldY != y) {
scrollTo(x, y);
}
// Keep on drawing until the animation has finished.
invalidate();
} else {
clearChildrenCache();
}
} else {
clearChildrenCache();
}
}
public void smoothScrollTo(int dx, int duration) {
int oldScrollX = getScrollX();
scroller.startScroll(oldScrollX, getScrollY(), dx, getScrollY(), duration);
invalidate();
}
private void enableChildrenCache() {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View layout = (View) getChildAt(i);
layout.setDrawingCacheEnabled(true);
}
}
private void clearChildrenCache() {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View layout = (View) getChildAt(i);
layout.setDrawingCacheEnabled(false);
}
}
}
然后在需要移动这个类的地方调用:
int xOffset = rightFrameLayout.getWidth();
centerFrameLayout.bringToFront();
centerFrameLayout.smoothScrollTo(-xOffset, SCROLL_DURATION);
handler.postDelayed(new Runnable() {
@Override
public void run() {
rightFrameLayout.setVisibility(View.INVISIBLE);
}
}, SCROLL_DURATION);
问题完美解决
在Android中动画移动一个View的位置,采用Scroller类实现Android动画之 View移动的更多相关文章
- Android中滑屏实现----触摸滑屏以及Scroller类详解 .
转:http://blog.csdn.net/qinjuning/article/details/7419207 知识点一: 关于scrollTo()和scrollBy()以及偏移坐标的设置/取值问 ...
- Android中Service的一个Demo例子
Android中Service的一个Demo例子 Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding. 本文,主要贴代码,不对Servic ...
- Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
今天继续讲解关于ActivityManager的使用,通过前面一节的学习,我们学会了如何利用ActivityManager获取系统里 正在运行的进程.本文要讲解的知识点是利用这些进程信息获取系统里正在 ...
- c++中在一个类中定义另一个只有带参数构造函数的类的对象
c++中在一个类中定义另一个只有带参数构造函数的类的对象,编译通不过 #include<iostream> using namespace std; class A { public: ...
- 【动画消消乐 】仿ios、android中常见的一个loading动画 074
前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...
- 浅谈android中只使用一个TextView实现高仿京东,淘宝各种倒计时
今天给大家带来的是只使用一个TextView实现一个高仿京东.淘宝.唯品会等各种电商APP的活动倒计时.近期公司一直加班也没来得及时间去整理,今天难得歇息想把这个分享给大家.只求共同学习,以及自己兴许 ...
- Android 中如何从一个App启动另外一个App(如启动支付界面、启动地图界面、应用商场下载App等场景)
假定两个App,分别是A和B,当A运行某个功能需要启动B,一种是启动B应用,一种直接进入B的某个Activity.搜了很多资料,没有一个完整的.下面就A--Android5.1.1.B--Androi ...
- Android中自己定义一个shade.xml
自己定义一个shade: <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!- ...
- Android中自定义Activity和Dialog的位置大小背景和透明度等
1.自定义Activity显示样式 先在res/values下建colors.xml文件,写入: view plainprint? 1. <?xml version="1.0" ...
随机推荐
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- labview多个并行循环同时退出
labview中停止并行的循环 问题: 在labview中我如何停止两个并行的循环?我使用一个局部变量,但是当我停止程序执行后,第二次不能运行程序.我该如何解决这个问题呢? 解答: 你使用局部变量来 ...
- Google中rel="canonical"的相关解释和用法
转载原地址 http://blog.sina.com.cn/s/blog_673b01740100jxlz.html 近听到很多SEO 对在页面的规范版本用规范 URL 标签( canonical U ...
- 转载github上最全的资源教程--前端涉及的所有知识体系
以下地址为园子里一个哥们总结的前端所涉及的所有知识体系 http://www.cnblogs.com/bymax/p/5878113.html 很值得学习参考
- SQLite数据库连接方式
http://blog.csdn.net/ZF101201/archive/2010/05/26/5626365.aspx SQLite.NET Type: .NET Framework Cla ...
- 关于H-Fox 函数
........We arrive at the following results which provide the sine and cosine transforms of the H-fun ...
- linux 查看当前路径命令:pwd
查看当前路径命令:pwd pwd命令能够显示当前所处的路径. 这个命令比较简单,如果有时在操作过程中忘记了当前的路径,则可以通过此命令来查看路径,其执行方式为: # pwd /home/samlee ...
- Java中字符串相等与大小比较
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- socket.io+angular.js+express.js做个聊天应用(一)
node,express开发环境等安装如果已经搞好了. justhacker@justhacker-ThinkPad-Edge-E440:~/projects/nodejs$ express -e c ...
- quartz 2.2.1 jdbc 连接池参数配置
/** The JDBC database driver. */指定连接驱动 public static final String DB_DRIVER = "driver"; /* ...