Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义进度条仅在Galaxy Nexus设备中不起作用_Android_Progress Bar_Custom Component - Fatal编程技术网

Android 自定义进度条仅在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

我需要创建进度条,所以我在onDraw方法中扩展了进度条,这段代码在除galaxy nexus之外的所有android设备上都能工作。。虽然它没有抛出异常,但是进程drawable没有通过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);

}

问题可能出在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>