2012-10-31 7 views
7

私はシークバーを作る必要があります。そして今、私は親指とバーのサイズを変更するには問題があります。バーが親指と同じか大きい場合はすべてがうまくいきますが、親指が親指より大きい場合は親指全体が正しい大きさで表示されません。ここでシークバーのサイズをプログラムで変更するが、サムがバーよりも大きくなることはない

は親指を作成するために私のコードです:

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にバーを作成するために私のコード:私はの高さを設定するのはここ

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); 

    } 

は、一緒にすべてを置くために私のコードですシークバー:

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);      

    } 

私がバーを膨らませるXMLコード:

<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" 
    /> 

シークバーのサイズを変更するには、ParentLayout.addView(seekbar、params)を使用します。
ビューを追加するときに、私がバーに必要な高さを使用すると、サムがカットされます。親指の高さを使用すると、バーは親指と同じ高さになります。 Androidは、ビューを追加するために使用するサイズにバーのサイズを変更します。
バーであるGradientDrawableのサイズをs hape.setSize(width, height)に設定しようとしましたが、shape.setBounds(0, 0, width, height)も試してみました。正しいサイズの別のイメージを作成して、LayerDrawable mylayerに追加しようとしました。しかし何も働かなかった。

XMLを使用してこれを作成するときは、正しいサイズにバーを取得するためにpaddingToppaddingBottomを使用することができます。しかし、これをGradientDrawableにプログラムでどのように行うかはわかりません。

答えて

15

よく、あまりにも悪い回答はありませんでしたが、自分で質問に答えることができました。答えは、InsetDrawableを使用することです。バーのドロアブルの1つをInsetDrawableに配置する必要があります。コードザッツ

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); 

    } 

私は、これは同じ問題で他の誰かを助けることができると思います。

+0

良い仕事nありがとう –

関連する問題