2012-11-19 47 views
1

我需要創建進度條,所以我在onDraw方法中通過擴展ProgressBar來完成此代碼在所有Android設備上工作,除了galaxy nexus ..雖然它沒有引發異常但可繪製的進度不是通過asynctask來更新的。此代碼中的所有設備完全工作,除了Galaxy Nexus的自定義進度條不能在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,我不知道你爲什麼​​onDraw方法,沒有必要這樣做。通過使onDraw方法​​,你只需阻止所有其他線程訪問時onDraw正在執行的對象,並注意onDraw可能會相當頻繁地調用,如果你真的需要同步,使​​塊,如果這樣做就足夠了。

一兩件事,這是真的BAD主意,以創建一個新的PaintTypeface在每次調用onDraw,它擊敗性能。將它們保存爲實例變量並儘可能重用它們,即使是Rect對象也應該被重用。我們回到你的問題,如果你想創建一個自定義的進度條,你可以簡單地在你的佈局定義(XML)中創建一個進度條樣式和引用。 drawable目錄下創建my_progressbar.xml並保存在文件中之後,參考這種風格在你的ProgressBar定義,就像<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> 

看看this有關定義形狀的更多信息。