Android群英传——第五章实现滑动的7种方法(四)scrollTo和scrollBy

scrollTo()和scrollBy()的区别

  • scrollTo()代表让View移动到一个具体的坐标点:scrollTo(x,y) 移动到(x,y)点
  • scrollBy()则代表横纵增量:设原点为(a,b),则scrollBy(dx,dy) 后 坐标变为(a+dx, b+dy)
  • scrollBy和scrollTo的移动,是移动View的content,即让View的内容移动(比如listView 地图控件 webview 重要的事情说三种!),如果在viewGroup中使用这两个方法,则它的子view们将会发生移动
  • 如果是ImageView调用了scroll,则drawable就会发生移动
  • scroll方法移动的是ImageView(即window、可视区域),而不是drawable,所以会造成往正方向移动时,drawabe往负方向移动
  • 就像一个放大镜 在一个地图上来回的移动(当然它不放大),地图(drawable)本身是不移动的,移动的是放大镜(ImageView)
  • 平移是瞬间完成的

    制作了一个简易的map如下: 
    这里写图片描述 
    图片大 所以有点卡

    以下是ImageView的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by feathers on 16-11-16.
 */

public class MapImageView extends ImageView {

    public MapImageView(Context context) {
        super(context);
    }

    public MapImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MapImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // 上一个
    private int lastX = 0;
    private int lastY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int currentX = (int) event.getRawX();
                int currentY = (int) event.getRawY();
                // 注意, 移动的不是drawable
                int offsetX = currentX - lastX;
                int offsetY = currentY - lastY;
                this.scrollBy(-offsetX, -offsetY);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
        }
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity">

    <com.example.myapplication.MapImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map"
        android:scaleType="matrix"/>

</RelativeLayout>
posted @ 2016-11-26 15:40  天涯海角路  阅读(67)  评论(0)    收藏  举报