Android 自定义进度条仅在Galaxy Nexus设备中不起作用
我需要创建进度条,所以我在onDraw方法中扩展了进度条,这段代码在除galaxy nexus之外的所有android设备上都能工作。。虽然它没有抛出异常,但是进程drawable没有通过asynctask进行更新。此代码在除galaxy nexus之外的所有设备中都完全有效Android 自定义进度条仅在Galaxy Nexus设备中不起作用,android,progress-bar,custom-component,Android,Progress Bar,Custom Component,我需要创建进度条,所以我在onDraw方法中扩展了进度条,这段代码在除galaxy nexus之外的所有android设备上都能工作。。虽然它没有抛出异常,但是进程drawable没有通过asynctask进行更新。此代码在除galaxy nexus之外的所有设备中都完全有效 @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); Paint textPaint = n
@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);
}
问题可能出在drawable和style上,但它在所有版本和所有设备上都能正常工作我看不出您在这段代码片段的问题中指出的问题,但我可以看到您的代码本身存在很多问题 我猜您是在子类化
视图
,我不知道为什么要同步onDraw
方法,没有必要这样做。通过使onDraw
方法synchronized
,您只需在执行onDraw
时阻止所有其他线程访问您的对象,并且请注意,如果您确实需要同步,可以非常频繁地调用onDraw
,如果这样做足够的话,制作一个小的同步的
块
还有一件事,创建一个新的绘图,字体的想法真的很糟糕,每次调用onDraw
,都会影响性能。将它们保存为实例变量,并尽可能地重用它们,即使Rect
对象也应该重用
让我们回到您的问题,如果您想要创建一个定制的progressbar,您只需在布局定义(XML)中创建一个progressbar样式和引用即可。在drawable
目录下创建my_progressbar.xml
,并将以下内容保存在文件中,在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>