Seekbar 还是水平 LinearLayout?
Seekbar OR Horizontal LinearLayout?
我需要在我的 android APP 中做类似这张图片的操作,但我不知道哪种方法最好。
值介于 0 和 150 之间,当然数字必须水平滚动(掩码是静态的)。
我有带有线条的蒙版图像,我想我应该以编程方式将数字放在充满 TextView 的水平 LinearLayout 中。我知道 TextViews 应该有的确切边距(以 dp 为单位)。
滚动后,TextViews 必须停在较大的行上,我必须能够获得中心的数字(当前选择的值)。
你将如何实现这一目标?
解决方案:
使用HorizontalPicker library很容易实现我想要的。
布局将如下所示。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.wefika.horizontalpicker.HorizontalPicker
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="@integer/picker_text_size"
app:sideItems="3"
android:layout_centerVertical="true"
android:marqueeRepeatLimit="-1"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/mask_image"
android:layout_centerVertical="true"
android:clickable="false"
android:scaleType="centerCrop"/>
检查this它可能满足您的要求。
我以前做过这个代码(抱歉它不是开源的或任何东西),所以我有一些坏消息要告诉你。比那个复杂一点。
理论上,您所问的大部分内容都可以通过 RecyclerView 完成,类似于以下代码:
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this, HORIZONTAL, false);
rv.setAdapter( // Some adapter that will generate the TextViews with numbers and the lines above/below the numbers);
然后是静态叠加层,您只需添加一个单独的布局即可。
上面代码的问题是你将无法完成这部分:
After scrolling, TextViews must stop at the larger lines
我发现实现此目的的唯一方法是创建 100% 自定义视图。这意味着,您必须从以下代码开始:
public class CustomHorizontalSeekBar extends View {
此 class 将有触摸事件并包含一个 Scroller 以帮助滚动、加速、减速,并且必须直接覆盖
@Override
protected void onDraw(Canvas canvas){
}
直接在canvas上执行所有的绘图,虽然要做到完美是一件很辛苦的事情,但肯定是一个令人满意的结果。
祝你好运,编码愉快。
我需要在我的 android APP 中做类似这张图片的操作,但我不知道哪种方法最好。
值介于 0 和 150 之间,当然数字必须水平滚动(掩码是静态的)。
我有带有线条的蒙版图像,我想我应该以编程方式将数字放在充满 TextView 的水平 LinearLayout 中。我知道 TextViews 应该有的确切边距(以 dp 为单位)。
滚动后,TextViews 必须停在较大的行上,我必须能够获得中心的数字(当前选择的值)。
你将如何实现这一目标?
解决方案:
使用HorizontalPicker library很容易实现我想要的。
布局将如下所示。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.wefika.horizontalpicker.HorizontalPicker
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="@integer/picker_text_size"
app:sideItems="3"
android:layout_centerVertical="true"
android:marqueeRepeatLimit="-1"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/mask_image"
android:layout_centerVertical="true"
android:clickable="false"
android:scaleType="centerCrop"/>
检查this它可能满足您的要求。
我以前做过这个代码(抱歉它不是开源的或任何东西),所以我有一些坏消息要告诉你。比那个复杂一点。
理论上,您所问的大部分内容都可以通过 RecyclerView 完成,类似于以下代码:
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this, HORIZONTAL, false);
rv.setAdapter( // Some adapter that will generate the TextViews with numbers and the lines above/below the numbers);
然后是静态叠加层,您只需添加一个单独的布局即可。
上面代码的问题是你将无法完成这部分:
After scrolling, TextViews must stop at the larger lines
我发现实现此目的的唯一方法是创建 100% 自定义视图。这意味着,您必须从以下代码开始:
public class CustomHorizontalSeekBar extends View {
此 class 将有触摸事件并包含一个 Scroller 以帮助滚动、加速、减速,并且必须直接覆盖
@Override
protected void onDraw(Canvas canvas){
}
直接在canvas上执行所有的绘图,虽然要做到完美是一件很辛苦的事情,但肯定是一个令人满意的结果。
祝你好运,编码愉快。