gpt4 book ai didi

android - 将 SeekBar onProgressChanged 用于自定义 View

转载 作者:行者123 更新时间:2023-11-29 21:12:00 25 4
gpt4 key购买 nike

我创建了一个可重复使用的自定义组件(一个带有 Canvas 位图的自定义 View ),它是一个可用于任何事物(音量控制/评级系统)的 slider 。问题是我需要监控它的进度,比如搜索栏 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>

最佳答案

免责声明:这可能不会像我预期的那样 100% 有效,但这应该可以帮助您入门。

您的 customView 显然扩展了 View。所以使用 getLeft() 来获取 View 的位置。使用 getParent() 获取包含此 View 的 ViewGroup,这可能是线性或相对布局并获取其宽度。

现在你对你的 View 在父 View 中的位置有了一个大概的了解,越靠右,“进步”越大。从父 View 的宽度中减去自定义 View 的宽度,因为我们是从左侧测量的,显然自定义 View 的左侧永远不能触及父 View 的右侧。

希望这能帮助你实现目标......

    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;

关于android - 将 SeekBar onProgressChanged 用于自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com