Android:在基于画布的视图上启用滚动条

Pau*_*ega 1 android scroll android-canvas

我有一个扩展View的自定义视图.它显示绘制的形状,并允许用户通过onDraw将触摸事件绘制到视图中来添加到这些绘图.

我启用了ScaleGestureDetector,以便用户可以放大特定部分并进行绘制,但是当我使用单点触摸绘制时,他们无法使用手指在放大的视图中平移.

我已经尝试为View启用滚动条,当它们被放大时,滚动条会显示并且可以被用户用来平移...但我根本无法显示滚动条.

本质上,我正在做的是当用户放大时触发awakenScrollBars()我的ScaleListener方法中的View 方法.我通过XML和编程方式启用了滚动条,但是我无法触发滚动条可见.这是我的XML:onScale()invalidate()onCreate()

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.package.name.Canvas
    android:id="@+id/canvas"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:scrollbars="horizontal|vertical" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的onCreate():

// set scrollbars
setHorizontalScrollBarEnabled(true);
setVerticalScrollBarEnabled(true);
Run Code Online (Sandbox Code Playgroud)

在onDraw有,我可以确认的是,滚动条通过启用isHorizontalScrollBarEnabled()isVerticalScrollBarEnabled(),并awakenScrollBars()onScale()返回true,但滚动条只是不可见.

有关如何进行的任何建议?在ScrollView布局中包含自定义视图似乎不是一个选项,因为它只支持垂直滚动.

谢谢,

保罗

Rus*_*hyn 11

如果以编程方式创建自定义视图,则答案如下:为了在自定义视图类上显示滚动条,应在初始化期间调用方法"initializeScrollbars"(例如在构造函数中).

此方法采用TypedArray类型的一个非常模糊的参数.要获得合适的TypedArray实例,您需要创建自定义可样式条目 - 只需在"res\values"目录中创建文件"attrs.xml",其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View">
    <attr name="android:background"/>
    <attr name="android:clickable"/>
    <attr name="android:contentDescription"/>
    <attr name="android:drawingCacheQuality"/>
    <attr name="android:duplicateParentState"/>
    <attr name="android:fadeScrollbars"/>
    <attr name="android:fadingEdge"/>
    <attr name="android:fadingEdgeLength"/>
    <attr name="android:fitsSystemWindows"/>
    <attr name="android:focusable"/>
    <attr name="android:focusableInTouchMode"/>
    <attr name="android:hapticFeedbackEnabled"/>
    <attr name="android:id"/>
    <attr name="android:isScrollContainer"/>
    <attr name="android:keepScreenOn"/>
    <attr name="android:longClickable"/>
    <attr name="android:minHeight"/>
    <attr name="android:minWidth"/>
    <attr name="android:nextFocusDown"/>
    <attr name="android:nextFocusLeft"/>
    <attr name="android:nextFocusRight"/>
    <attr name="android:nextFocusUp"/>
    <attr name="android:onClick"/>
    <attr name="android:padding"/>
    <attr name="android:paddingBottom"/>
    <attr name="android:paddingLeft"/>
    <attr name="android:paddingRight"/>
    <attr name="android:paddingTop"/>
    <attr name="android:saveEnabled"/>
    <attr name="android:scrollX"/>
    <attr name="android:scrollY"/>
    <attr name="android:scrollbarAlwaysDrawHorizontalTrack"/>
    <attr name="android:scrollbarAlwaysDrawVerticalTrack"/>
    <attr name="android:scrollbarDefaultDelayBeforeFade"/>
    <attr name="android:scrollbarFadeDuration"/>
    <attr name="android:scrollbarSize"/>
    <attr name="android:scrollbarStyle"/>
    <attr name="android:scrollbarThumbHorizontal"/>
    <attr name="android:scrollbarThumbVertical"/>
    <attr name="android:scrollbarTrackHorizontal"/>
    <attr name="android:scrollbarTrackVertical"/>
    <attr name="android:scrollbars"/>
    <attr name="android:soundEffectsEnabled"/>
    <attr name="android:tag"/>
    <attr name="android:visibility"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

完整的滚动条初始化代码也是(将它放在自定义视图构造函数上):

setHorizontalScrollBarEnabled(true);
setVerticalScrollBarEnabled(true);

TypedArray a = context.obtainStyledAttributes(R.styleable.View);
initializeScrollbars(a);
a.recycle();
Run Code Online (Sandbox Code Playgroud)

PS解决方案在Android 2.0上进行了测试

我忘了添加:还应该覆盖"computeVerticalScrollRange"和"computeHorizo​​ntalScrollRange"方法.它们应该只返回画布的假想宽度和高度.

  • 实际上,您可以替换资源文件并使用`final TypedArray a = context.getTheme().obtainStyledAttributes(new int [0]); initializeScrollbars(一);` (3认同)