Scroller 实现控件缓慢平移的效果

本文通过示例代码详细介绍了如何利用Scroller组件在Android中实现控件平滑且缓慢的移动效果,注释清晰,易于理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说直接上代码 看一下就懂了我有注释:

package com.example.k.scroller;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private ListView listView = null;
    private MyViewGroup myViewGroup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);

    }

    public void scrook(View view) {

        myViewGroup.beginScroll();

    }

}
====================================================================================


package com.example.k.scroller;

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

/**
 * Created by k on 2016/5/31.
 */
public class MyViewGroup extends LinearLayout {
    private boolean s1=true;
    Scroller mScroller=null;
    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller=new Scroller(context);
        // TODO Auto-generated constructor stub
    }
    //计算滚动,draw()方法中会调用此方法,而draw()在invalidate()或postInvalidate()后就会被调用,
    @Override
    public void computeScroll() {
        //mScroller.computeScrollOffset()方法在invalidate()方法没被调用之前返回值一直是false,当到达了目标坐标就会继续返回false就结束了
        Log.i("ok","==========="+mScroller.computeScrollOffset());
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            postInvalidate();
        }
    }
    public void beginScroll(){
        if (!s1) {
            /*下面的方法第1个参数开始移动的X坐标,第2个位开始的Y坐标,第3个为目标X坐标,第4个位目标Y坐标,第五个是整个移动过程的时间
            *可以想象屏幕后面有一块大画板,然后一块和画板一样宽的木板盖上去,木板中间被挖了一个长方形的洞,现在长方形左上角的坐标为0,0;
            *我们在长方形上放一个组件按钮;然后设置第一个参数为-500,注意是负500,此时这块木板就会往左移动500像素,屏幕上的组件也就实现了
            * 向右移动了500像素,很简单,想象一下就明白了。
             */
            mScroller.startScroll(-500, 0, 500, 0, 300);
            s1 = true;
        } else {
            //正值往右边移,负值反之
            mScroller.startScroll(0, 0, -500, 0, 300);
            s1 = false;
        }
        invalidate();
    }
}
=====================================================================================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="scroll"
        android:onClick="scrook" />


    <com.example.k.scroller.MyViewGroup
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/myviewGroup">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_gravity="center_horizontal" />

    </com.example.k.scroller.MyViewGroup>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值