2011-08-23 3 views
0

나는 이것을 잠시 동안 알아 내려고 노력해 왔습니다 ... 과거에 책갈피에 추가 한 사용자 장소를 표시하기 위해 seekBar 위에 마크를 배치해야합니다. 데이터는 xml에 저장됩니다. 문제는 일반적인 XML 레이아웃에 동적 요소 그리기

여기 내 코드의 ... 그냥 작동하지 않습니다 ... 작은 타원형가 SeekBar를 통해 표시하고있다 :

public class seekMark extends View { 
    private int  seekLength;  // in pixels 
    private int  seekLeftPad; // in pixels 
    private int  seekBottomPad; // in pixels 
    private int  trackLength; // in ms 
    private float pxOverMs;  // in px/ms 
    ShapeDrawable lmark; 
    private seekMark instance; 

    public seekMark(Context context){ 
     super(context); 
     instance = this; 
     seekLength = progressBar.getWidth(); 
     seekLeftPad = progressBar.getPaddingLeft(); 
     seekBottomPad = progressBar.getBottom(); 
     trackLength = player.getDuration(); 
     pxOverMs = pxPerMs(); 
     lmark = new ShapeDrawable(new OvalShape()); 
    } 

    private float pxPerMs(){ 
     return ((float) seekLength)/((float) trackLength); 
    } 

    private int[] markPxList() throws XmlPullParserException, IOException { 

     int bmStartTime = 0; 
     String bmNames[] = bmNameList(xmlPath); 
     int[] bmPos = new int[bmNames.length]; 

     for(int i=0; i < bmNames.length; i++){ 
      bmStartTime = getBookmark(xmlPath, bmNames[i]); 
      bmPos[i] = (int) (bmStartTime * pxOverMs); 
     } 
     return (bmPos); 
    } 

    public void markPlace() throws XmlPullParserException, IOException { 

     int y = seekBottomPad; 
     int x = 0; 
     int bmPos[] = markPxList(); 

     for(int i = 0; i < bmPos.length; i++){ 
      x = bmPos[i] + seekLeftPad; 
      lmark = new ShapeDrawable(); 
      lmark.getPaint().setColor(0xff74AC23); 
      lmark.setBounds(x, y, x + 1, y + 1); 
      instance.invalidate(); 
     } 

    } 
    protected void onDraw(Canvas canvas) { 
     lmark.draw(canvas); 
    } 

} 

그것은이 코드를 사용에서 onCreate에서라고. progressBar의 크기가 onCreate에 아직 설정되지 않은 문제를 피하기 위해 다른 스레드에서이 함수를 호출합니다. 내가 seekMark.markPlace를 (호출 할 때마다

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

    if (display.getRotation() == 1){ // if landscape 

     final Runnable runner = new Runnable() { 
      public void run() { 
       seekMark seekMarks = new seekMark(context); 

       try { 
        seekMarks.markPlace(); 
       } catch (XmlPullParserException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       // runs in another thread to avoid the problem with calling 
       // seekMark directly from onCreate 
      } 
     }; 
     handler.postDelayed(runner, 1000); 

    } 

프로그램

)는 충돌 ... 내 레이아웃 main.xml에의 맨 위에이를 그릴 노력하고있어.

+0

모든 UI 관련 항목은 기본 스레드에서 실행해야합니다. 다른 스레드에서 표시하려고하면 오류가 발생합니다. – Samuel

+0

onClick에서 호출 할 때와 같은 방식으로 충돌이 발생합니다. –

답변

0

이 작업을 수행할지 확실하지 않습니다.

Customize Seekbar

이 접근 방식은 다르지만 비슷한 것 같다.

+0

링크를 제공해 주셔서 감사합니다. 나중에 도로에서 사용 하겠지만 화면에서 타원을 얻는 방법을 알아야합니다. –