Android custom Progress values can be dragged by Seekbar

Source: Internet
Author: User

Recently busy looking for an internship, plus the lab is pushing new projects, need to learn new knowledge. So for a long time did not go to tidy up the official blog, GitHub also quite long not updated, very ashamed. The next step is to keep writing. Today, simply write down the custom Seekbar blog I used in the project, the requirements are, seekbar need to show the leftmost and most right values, and the progress will be moved with the progress block. It's clear to see.

In fact, the implementation is very simple, mainly thinking. Custom control words are not difficult, before my blog also has a special introduction, here will not say more.

Implementation scenarios

Here is the quickest way to customize the control by inheriting Seekbar. The main difficulty lies in the progress of the display, in fact, I am the most stupid method, is to use a Popwindow display above the progress bar, and then move the slider in real time to change the horizontal axis of its display. Look at the core code of the progress display:

private void Inithintpopup () {
String popuptext = null;

    if (mProgressChangeListener!=null){        popupText = mProgressChangeListener.onHintTextChanged(this, cuclaProcess(leftText));    }    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);    final View undoView = inflater.inflate(R.layout.popup, null);    mPopupTextView = (TextView)undoView.findViewById(R.id.text);    mPopupTextView.setText(popupText!=null? popupText : String.valueOf(cuclaProcess(leftText)));   // mPopup.dismiss();    if(mPopup == null)        mPopup = new PopupWindow(undoView, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, false);    else{        mPopup.dismiss();        mPopup = new PopupWindow(undoView, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, false);    }}

The layout is simple, just a textview.

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" wrap_content " android:layout_height  =" wrap_content " android:orientation  =" horizontal " android:background  = "#0fff"  android:gravity  =;     <TextView android:id="@+id/text"android:padding="8DP"  Android:textsize="16sp"android:singleline="true"android:ellipsize= "End" Android:textcolor= "@color/green"android:layout_width="Wrap_content"  android:layout_height="wrap_content"/>                                                        </linearlayout>

The value of the display is the same as the principle, see the following code:

private void Initrighttext () {Layoutinflater inflater = (layoutinflater) getcontext (). Getsystemservice(Context. LAYOUT_inflater_service);Final View Undoview = Inflater. Inflate(R. Layout. Rightpop, null);Mpopuprightview = (TextView) undoview. Findviewbyid(R. ID. Righttext);Mpopuprightview. SetText(righttext+"");Mrightpopup = new Popupwindow (Undoview, Mpopupwidth, ViewGroup. Layoutparams. WRAP_content, False);Mrightpopup. Setanimationstyle(R. Style. Fade_animation);}

So how do you let the text above the slider slide. Just rewrite the onprogresschanged to do it.

  Public void onprogresschanged(SeekBar SeekBar,intProgressBooleanb) {String Popuptext =NULL;if(mprogresschangelistener!=NULL) {Popuptext = mprogresschangelistener.onhinttextchanged ( This, Cuclaprocess (Lefttext)); }if(Mexternallistener! =NULL) {mexternallistener.onprogresschanged (SeekBar, progress, b);        } step = Cuclaprocess (Lefttext); Mpopuptextview.settext (popuptext!=NULL? PopupText:String.valueOf (step));if(Mpopupstyle==popup_follow) {Mpopup.update (int) ( This. GetX () + (int) getxposition (SeekBar)), (int) ( This. GetY () +2*mylocationoffset+ This. GetHeight ()),-1, -1); }    }

In fact, the most important thing is to figure out the position of X getxposition. Look at the above code:

 private   float  getxposition  (SeekBar SeekBar) {float
      val = (((float ) seekbar.getprogress () * (float        ) (Seekbar.getwidth ()-2  * Seekbar.getthumboffset ()))/Seekbar.getmax ());        float  offset = seekbar.getthumboffset () *2 ;        int  textWidth = mpopupwidth;        float  textcenter = (textwidth/2.0  f);        float  newx = val+offset-textcenter;    return  newx; }

The

calculates the distance to the X movement by getprogress the progress. This enables the movement of text. Finally, the source code will be downloaded.
How to use it, as with normal custom controls, as follows:

<com.canmou.cm4restaurant.tools.SeekBarHint        android:id="@+id/seekbar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:layout_marginTop="40dp"        android:progress="5"        hint:popupWidth="40dp"        hint:yOffset="10dp"        hint:popupStyle="fixed"/>

Of course, the native style is now implemented, and here's how to customize the Seekbar style.

Custom styles

Seekbar to change the style to prepare three pictures, the left has selected the slider picture, the right not selected slider picture and slider picture, slider to 9.png format the best. For convenience, the layer-list is used to handle the slide section directly. Define the XML file in drawable.

<?xml version= "1.0" encoding= "Utf-8"?>    <layer-list xmlns:android="Http://schemas.android.com/apk/res/android" >   <item android:id="@android: Id/background"><shape >          <corners Android:radius="10dip" />          <gradientandroid:angle="android:centercolor" = "#F5F5F5" android:centery="0.2"android:endcolor="#F5F5F5"Android:startcolor ="#F5F5F5" />                                                                            </shape>  </Item>  <item android:id="@android: id/progress"><clip >          <shape >              <corners Android:radius="10dip" />              <gradientandroid:angle="android:centercolor" = "#39ac69" android:centery="0.45"android:endcolor="#39ac69"Android:startcolor ="#39ac69" />                                                                                                    </shape>      </clip>  </Item></layer-list>

This allows overlapping images to be implemented. The picture that sets the slider is set directly in the Seekhint:

android:thumb="@drawable/bt_seekbar"

The Seekbar of this progress value can be dragged into the implementation.
SOURCE download

Android custom Progress values can be dragged by Seekbar

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.