【问题标题】:How to make custom seek bar in android?如何在android中制作自定义搜索栏?
【发布时间】:2015-11-06 17:07:29
【问题描述】:

我想让客户搜索栏像这张图片:

我检查了thisthis。 如果有人对此有任何想法..请分享。谢谢

【问题讨论】:

标签: android seekbar


【解决方案1】:

如果我理解正确,您想在沿球体移动的实际滑块上方创建一个数值。

您可以做的一件事是通过将文本直接“合并”到球体图像的可绘制文件上,在滑块的实际图像上方写入文本。

我假设您使用的是您在原始问题中提供的first tutorial

public class CustomSeekBar extends Activity {
  SeekBar mybar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_seek_bar);
        mybar = (SeekBar) findViewById(R.id.seekBar1);

        mybar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

        //add here your implementation
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

        //add here your implementation
      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
            int value = seekBar.getProgress(); //this is the value of the progress bar (1-100)
            //value = progress; //this should also work
            String valueString = value + ""; //this is the string that will be put above the slider
            seekBar.setThumb(writeOnDrawable(R.drawable.thumbler_small, value));        
      }
    });
    }

    public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); //Change this if you want other color of text
    paint.setTextSize(20); //Change this if you want bigger/smaller font

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint); //Change the position of the text here

    return new BitmapDrawable(bm);
}
} 

这会从您的资源中获取一个可绘制对象,并在其上绘制一些文本并返回新的可绘制对象。使用seekBar.getProgress(); 从搜索栏获取所需的值。

我建议稍微清理一下代码,因为现在每次触摸搜索栏时它都会创建一个新的绘制对象,这真的很糟糕。

你需要做更多的事情才能使它工作只有当你点击球体时...

【讨论】:

  • 实际上我无法理解这段代码..这就是我要求完整代码的原因..谢谢..
  • 当我使用 writeOnDrawable() 方法时,拇指大小会从实际大小减小。对此有什么想法吗?
【解决方案2】:

XML 代码

 <SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:progressDrawable="@drawable/seek_bar"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:thumb="@drawable/thumbler_small"
    android:indeterminate="false" />

seek_bar.xml

 <?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+android:id/SecondaryProgress"
        android:drawable="@drawable/first"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/second"/>
</layer-list>

第一个可绘制对象为空或未显示完整区域。

第二个可绘制对象是填充或着色整个区域。

this链接帮助你更多。

【讨论】:

  • 先生,我收到错误error: Error: No resource found that matches the given name (at 'id' with value '@+android:id/ SecondaryProgress').
  • maveň - 这些是存储在可绘制文件夹中的名为 firstsecond 的进度条图像。
【解决方案3】:

为什么不编写自定义搜索栏视图。

这里是一些示例代码,用于将文本放在搜索栏拇指滑块的中间,并且文本会随着滑块移动。应该很容易适应滑块顶部的打印文本。

public class CustomSeekBar extends SeekBar {

private Paint textPaint;

private Rect textBounds = new Rect();

private String text = "";


public CustomSeekBar(Context context) {
    super(context);
    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);

}

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);

}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);


    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);
        }

@Override
protected synchronized void onDraw(Canvas canvas) {
    // First draw the regular progress bar, then custom draw our text
    super.onDraw(canvas);

    // Now progress position and convert to text.
    text = Integer.toString(getProgress());

    // Now get size of seek bar.
    float width = getWidth();
    float height = getHeight();

    // Set text size.
    textPaint.setTextSize((height * 3) / 4);
    // Get size of text.
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    // Calculate where to start printing text.
    float position = (width / getMax()) * getProgress();

    // Get start and end points of where text will be printed.
    float textXStart = position - textBounds.centerX();
    float textXEnd = position + textBounds.centerX();

    // Check does not start drawing outside seek bar.
    if(textXStart < 0) textXStart = 0; 

    if(textXEnd > width)
        textXStart -= (textXEnd - width);

    // Calculate y text print position.
    float yPosition = (height / 2) - textBounds.centerY(); //- (textBounds.bottom / 2);

    canvas.drawText(text, textXStart, yPosition, textPaint);
}

public synchronized void setTextColor(int color) {
    super.drawableStateChanged();
    textPaint.setColor(color);
    drawableStateChanged();
}

}

希望对你有用。

【讨论】:

    【解决方案4】:

    如果您想使外观与图像中显示的外观相同,则必须在 xml 文件中为 seekbar 提供 minHeight 和 maxHeight 两个属性。 例如:

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:progressDrawable="@drawable/seek_bar"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:thumb="@drawable/thumbler_small"
        **android:minHeight = "20dp"
        android:maxHeight = "20dp"**   
        android:indeterminate="false" />
    

    请检查上面代码中突出显示的属性。

    【讨论】:

      【解决方案5】:

      首先在你的drawable文件夹中创建一个xml文件(如果没有创建一个新的)
      粘贴此代码或关注tutorial 以尽快完成此操作

      repeatbottombackground.xml

           <bitmap android:dither="true" android:src="@drawable/bottom_bar_repeat" android:tilemode="repeat"      xmlns:android="http://schemas.android.com/apk/res/android">
      
          </bitmap>
      

      seek_thumb.xml

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

      我给你一个link我希望它可以帮助你

      对不起,我不必解释更多,只需按照教程进行即可:)


      敬上,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多