2012-10-31 8 views
7

Devo fare una barra di ricerca in cui posso cambiare quasi tutto a livello di programmazione. E ora ho un problema a cambiare il pollice e la misura della barra. Tutto funziona alla grande se la barra è più grande o ha le stesse dimensioni del pollice, ma se il pollice è più grande della barra, non riesco a farlo mostrare il pollice intero con la misura corretta della barra.Cambiando la dimensione della barra di ricerca a livello di codice ma non riesco a ottenere il pollice per essere più grande della barra

Ecco il mio codice per creare il pollice:

public void setSeekBarThumb(int width, int height, int rColor, int gColor, int bColor){ 

     ShapeDrawable thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 


     StateListDrawable states = new StateListDrawable(); 
     states.addState(new int[] {android.R.attr.state_pressed},thumb );//add to the state list with the state pressed 

     thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 

     states.addState(new int[] { },thumb);//add to the state list with no state 

     seekbar.setThumb(states); 
     seekbar.setThumbOffset(0); 
    } 

heres il mio codice per creare la barra:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape.setCornerRadius(50); 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 
     ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape.setCornerRadius(50);//change the corners of the rectangle 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 

     LayerDrawable mylayer= new LayerDrawable(new Drawable[]{shape, clip,}); 

     seekbar.setProgressDrawable(mylayer); 
     seekbar.setProgress(50); 

    } 

Ecco il mio codice di mettere tutto insieme, dove ho impostato l'altezza del barra di ricerca:

public MySeekBar(Activity activity, AbsoluteLayout ParentLayout, SeekBarParameters parameters) 
    {   
     super(activity.getApplicationContext()); 



     seekbar =(SeekBar)activity.getLayoutInflater().inflate(R.layout.horizontal_seek_bar, ParentLayout,false); 
     seekbar.setMax(100);  
     setSeekBarThumb(parameters.widthThumb, parameters.heightThumb,0,100,0); 
     setSeekBarProgress(parameters.width, parameters.height,255,69,0); 

     int drawHeight; 
     if(parameters.height>parameters.heightThumb) drawHeight=parameters.height; 
     else drawHeight=parameters.heightThumb; 

     AbsoluteLayout.LayoutParams params = new AsoluteLayout.LayoutParams(parameters.width,drawHeight,parameters.xPosition, parameters.yPosition); 
     ParentLayout.addView(seekbar, params);      

    } 

il codice XML in cui ho gonfiare il bar dalle:

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:max="100" 
     android:progress="0" 
     android:secondaryProgress="0" 
     android:maxHeight="10000dip" 
     android:id="@+id/slider" 
    /> 

Per modificare la dimensione della barra di ricerca, utilizzo ParentLayout.addView (barra di ricerca, parametri).
Quando si aggiunge la vista, se si utilizza l'altezza che desidero sulla barra, il pollice viene tagliato. Se utilizzo l'altezza del pollice, la barra raggiunge la stessa altezza del pollice. Android ridimensiona la barra alla dimensione che uso per aggiungere la vista.
Ho provato ad impostare la dimensione del GradientDrawable che è la barra con s hape.setSize(width, height), ho anche provato shape.setBounds(0, 0, width, height), e ho provato a creare un'altra immagine con le giuste dimensioni e aggiungendo al LayerDrawable mylayer. Ma niente ha funzionato.

Quando si crea questo tramite XML è possibile utilizzare paddingTop e paddingBottom per ottenere la barra delle dimensioni corrette. Ma non so come farlo a GradientDrawable a livello di programmazione.

risposta

15

beh, peccato non ci sono state risposte ma sono stato in grado di rispondere alla domanda da solo. La risposta è usare InsetDrawable. Devi mettere quello dei drawable della barra in un oggetto InsetDrawable.

questo è il codice:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape2.setCornerRadius(50);      
     ClipDrawable clip = new ClipDrawable(shape2, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     GradientDrawable shape1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape1.setCornerRadius(50);//change the corners of the rectangle  
     InsetDrawable d1= new InsetDrawable(shape1,5,5,5,5);//the padding u want to use   


     LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip}); 


     seekbar.setProgressDrawable(mylayer); 

     seekbar.setProgress(50); 

    } 

Spero che questo possa aiutare qualcun altro con lo stesso problema.

+0

ottimo lavoro n Grazie –

Problemi correlati