2012-11-19 2 views
1

ProgressBar를 확장하여 onDraw 메서드에서 진행 막대를 만들려면이 진행률 표시 줄을 만들어야합니다.이 코드는 throw 및 예외는 아니지만 모든 Galaxy 넥서스 장치에서 작동합니다. 진행 드로어 블은 asynctask의 의미로 업데이트되지 않습니다. 이 코드는 완전히 문제는 당김과 스타일있을 수 있습니다 갤럭시 넥서스사용자 정의 진행률 표시 줄이 Galaxy Nexus 장치에서만 작동하지 않습니다.

@Override 
protected synchronized void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Paint textPaint = new Paint(); 
    textPaint.setAntiAlias(true); 
    textPaint.setColor(textColor); 

    Typeface tmTypeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD); 
    textPaint.setTypeface(tmTypeface); 
    textPaint.setTextSize(textSize * mContext.getResources().getDisplayMetrics().density); 
    Rect bounds = new Rect(); 
    textPaint.getTextBounds(text, 0, text.length(), bounds); 
    int x = getWidth()/2 - bounds.centerX(); 
    int y = getHeight()/2 - bounds.centerY(); 
    canvas.drawBitmap(thumbnailBitmap, 10, y - bitmapHeight/2, null); 
    canvas.drawText(text, 15 + thumbnailBitmap.getWidth(), y, textPaint); 
    canvas.drawBitmap(downloadBitmap, getWidth() - bitmapWidth, y - bitmapHeight/2, null); 

} 

를 제외한 모든 장치에서 작동하고 있지만 모든 버전에서 작동하고 모든 장치가

답변

0

나는 당신이 상태 문제를 볼 수 없습니다 이 코드 조각에 귀하의 질문을,하지만 난 당신의 코드 자체에 문제가 많이 볼 수 있습니다.

View을 서브 클래 싱하는 것 같습니다. synchronizedonDraw 방법을 사용하는 이유가 무엇인지 알 수 없으므로 그렇게 할 필요가 없습니다. 이렇게하면 충분한 경우 onDraw 방법 synchronized을함으로써, 당신은 단순히 당신이 정말로 동기화를 필요로한다면, 작은synchronized 블록을, onDraw가 실행 개체에 액세스하는 모든 다른 스레드를 차단하고, 그 onDraw주의 꽤 자주 호출 할 수 있습니다 .

한가지 더, 성능을 패배, 정말 onDraw에 호출 할 때마다 새로운 Paint, Typeface을 만들 수있는 정말 BAD 생각이다. 인스턴스 변수로 저장하고 가능하면 최대한 재사용하십시오. 심지어 Rect 객체를 다시 사용해야합니다.

문제로 돌아가서 사용자 정의 진행률 표시 줄을 만들려면 레이아웃 정의 (XML)에서 간단히 진행 막대 스타일과 참조를 만들면됩니다. drawable 디렉토리에 my_progressbar.xml를 만들고 파일에 다음을 저장, 정의 모양에 대한 추가 정보를 원하시면 this를 살펴 보자 <ProgressBar ... android:progressDrawable="@drawable/my_progressbar"/>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:id="@android:id/background"> 
    <shape android:shape="rectangle" > 
     <solid android:color="#ffcccccc" /> 
    </shape> 
</item> 

<item android:id="@android:id/secondaryProgress"> 
    <clip> 
    <shape android:shape="rectangle" > 
     <solid android:color="#ff000000" /> 
    </shape> 
    </clip> 
</item> 

<item android:id="@android:id/progress"> 
    <clip> 
    <shape android:shape="rectangle" > 
     <solid android:color="#ffff6100" /> 
    </shape> 
    </clip> 
</item> 

처럼 ProgressBar 정의에이 스타일을 참조합니다.

관련 문제