0

How could I move image when scrolling seekbar? I want to move this indicator image when changing seekbar thumb position.

Here is image.

enter image description here

gprathour
  • 14,813
  • 5
  • 66
  • 90
fish40
  • 5,738
  • 17
  • 50
  • 69
  • can you share the code for this, i have a similar issue where i want to move an image absed on seekbar value – usr30911 Aug 11 '14 at 13:16

2 Answers2

7

Some adjustments may need to be made depending upon your choice of image and/or its alignment with the seekbar. I am sure someone else here will come up with a better approach, but here's my take on it:

// Declare global variables
SeekBar sb;
LinearLayout.LayoutParams params;
Point p;
ImageView iv;

// Initialize the widgets
sb = (SeekBar) findViewById(....);

iv = (ImageView) findViewById(....);

// Since I added SeekBar and ImageView to a LinearLayout.
// Use LayoutParams for whichever container you use.
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

// Starting position
params.leftMargin = 0;

// Decide the value based on where you wish to place the image w.r.t the SeekBar
params.topMargin = ...;

iv.setLayoutParams(params);

// You will use this to get the width of the screen
p = new Point();

getWindowManager().getDefaultDisplay().getSize(p);


sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {        
        // ((float)progress * p.x) / 100): "p.x" holds the screen's width.
        //                                 The expression computes the percentage of
        //                                 screen width given the "progress" value

        // (progress * 0.5): This is being used to offset image position.
        //                   I am using the offset because it seemed that the image 
        //                   was leading the SeekBar at higher progress values. 
        //                   You can try different values to see which one works best.
        int measure = (int)((((float)progress * p.x) / 100) - (progress * 0.5));

        // When "measure" will become equal to "p.x"(at progress = 100),
        // the image will be outside the view when we set its "leftMargin".
        // But, the image will start disappearing before that.
        // When this situation comes, set the "leftMargin" to a maximum value
        // which is the screen width - ImageView' width
        if (p.x - measure < iv.getWidth()) {
            params.leftMargin = p.x - iv.getWidth();
        } else {
            params.leftMargin = measure ;
        }

        // Set the "topMargin" to the value you decided upon before  
        params.topMargin = ...;

       // Set LayoutParams
       iv.setLayoutParams(params);
    }
});

Edit (for user Shripal's comment) -- above the SeekBar (Y-axis):

layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp" >   <!-- adjust this in java code -->

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/some_drawable" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="50" />

    </RelativeLayout>

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

</LinearLayout>
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • hey can you post your whole code if not then plz post your xml file...with only this i m getting confused and not getting any output..plz rply me soon.thanks – shripal Oct 21 '13 at 11:24
  • @shripal I wrote this code from memory. So, there isn't an xml file. If you're confused about a specific thing, please go ahead and ask. I'll try to explain the best I can. As far as the xml goes, it would be a `LinearLayout(vertical)` container holding a `SeekBar` and an `ImageView`. If you're using another container, say `RelativeLayout`, you would use `RelativeLayout.LayoutParams`. – Vikram Oct 22 '13 at 08:31
  • thanks for your reply....how can i draw an image on the top of thumb. in that image i want to write progress value. and that image and value should be stay upon the thumb while sliding. can you guide me how can i achieve this – shripal Oct 22 '13 at 08:40
  • @shripal For this, you can look through the answers on this page: [How add TextView in middle of SeekBar thumb?](http://stackoverflow.com/questions/9272384/how-add-textview-in-middle-of-seekbar-thumb). – Vikram Oct 22 '13 at 09:05
  • this way i have done...but i want to show that image and progress value on the top of thumb. – shripal Oct 22 '13 at 09:13
  • @shripal For me, `on top of` means `on the thumb` -- in Z-axis. If you want to show the image and progress `above the thumb` -- Y-axis -- then see the edit I have added to my answer. For ordering in Z-axis you'll use the link I posted in my previous comment.. – Vikram Oct 22 '13 at 19:14
0

Here is the xml code

 <SeekBar
                        android:id="@+id/seek_bar"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:thumb="@drawable/seek_thumb_icon"
                        android:progressDrawable="@drawable/progress"/>

Heres the java code

@Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        mSeekBarProgressText.setText(String.valueOf(i) + " %");
        mParcentValue = i;
        setPercent(mDiscountPercentText , "-" + i , getResources().getDimension(R.dimen.discount_percent_text));
        if (i > 0){
            findMGTextView(R.id.card_without_discount).setVisibility(View.GONE);
        } else {
            findMGTextView(R.id.card_without_discount).setVisibility(View.VISIBLE);
        }
        int measure = (int)((((float)i * point.x) / 100) - (i * 0.5));

        if (i < 50){
            mSeekBarProgressText.setBackgroundResource(R.drawable.cucich1);
            params.leftMargin = measure ;
        } else {
            mSeekBarProgressText.setBackgroundResource(R.drawable.cucich_reverse1);
            params.leftMargin = measure - (mSeekBarProgressText.getWidth() + 20);
        }
        params.topMargin = 0;

        mSeekBarProgressText.setLayoutParams(params);
        findMGTextView(R.id.card_without_discount1).setLayoutParams(params);
    }
fish40
  • 5,738
  • 17
  • 50
  • 69