本文介绍了Android:在基于画布的视图上启用滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom view that extends View. It displays drawn shapes, and allows the user to add to those drawings via drawing touch events to the View via onDraw.

我已启用 ScaleGestureDetector,以便用户可以放大特定部分并进行绘制,但由于我使用单点触控进行绘制,因此他们无法使用手指在放大的视图周围平移.

I've enabled the ScaleGestureDetector so that the user can zoom in on a particular section and draw, but as I am using single-touch to draw, they cannot use their finger to pan around the zoomed in View.

我尝试为视图启用滚动条,以便在放大时显示滚动条,用户可以使用滚动条进行平移……但我根本无法显示滚动条.

I've tried to enable scrollbars for the View such that when they are zoomed in, the scrollbars are displayed and can be used by the user to pan... but I simply can't get the scrollbars to be displayed.

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

Essentially, what I am doing is to call the View's awakenScrollBars() method in my ScaleListener's onScale() method when the user is zoomed in, which triggers invalidate(). I enabled the scrollbars via both the XML, and programatically in onCreate(), but I can't trigger the scrollbars to be visible. Here is my XML:

<?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>

这是我的 onCreate():

And here is my onCreate():

// set scrollbars
setHorizontalScrollBarEnabled(true);
setVerticalScrollBarEnabled(true);

在 onDraw 中,我可以通过 isHorizo​​ntalScrollBarEnabled()isVerticalScrollBarEnabled()awakenScrollBars() 验证滚动条是否已启用code>onScale() 返回 true,但滚动条不可见.

In onDraw, I can verify that the scrollbars are enabled via isHorizontalScrollBarEnabled() and isVerticalScrollBarEnabled(), and awakenScrollBars() in onScale() returns true, but the scroll bars are just not visible.

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

Any suggestions on how to proceed? Containing the custom View in a ScrollView layout doesn't seem to be an option, as that only supports vertical scrolling.

谢谢,

保罗

推荐答案

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

If you are creating your custom view programatically, then the answer is the following:in order to show scrollbars on custom view class, the method "initializeScrollbars" should be called during initialization (in constructor for example).

此方法采用 TypedArray 类型的一个非常晦涩的参数.要获得合适的 TypedArray 实例,您需要创建自定义样式条目 - 只需在resvalues"目录中创建文件attrs.xml",内容如下:

This method takes one very obscure parameter of type TypedArray. To get suitable TypedArray instance you need to create custom styleable entry - just create file "attrs.xml" in your "resvalues" directory with the following content:

<?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>

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

Also the complete scrollbars initialization code is (place it on your custom view constructor):

setHorizontalScrollBarEnabled(true);
setVerticalScrollBarEnabled(true);

TypedArray a = context.obtainStyledAttributes(R.styleable.View);
initializeScrollbars(a);
a.recycle();

附言解决方案在Android 2.0上测试

P.S. the solution was tested on Android 2.0

我忘了补充:还应该覆盖computeVerticalScrollRange"和computeHorizo​​ntalScrollRange"方法.他们应该只返回画布的虚构宽度和高度.

I have forgot to add:also "computeVerticalScrollRange" and "computeHorizontalScrollRange" methods should be overridden. They should just return imaginary width and height of the canvas.

这篇关于Android:在基于画布的视图上启用滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:35