【问题标题】:Changing size of seekbar thumb更改搜索栏拇指的大小
【发布时间】:2012-03-30 19:02:38
【问题描述】:

我正在为搜索栏拇指使用可绘制对象

android:thumb="@drawable/thumb"

如何以 dip 为单位设置此拇指的大小?因为我使用了类似

的搜索栏样式
<style name="CustomSeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:minHeight">8dip</item>
    <item name="android:maxHeight">8dip</item>
    <item name="android:thumbOffset">8dip</item>
</style> 

我希望拇指具有12dip 的高度和宽度。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    为我设置拇指大小的最灵活方法是创建分层可绘制对象,形状为占位符thumb_image.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape>
                <size
                    android:height="40dp"
                    android:width="40dp" />
    
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
        <item android:drawable="@drawable/scrubber_control_normal_holo"/>
    
    </layer-list>
    

    所以基本上改变形状的大小会拉伸drawable,形状是透明的,所以它不可见。这种拇指的最小尺寸也是位图的尺寸。

    这是布局示例:

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:thumb="@drawable/thumb_image" />
    
    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    【讨论】:

    • 如果你使用那种 layer-list-thumb.xml 作为在 selector-drawable 项目中的可绘制对象,这也可以工作。完美!
    • 这适用于使其更大......但如果它应该更小怎么办?
    • 将 thumb_image.xml 放置在项目文件夹结构中的哪个位置是明智的?
    • 请保存在文件夹drawable/thumb_image.xml
    • 如何让它变小?像一个巨大的剪掉的东西一样地雷,它的弱智,我真的必须在 photoSHOp 中让它变小吗!??
    【解决方案2】:

    我也在寻找一种方法来做类似的事情,在查看了一些其他问题并将所有答案放在一起之后,我想出了一种方法来调整 onCreate() 中的 Seekbar 拇指大小。

    这是我来自 onCreate() 的代码,稍微适应了您的需求。

    ViewTreeObserver vto = mySeekBar.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            Resources res = getResources();
            Drawable thumb = res.getDrawable(R.drawable.thumb);
            int h = mySeekBar.getMeasuredHeight() * 1.5; // 8 * 1.5 = 12
            int w = h;
            Bitmap bmpOrg = ((BitmapDrawable)thumb).getBitmap();
            Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOrg, w, h, true);
            Drawable newThumb = new BitmapDrawable(res, bmpScaled);
            newThumb.setBounds(0, 0, newThumb.getIntrinsicWidth(), newThumb.getIntrinsicHeight());
            mySeekBar.setThumb(newThumb);
    
            mySeekBar.getViewTreeObserver().removeOnPreDrawListener(this);
    
            return true;
            }
        });
    

    请注意,这适用于调整拇指大小,但它可能会被剪裁,因为它比包含的搜索栏大(不确定)。 如果它确实被剪裁了,您可能需要创建一个更高的搜索栏并创建自己的搜索栏背景,以匹配您想要的显示尺寸。

    希望这会有所帮助!

    【讨论】:

    • 我复制了你的代码,它剪切了我的图像。我不知道你的 1.5 是从哪里得到的,但是当我将其更改为 1 时,不会发生剪辑。
    • 1.5 是为了回答 OP 的问题。他希望拇指大小为 12dip,而搜索栏为 8dip,因此我建议将搜索栏的大小乘以 1.5 以获得正确的拇指大小,因为您无法在 dip 中指定大小。
    • 啊,我明白了。不管怎样,你的代码对我帮助很大,点个赞吧!
    • 你太棒了!我不得不缩小拇指,它起作用了。正在收到一个类广播异常,但随后找到了以下答案并且一切正常。 stackoverflow.com/questions/18459618/…
    【解决方案3】:

    从 sdk 你可以看到这是基本 SeekBar 的样式:

    <style name="Widget.SeekBar">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:minHeight">20dip</item>
        <item name="android:maxHeight">20dip</item>
        <item name="android:thumb">@android:drawable/seek_thumb</item>
        <item name="android:thumbOffset">8dip</item>
        <item name="android:focusable">true</item>
    </style>
    

    以此作为拇指选择器:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true"
              android:state_window_focused="true"
              android:drawable="@drawable/seek_thumb_pressed" />
    
        <item android:state_focused="true"
              android:state_window_focused="true"
              android:drawable="@drawable/seek_thumb_selected" />
    
        <item android:state_selected="true"
              android:state_window_focused="true"
              android:drawable="@drawable/seek_thumb_selected" />
    
        <item android:drawable="@drawable/seek_thumb_normal" />
    
    </selector>
    

    您应该能够使用这些作为基础来替换当前正在使用的可绘制对象(这看起来像您必须要进入的阶段)。

    根据可绘制文件夹大小桶(drawable-hdpidrawable-large-hdpi 等),这些可绘制对象可以在不同屏幕上具有不同的尺寸,或者您可以使不同的 SeekBars 查看不同的样式/可绘制尺寸,如下所示:

    <style name="SeekBar.Thumb.Smiley" parent="@android:style/Widget.SeekBar">
        <item name="android:thumb">@drawable/smiley</item>
    <style>
    
    <style name="SeekBar.Thumb.Smiley.Large" parent="@android:style/Widget.SeekBar">
        <item name="android:thumb">@drawable/smiley_large</item>
    <style>
    
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        style="@style/SeekBar.Thumb.Smiley.Large"/>
    

    【讨论】:

      【解决方案4】:

      我是这样工作的:

      public class FlexibleThumbSeekbar extends SeekBar {
      
          public FlexibleThumbSeekbar(Context context) {
              super(context);
          }
      
          public FlexibleThumbSeekbar(Context context, AttributeSet attrs) {
              super(context, attrs);
      
              Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.thumb);
              Bitmap thumb = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(thumb);
              canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
              new Rect(0, 0, thumb.getWidth(), thumb.getHeight()), null);
              Drawable drawable = new BitmapDrawable(getResources(), thumb);
              setThumb(drawable);
          }
      }
      

      只是一个示例,在您的实际使用中,您应该在 xml 文件中定义拇指的宽度、高度和图片。

      【讨论】:

        【解决方案5】:

        另一个对我有用的简单解决方案是使用scaleXscaleY。这样整个搜索栏会更大,而不仅仅是拇指。

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleX="2"
            android:scaleY="2" />
        

        【讨论】:

        • 我的搜索栏在约束布局内,当苹果缩放时,栏在视图区域之外增长,所以拇指变得不可见。
        • 可能在 LinerLayout 或 RelativeLayout 中有用,但在 ConstraintLayout 中,正如 Damn Vegetables 所说,它超出了查看范围。
        • 在线性布局中没有用
        【解决方案6】:

        根据需要创建一个具有高度和宽度的矩形。将此形状用作拇指的可绘制对象。

        【讨论】:

        • 但我想用一张图片作为拇指。我想调整这张图片的大小。
        【解决方案7】:

        我通过这种方式解决了我的问题

        首先,我通过源converter 将我的图像/自定义拇指/位图/其他转换为 SVG 文件

        其次,我将我的 SVG 文件转换为可通过源 SVG to vector 绘制的矢量

        然后我在 .xml 文件中使用了矢量代码,该文件在矢量标签中有 android:width="28dp"android:height="28dp" 属性。在这里我们可以改变拇指的大小。

        最后,我在 SeekBar 标签的android:thumb="@drawable/thumb.xml" 中使用了我的 thumb.xml

        这是我的 thumb.xml

        <?xml version="1.0" encoding="utf-8"?>
        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:viewportWidth="56"
            android:viewportHeight="56"
            android:width="28dp"
            android:height="28dp">
            <group
                android:scaleX="2.0"
                android:scaleY="-2.0"
                android:translateY="56">
                <path
                    android:pathData="M28 14A14 14 0 0 1 14 28 14 14 0 0 1 0 14 14 14 0 0 1 14 0 14 14 0 0 1 28 14Z"
                    android:fillColor="#6b6b6b"
                    android:fillAlpha="0.205" />
            </group>
            <group
                android:scaleX="0.1"
                android:scaleY="-0.1"
                android:translateY="56">
                <path
                    android:pathData="M135 376C83 353 40 311 40 281c0 -22 13 -34 85 -80 85 -54 115 -59 115 -22 0 22 -59 77 -98 92 -26 10 -26 11 36 50 72 46 81 69 25 69 -21 -1 -51 -7 -68 -14z"
                    android:fillColor="#000000" />
                <path
                    android:pathData="M320 366c0 -26 55 -81 98 -97l26 -10 -44 -26c-88 -51 -106 -83 -46 -83 72 0 179 81 163 124 -8 20 -156 116 -179 116 -12 0 -18 -8 -18 -24z"
                    android:fillColor="#000000" />
            </group>
        </vector>
        

        【讨论】:

          【解决方案8】:

          我想用更详细的答案更新@andrew 的答案,因为它已经 7 年了..

          正如他所说;

          你应该创建layered-drawable thumb_image.xml (请将它保存在drawable文件夹中):

          <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          
          <item>
              <shape>
                  <size
                      android:height="40dp"
                      android:width="40dp" />
          
                  <solid android:color="@android:color/transparent" />
              </shape>
          </item>
          <item android:drawable="@drawable/scrubber_control_normal_holo"/>
          

          然后您应该将此属性行添加到布局文件中的 SeekBar :

          android:thumb="@drawable/thumb_image"
          

          当你这样做时,Android Studio 会告诉你它在你的 @drawable 文件夹中找不到“scrubber_control_normal_holo.xml”

          所以你应该把它放在那里。您可以在 SDK 文件夹中找到这种类型的 android 资源。默认情况下,“Android Studio IDE”将安装在“C:\Program Files\Android\Android Studio”,“Android SDK”安装在c:\Users\username\AppData \Local\Android\Sdk.

          在您的 SDK 文件夹中,您应该遵循此路径 platforms->android-xx->data->res->drawable

          然后在这个“drawable”文件夹中你可以找到scrubber_control_selector_holo.xml,你应该把它复制到你的Android项目的drawable文件夹中。

          现在 android studio 将能够找到 scrubber_control_selector_holo.xml ,但它还没有工作,因为在这个 .xml 文件中;为了达到进化的最高奇点,您还需要复制 4 个文件。否则 Android Studio 会给你这样的错误:" Resource linking failed because something is private blablabla"

          scrubber_control_disabled_holo

          scrubber_control_pressed_holo

          scrubber_control_focused_holo

          scrubber_control_normal_holo

          我在 platforms->android-xx->data->res->drawable-mdpi 中找到了这些文件 (不同尺寸的dpi选项不同,我用的是mdpi里的)

          也将这些 .png 文件复制到您的 @drawable 文件夹中。

          然后作为最后一步(很抱歉这个答案太长,但我不想引起任何混淆)

          您应该编辑 scrubber_control_selector_holo.xml 文件的这些行:

          android:drawable="@android:drawable/scrubber_control_disabled_holo" />
          android:drawable="@android:drawable/scrubber_control_pressed_holo" />
          android:drawable="@android:drawable/scrubber_control_focused_holo" />
          android:drawable="@android:drawable/scrubber_control_normal_holo" />
          

          您应该将“@android:drawable”部分替换为您的项目资源,而不是 android 中的资源。

          现在应该可以正常使用了☺

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-20
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多