Android_Scroller滑动动画

本文介绍如何使用Scroller类为Android视图添加平滑滚动动画,包括基本使用步骤、实战示例以及在ListView中实现滑动删除功能。通过Scroller的startScroll()方法控制动画时间和在computeScroll()中更新滚动位置,解决视图滚动生硬的问题。
摘要由CSDN通过智能技术生成

转载请注明出处:http://blog.csdn.net/y22222ly/article/details/51842218

当我们在使用view的scrollTo()或scrollBy()时,会发现这个滑动很生硬,没有动画效果,一下就过去了,就像我前篇文章提到的那样。如果能平滑的滑动回去的话,最好不过了,刚好安卓提供一个Scroller类,专门来处理view在scrollTo()或scrollBy()时没有滑动效果的问题。

Scroller基本使用

下例源码引用自:http://ipjmc.iteye.com/blog/1615828
该大神详细说明了各个方法的用法。

import android.content.Context;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.view.View;  
import android.widget.LinearLayout;  
import android.widget.Scroller;  

public class CustomView extends LinearLayout {
     

    private static final String TAG = "Scroller";  

    private Scroller mScroller;  

    public CustomView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        mScroller = new Scroller(context);  
    }  

    //调用此方法滚动到目标位置  
    public void smoothScrollTo(int fx, int fy) {  
        int dx = fx - mScroller.getFinalX();  
        int dy = fy - mScroller.getFinalY();  
        smoothScrollBy(dx, dy);  
    }  

    //调用此方法设置滚动的相对偏移  
    public void smoothScrollBy(int dx, int dy) {  

        //设置mScroller的滚动偏移量  
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);  
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果  
    }  

    @Override  
    public void computeScroll() {  

        //先判断mScroller滚动是否完成  
        if (mScroller.computeScrollOffset()) {  

            //这里调用View的scrollTo()完成实际的滚动  
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  

            //必须调用该方法,否则不一定能看到滚动效果  
            postInvalidate();  
        }  
        super.computeScroll();  
    }  
} 

使用Scroller分三步:
1. 声明与初始化
2. 调用startScroll()一定要注意4个参数,前2个为当前滑动偏移量,后2个为需要滑动的偏移量
3. 重写view的computeScroll()方法,调用mScroller.computeScrollOffset()计算滑动值
4. 使用计算后的Scroller实例,调用view的scrollTo()进行滑动

Scroller实战

根据上述代码后自己学习后,对前一篇文章提到的滑动处理scrollTo()问题做了实现。
效果图:
这里写图片描述
源码:

package com.example.y2222.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.example.y2222.myapplication.R;

/**
 * Created by raise.yang on 2016/06/29.
 */
public class GestureDemoView extends LinearLayout {
   
    //1,定义GestureDetector类
    private GestureDetector m_gestureDetector;
    //1,定义Scroller类
    private Scroller m_scroller;
    private int m_max_scrollX;

    public GestureDemoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GestureDemoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //设置为可点击
        setClickable(true);
        //2,初始化手势类,同时设置手势监听
        m_gestureDetector
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值