众所周知,Android 视图具有滚动功能。
为了显示滚动条,有两件事要做。
- 在视图的 xml 声明中添加 android:scrollbars="horizontal|vertical"
- 覆盖View类的computeHorizontalScrollRange/computeVerticalScrollRange方法,使得返回值大于computeHorizontalScrollExtent/computeVerticalScrollExtent方法的返回值
之后,当您调用 scrollTo 或 scrollBy 方法时,滚动条应该会自动显示。
对不起,我的英语很差,如果有任何拼写或语法错误。你可以试试这个:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".MainActivity" >
<com.netease.test.testscroll.ScrollImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:scrollbars="horizontal|vertical"
android:src="@drawable/pp" />
</LinearLayout>
ScrollImageView 是一个自定义视图,你可以这样实现:
public class ScrollImageView extends ImageView {
static final String TAG = "ScrollImageView";
private Rect mContentRect = new Rect();
GestureDetector mDetector;
OnGestureListener mListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("onScroll", "before:distanceX = " + distanceX
+ ", distanceY = " + distanceY);
scrollBy((int)distanceX, (int)distanceY);
// boolean value = awakenScrollBars();
Log.i("onScroll", "after:current ScrollX=" + getScrollX()
+ ", ScrollY=" + getScrollY());
return true;
}
};
public ScrollImageView(Context context) {
this(context, null);
}
public ScrollImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setScaleType(ScaleType.MATRIX);
mDetector = new GestureDetector(getContext(), mListener);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mContentRect.set(getPaddingLeft(), getPaddingTop(), getWidth()
- getPaddingRight(), getHeight() - getPaddingBottom());
}
@Override
protected int computeHorizontalScrollRange() {
return getDrawable().getIntrinsicWidth();
}
@Override
protected int computeHorizontalScrollExtent() {
return mContentRect.width();
}
@Override
protected int computeHorizontalScrollOffset() {
return Math.max(0, getScrollX());
}
private int getScrollRangeX() {
return computeHorizontalScrollRange() - computeHorizontalScrollExtent();
}
@Override
protected int computeVerticalScrollRange() {
return getDrawable().getIntrinsicHeight();
}
@Override
protected int computeVerticalScrollExtent() {
return mContentRect.height();
}
@Override
protected int computeVerticalScrollOffset() {
return Math.max(0, getScrollY());
}
private int getScrollRangeY() {
return computeVerticalScrollRange() - computeVerticalScrollExtent();
}
}
指定一个名为“pp”的大 png,然后滚动图像,您应该会看到滚动条。