2013-07-14 4 views
5

シークバーをカスタマイズする必要があります。あらかじめ決められた時間(30秒)でシークバーにドットを付ける必要があります。 この期間はそれぞれ、すべてのビデオのために変わるので、どのように私はいくつかの可能性、特定の二アンドロイドシークバーのカスタマイズ、

+0

カスタムビューを作成しようとしましたか? – sandrstar

+0

シークバーの@sandrstarカスタムビュー?シークバーを拡張するクラス?これらの点を配置​​する方法についてはあまり知られていません。どんな助けもありがとう、ありがとう – user2558496

答えて

16

Here'reでシークバー上のドットポイントを置く:

  1. 入れ「ドット」ビューだけSeekBar上記1。それは簡単なアンドロイドレイアウトのタスクであるため、ここで詳細に説明しません。
  2. は(this good explanation about custom viewsを参照)、以下のような、SeekBarを拡張:

    /** 
    * Seek bar with dots on it on specific time/percent 
    */ 
    public class DottedSeekBar extends SeekBar { 
    
        /** Int values which corresponds to dots */ 
        private int[] mDotsPositions = null; 
        /** Drawable for dot */ 
        private Bitmap mDotBitmap = null; 
    
        public DottedSeekBar(final Context context) { 
         super(context); 
         init(null); 
        } 
    
        public DottedSeekBar(final Context context, final AttributeSet attrs) { 
         super(context, attrs); 
         init(attrs); 
        } 
    
        public DottedSeekBar(final Context context, final AttributeSet attrs, final int defStyle) { 
         super(context, attrs, defStyle); 
         init(attrs); 
        } 
    
        /** 
        * Initializes Seek bar extended attributes from xml 
        * 
        * @param attributeSet {@link AttributeSet} 
        */ 
        private void init(final AttributeSet attributeSet) { 
         final TypedArray attrsArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.DottedSeekBar, 0, 0); 
    
         final int dotsArrayResource = attrsArray.getResourceId(R.styleable.DottedSeekBar_dots_positions, 0); 
    
         if (0 != dotsArrayResource) { 
          mDotsPositions = getResources().getIntArray(dotsArrayResource); 
         } 
    
         final int dotDrawableId = attrsArray.getResourceId(R.styleable.DottedSeekBar_dots_drawable, 0); 
    
         if (0 != dotDrawableId) { 
          mDotBitmap = BitmapFactory.decodeResource(getResources(), dotDrawableId); 
         } 
        } 
    
        /** 
        * @param dots to be displayed on this SeekBar 
        */ 
        public void setDots(final int[] dots) { 
         mDotsPositions = dots; 
         invalidate(); 
        } 
    
        /** 
        * @param dotsResource resource id to be used for dots drawing 
        */ 
        public void setDotsDrawable(final int dotsResource) { 
         mDotBitmap = BitmapFactory.decodeResource(getResources(), dotsResource); 
         invalidate(); 
        } 
    
        @Override 
        protected synchronized void onDraw(final Canvas canvas) { 
         super.onDraw(canvas); 
    
         final int width = getMeasuredWidth(); 
         final int step = width/getMax(); 
    
         if (null != mDotsPositions && 0 != mDotsPositions.length && null != mDotBitmap) { 
          // draw dots if we have ones 
          for (int position : mDotsPositions) { 
           canvas.drawBitmap(mDotBitmap, position * step, 0, null); 
          } 
         } 
        } 
    } 
    

    res/values/attrs.xmlのカスタムattrsに忘れてはいけない:

    <resources> 
        <declare-styleable name="DottedSeekBar"> 
         <attr name="dots_positions" format="reference"/> 
         <attr name="dots_drawable" format="reference"/> 
        </declare-styleable> 
    </resources> 
    

    そして、次のコードを使用して:

    setContentView(R.layout.main); 
    
    final DottedSeekBar bar = (DottedSeekBar) findViewById(R.id.seekBar); 
    
    bar.setDots(new int[] {25, 50, 75}); 
    bar.setDotsDrawable(R.drawable.dot); 
    

    、main.xmlレイアウト:

    <LinearLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:custom="http://schemas.android.com/apk/res/com.example.TestApp" 
         android:orientation="vertical" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
    
        <com.example.TestApp.DottedSeekBar 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/seekBar" /> 
    </LinearLayout> 
    

    または単に1つのmain.xml:

    <LinearLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:custom="http://schemas.android.com/apk/res/com.example.TestApp" 
         android:orientation="vertical" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
    
        <com.example.TestApp.DottedSeekBar 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/seekBar" 
         custom:dots_positions="@array/dots" 
         custom:dots_drawable="@drawable/dot" /> 
    </LinearLayout> 
    

    次の画像取得できます。より多くのアイデアのためにthis exampleを参照してください enter image description here

  3. を。

    特定の「時間」にドットを置くことについて

SeekBarは時間に関するものではありませんので、任意の時間に関連するロジックを提供するのはあなた次第です。

関連する問題