Custom seekbar in Android

Source: Internet
Author: User

In actual work, it is often necessary to change the color, style, and background image of some controls based on the artist's design, the following describes how to customize the background color, progress bar color, and slider image in Android.

First, the main. XML content in the layout folder is as follows:

   1. <?xml version="1.0" encoding="utf-8"?>   2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   3.         android:orientation="vertical" android:layout_width="fill_parent"   4.         android:layout_height="fill_parent">   5.   6.         <SeekBar android:id="@+id/seek" android:layout_width="300px"   7.                 android:layout_height="wrap_content" android:max="100"   8.                 android:progress="50" android:progressDrawable="@drawable/seekbar_img"   9.                 android:thumb="@drawable/thumb" />  10. </LinearLayout>

Note that its Android: progressdrawable = "@ drawable/seekbar_img" and Android: thumb = "@ drawable/thumb" correspond to the picture of the progress bar and the picture of the slider, the image here can also be an XML file in our custom drawable, which can be understood as how the two attributes should be displayed, @ drawable/seekbar_img and @ drawable/thumb correspond to the files seekbar_img.xml and thumb in the drawable folder respectively. XML, which shows how to display the progress bar and slider

Seek_thumb.xml in the system. The content is as follows:

   1. <?xml version="1.0" encoding="utf-8"?>   2. <!-- Copyright (C) 2008 The Android Open Source Project   3.   4.      Licensed under the Apache License, Version 2.0 (the "License");   5.      you may not use this file except in compliance with the License.   6.      You may obtain a copy of the License at   7.   8.           http://www.apache.org/licenses/LICENSE-2.0   9.  10.      Unless required by applicable law or agreed to in writing, software  11.      distributed under the License is distributed on an "AS IS" BASIS,  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  13.      See the License for the specific language governing permissions and  14.      limitations under the License.  15. -->  16.  17. <!-- This is the thumb on the seek bar. -->  18. <selector xmlns:android="http://schemas.android.com/apk/res/android">  19.  20.     <item android:state_pressed="true"  21.           android:state_window_focused="true"  22.           android:drawable="@drawable/seek_thumb_pressed" />  23.  24.     <item android:state_focused="true"  25.           android:state_window_focused="true"  26.           android:drawable="@drawable/seek_thumb_selected" />  27.  28.     <item android:state_selected="true"  29.           android:state_window_focused="true"  30.           android:drawable="@drawable/seek_thumb_selected" />  31.  32.     <item android:drawable="@drawable/seek_thumb_normal" />  33.  34. </selector>

Progress_horizontal.xml in the system. The content is as follows:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! -- Copyright (c) 2008 the android open source project
  3. Licensed under the Apache license, version 2.0 (the "License ");
  4. You may not use this file before t in compliance with the license.
  5. You may obtain a copy of the license
  6. Http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. Distributed under the license is distributed on an "as is" basis,
  9. Without warranties or conditions of any kind, either express or implied.
  10. See the license for the specific language governing permissions and
  11. Limitations under the license.
  12. -->
  13. <Layer-list xmlns: Android = "http://schemas.android.com/apk/res/android">
  14. <Item Android: Id = "@ Android: ID/background">
  15. <Shape>
  16. <Corners Android: radius = "5dip"/>
  17. <Gradient
  18. Android: startcolor = "# ff9d9e9d"
  19. Android: centercolor = "# ff5a5d5a"
  20. Android: centery= "0.75"
  21. Android: endcolor = "# ff747674"
  22. Android: Angular = "270"
  23. />
  24. </Shape>
  25. </Item>
  26. <Item Android: Id = "@ Android: ID/secondaryprogress">
  27. <Clip>
  28. <Shape>
  29. <Corners Android: radius = "5dip"/>
  30. <Gradient
  31. Android: startcolor = "#80ffd300"
  32. Android: centercolor = "#80ffb600"
  33. Android: centery= "0.75"
  34. Android: endcolor = "# a0ffcb00"
  35. Android: Angular = "270"
  36. />
  37. </Shape>
  38. </Clip>
  39. </Item>
  40. <Item Android: Id = "@ Android: ID/progress">
  41. <Clip>
  42. <Shape>
  43. <Corners Android: radius = "5dip"/>
  44. <Gradient
  45. Android: startcolor = "# ffffd300"
  46. Android: centercolor = "# ffffb600"
  47. Android: centery= "0.75"
  48. Android: endcolor = "# ffffcb00"
  49. Android: Angular = "270"
  50. />
  51. </Shape>
  52. </Clip>
  53. </Item>
  54. </Layer-List>
  55. With the source code of these two files, we can draw a gourd Based on the samples.
    First, create the seek_bar.xml file and thumb. xml file in the drawable folder of your project.
    The content of the two files I wrote is as follows: seekbar_img.xml
  56. 1. <? XML version = "1.0" encoding = "UTF-8"?> 2. <layer-list xmlns: Android = "http://schemas.android.com/apk/res/android"> 3. <! -- Background image --> 4. <item Android: Id = "@ + Android: ID/background" Android: drawable = "@ drawable/BG"/> 5. <! -- All energy diagram --> 6. <item Android: Id = "@ + Android: ID/secondaryprogress" 7. android: drawable = "@ drawable/BG"/> 8. <! -- Progress and energy diagram --> 9. <item Android: Id = "@ + Android: ID/progress" Android: drawable = "@ drawable/bg2"/> 10. </layer-List>

    Thumb. xml

    1. <? XML version = "1.0" encoding = "UTF-8"?>
    2. <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
    3. <! -- Press status -->
    4. <Item Android: state_pressed = "true" Android: drawable = "@ drawable/bg3"/>
    5. <! -- Normal no-focus status -->
    6. <Item Android: state_focused = "false" Android: state_pressed = "false"
    7. Android: drawable = "@ drawable/bg4"/>
    8. </Selector>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.