使用SeekBar onProgressChanged进行自定义视图

时间:2014-03-18 13:03:53

标签: android android-custom-view progress custom-component android-seekbar

我创建了一个可重复使用的自定义组件(带有绘制位图的画布的自定义视图),它是一个可用于任何内容的滑块(音量控制/评级系统)。问题是我需要监控它的进度,比如搜索栏onProgressChanged。

我不知道我会实现这个。我无法使用搜索栏并尝试重新使用它,我需要使用自定义组件..

有人会对此有任何想法吗?

我的布局如下:

enter image description here 所以我需要监控我的滑动图像的运动并尝试从中计算某种进展还是有更简单的方法?

任何关于此的输入都将受到赞赏,这是我在自定义组件上的第一次努力

修改

以下是我的xml布局的外观:

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id = "@+id/slider1"
        android:layout_alignParentTop="true">

        <com.cs.customSlider.slider.app.sliderView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sliderView"
            sliderView:diagonalSlide="false"
            sliderView:translateAxisX="false"/>
        <ImageView
            android:id="@+id/slider_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/slider_frame"
            android:layout_centerHorizontal="true">
        </ImageView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

免责声明:这可能不会像我想的那样100%工作,但这应该让你开始。

您的customView显然扩展了View。因此,使用getLeft()来获取视图的位置。使用getParent()来获取包含此视图的ViewGroup,该视图可能是线性或相对布局并获得它的宽度。

现在你粗略地了解了你在父母中的观点,更远的权利,更多的进步。&#34;从parentView的宽度减去customView的宽度,因为我们从左侧测量,显然,customView的左侧永远不会触及父视图的右侧。

希望有助于你到达那里......

    View view = ...;

    //position of my view;
    int pos = view.getLeft();

    //Number of pixels my view can slide across the screen.
    ViewGroup vg = (ViewGroup) view.getParent();
    int width = vg.getMeasuredWidth();

    //right most position my view can reach,
    int available_width = width - view.getMeasuredWidth();

    float progress = ((float) pos) / available_width;
相关问题