簡體   English   中英

自定義進度欄僅在Galaxy Nexus設備上不起作用

[英]Custom progress bar not working in only Galaxy Nexus device

我需要創建進度條,所以我通過擴展ProgressBar在onDraw方法中做到了這一點,並且此代碼在除galaxy nexus ..以外的所有android設備中都有效,盡管它不會拋出異常,但是可繪制進度不會通過asynctask進行更新。 該代碼可在除星系關系之外的所有設備中完全正常工作

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

}

問題可能與可繪制和樣式有關,但在所有版本和所有設備中均有效

在此代碼段中,我看不到您在問題中陳述的問題,但是在您的代碼本身中,我可以看到很多問題。

我猜您是在繼承View的子類,我不知道您為什么synchronized onDraw方法,沒有必要這樣做。 通過使onDraw方法synchronized ,您只需在執行onDraw時阻止所有其他線程訪問您的對象,並注意onDraw可能會被頻繁調用,如果您確實需要同步,則這樣做就可以做成一個小的 synchronized塊。

還有一件事,在onDraw每次調用中創建一個新的PaintTypeface確實是一個糟糕的主意,這會降低性能。 將它們保存為實例變量,並盡可能地重用它們,即使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>

查看此內容以獲取有關定義形狀的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM