[英]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
每次调用中创建一个新的Paint
和Typeface
确实是一个糟糕的主意,这会降低性能。 将它们保存为实例变量,并尽可能地重用它们,即使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.