2013-09-02 13 views
7

sto creando un SeekBar verticale utilizzando la seguente classeSet pollice attenzione e drawable premuto sul SeekBar personalizzato a livello di codice

public class VerticalSeekBar extends SeekBar { 

    public VerticalSeekBar(Context context) { 
     super(context); 
    } 

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

    public VerticalSeekBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(h, w, oldh, oldw); 
    } 

    @Override 
    protected synchronized void onMeasure(int widthMeasureSpec, 
      int heightMeasureSpec) { 
     super.onMeasure(heightMeasureSpec, widthMeasureSpec); 
     setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 
    } 

    @Override 
    protected void onDraw(Canvas c) { 
     c.rotate(-90); 
     c.translate(-getHeight(), 0); 
     super.onDraw(c); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (!isEnabled()) { 
      return false; 
     } 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     case MotionEvent.ACTION_MOVE: 
     case MotionEvent.ACTION_UP: 
      setProgress(getMax() 
        - (int) (getMax() * event.getY()/getHeight())); 
      onSizeChanged(getWidth(), getHeight(), 0, 0); 
      break; 

     case MotionEvent.ACTION_CANCEL: 
      break; 
     } 
     return true; 
    } 
} 

e quindi creare la SeekBar nell'attività usando

VerticalSeekBar myZoomBar = new VerticalSeekBar(this); 
Drawable drawable = getResources().getDrawable(R.drawable.green_bar); 
ClipDrawable clip = new ClipDrawable(drawable, Gravity.LEFT,ClipDrawable.HORIZONTAL); 
Drawable drawable2 = getResources().getDrawable(R.drawable.white_bar); 
InsetDrawable d1 = new InsetDrawable(drawable2, 5, 5, 5, 5); 
myZoomBar.setThumb(getResources().getDrawable(R.drawable.whitecircle)); 
LayerDrawable mylayer = new LayerDrawable(new Drawable[] { d1, clip }); 
myZoomBar.setProgressDrawable(mylayer); 
myZoomBar.setMax(100); 
myZoomBar.setProgress(50); 
LinearLayout.LayoutParams zoomBarParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); 
zoomBarParams.gravity = Gravity.CENTER_HORIZONTAL; 
LinearLayout zoomLayout = new LinearLayout(this); 
zoomLayout.addView(myZoomBar, zoomBarParams); 
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,Gravity.CENTER); 
addContentView(zoomLayout, frameLayoutParams); 

La domanda è come faccio a impostare il pollice premuto e focalizzato drawable dal codice?

+0

Ho visto la modifica nella mia risposta. Quando l'avrei accettato, qualcun altro l'ha rifiutato. A proposito, dal momento che stiamo cambiando il pollice estraibile con pressed_state su ACTION_DOWN, non dovremmo rimanere così finché non si incontra ACTION_UP? Se ACTION_MOVE viene rilevato, sarà solo dopo che ACTION_DOWN è già stato chiamato (e il pollice è stato impostato). Sto assumendo il flusso degli eventi mentre il trascinamento è sempre: ACTION_DOWN >> ACTION_MOVE >> ACTION_UP. Ho sbagliato qui? – Vikram

+0

Sì, ma sto usando una classe personalizzata per disegnare una barra di ricerca verticale come puoi vedere sopra, quindi ho dovuto includerla :) –

risposta

4

Aggiunta di un OnTouchListener a myZoomBar dovrebbe risolvere il tuo problema. Impostare il suo comportamento in questo modo:

myZoomBar.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch(event.getAction()) { 

     case MotionEvent.ACTION_DOWN: 
      // Pressed state 
      myZoomBar.setThumb(getResources().getDrawable(
          R.drawable.pressed_state)); 
      break; 

     case MotionEvent.ACTION_UP: 
      myZoomBar.setThumb(getResources().getDrawable(
          R.drawable.)); 
      break; 
     } 

     return false; 
    } 
}); 

È possibile utilizzare un OnFocusChangeListener per gestire il cambiamento a fuoco, ma se tutto ciò che serve è quello di cambiare il pollice quando si preme una posizione sul SeekBar, OnTouchListener sarà sufficiente.

Il selettore di stato drawable potrebbe non funzionare correttamente in questo caso. Perché, l'utente potrebbe voler saltare a una posizione facendo clic su di esso (anziché scorrere lì). E non penso che il pollice possa gestire i cambiamenti di stato. Ho provato a utilizzare StateListDrawable per creare un drawable con stati, ma il suo utilizzo con myZoomBar.setThumb(stateDrawable) non ha funzionato.

1

Crea Drawable dalle risorse oa livello di programmazione e dopo, quando hai fatto esso uso

setThumb(your_drawable); 

Non so se v'è possibile creare drawable con gli stati (mirato e selezionato) a livello di codice, ma da xml:

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

e

getResources().getDrawable(R.drawable.your_drawable_id); 
+0

Ho già provato a farlo in questo modo, ma senza successo. –

+0

Ecco qualcosa di interessante [collegamento] (http://stackoverflow.com/questions/16163215/android-styling-seek-bar) –

Problemi correlati