首页 \ 问答 \ 以编程方式创建可绘制的进度(Create a progress drawable programmatically)

以编程方式创建可绘制的进度(Create a progress drawable programmatically)

我有一个场景,我需要有大量的进度条可绘制。 我不能创建所有这些XML资源,因为我想让用户选择一种颜色,然后将用于动态创建绘图。 下面是XML中的一个这样的drawable,我如何以编程方式创建这个精确的drawable?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <solid android:color="@color/transparent" />
        <stroke android:width="2px" android:color="@color/category_blue_stroke"/>
    </shape>
</item>


<item android:id="@android:id/progress">
<clip>
    <shape>
        <solid android:color="@color/category_blue" />
        <stroke android:width="2px" android:color="@color/category_blue_stroke"/>
    </shape>
</clip>
</item>

</layer-list>

I have a scenario where i need to have a large number of progress bar drawables. I cant create xml resources for all of them because i want the user to choose a color that will then be used to dynamically create the drawable. Below is one such drawable in xml, how can i create this exact drawable programmatically?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <solid android:color="@color/transparent" />
        <stroke android:width="2px" android:color="@color/category_blue_stroke"/>
    </shape>
</item>


<item android:id="@android:id/progress">
<clip>
    <shape>
        <solid android:color="@color/category_blue" />
        <stroke android:width="2px" android:color="@color/category_blue_stroke"/>
    </shape>
</clip>
</item>

</layer-list>

原文:https://stackoverflow.com/questions/18011075
更新时间:2022-07-24 20:07

最满意答案

你的ProgressDialog不应该是intendminate。 您应该使用false而不是true调用setIndeterminate

pd.setIndeterminate(false);

你应该使用setProgress而不是incrementProgressBy


your ProgressDialog should not be intederminate. You should call setIndeterminate with false instead of true

pd.setIndeterminate(false);

and you should use setProgress instead of incrementProgressBy

相关问答

更多
  • /// try :) progressgialog.post(new Runnable(){ run () { progressgialog.setMessage("asd"); } }) //// catch :) activity.runOnUiThread(new Runnable() { progressgialog.setMessage("asd"); }); I found a work-around and thought I would share it. Since I was m ...
  • 首先,如果你可以替换它会更有意义: pro_dialog.incrementProgressBy(2); 有了这个: pro_dialog.setProgress(progress); 当前代码在进度变量达到MAX_PROGRESS后解除对话框,但进度对话框不显示此变量的当前值。 如果您想显示AlertDialog,可以在“pro_dialog.dismiss()”之后执行此操作。 案例进展对话框样式已在此处讨论过 。 First of all, it would make more sense if ...
  • 你的ProgressDialog不应该是intendminate。 您应该使用false而不是true调用setIndeterminate pd.setIndeterminate(false); 你应该使用setProgress而不是incrementProgressBy your ProgressDialog should not be intederminate. You should call setIndeterminate with false instead of true pd.setInd ...
  • 第二个示例可能运行得更快,但它独占GUI线程。 使用AsyncTask的第一种方法更好; 它允许GUI在下载过程中保持响应。 我发现将AsyncTask与SwingWorker进行比较很有帮助,如本例所示。 The second example may run faster, but it monopolizes the GUI thread. The first approach, using AsyncTask, is better; it allows the GUI to stay responsi ...
  • 您可以使用onProgressUpdated()来更改消息,因为此方法在UI线程上运行。 但是,它不会让您有机会在特定的时间内更改消息。 You can use onProgressUpdated() in order to change the message, because this method is run on the UI Thread. Though, it doesn't give you the opportunity to change the message on the parti ...
  • 你不必做任何特别棘手或不寻常的事情。 只需使用DialogBox()创建模式对话框即可。 在对话框过程的WM_INITDIALOG处理程序中,创建后台线程以加载文件。 随着加载的进行,将PBM_SETPOS消息发送到进度条控件以进行更新。 加载完成后,调用EndDialog()关闭对话框。 但是,必须在对话框过程中EndDialog() 。 所以要做到这一点,你需要发送一个虚拟消息(例如WM_APP ): DialogBox(..., DlgProc); // File loading is done an ...
  • Bala您创建的自定义布局作为对话框插入水平progressBar的代码,其中水平SCrollBar的颜色为绿色, 并带有自定义颜色。 Bala the custom layout which you have created as a dialog insert the code for the horizontal progressBar having color green from Horizontal SCrollBar with Custom Color.
  • 您可能想看看ProgressMonitor 。 如果操作需要很长时间,它会自动弹出一个带进度条的对话框; 请参阅如何使用进度监视器 。 You might want to look into ProgressMonitor. It automatically pops up a dialog with a progress bar if the operation is taking a long time; see How to Use Progress Monitors.
  • 调用publishProgress以在后台计算仍在运行时在UI线程上发布更新。 您可以在特定时间间隔内调用它来发布更新,以便通知用户正在运行的任务。 典型案例如下: protected Void doInBackground(String.. params ) { publishProgress("sentence 1"); //do some time consuming task //.. publishProgress("sentence 2"); ...
  • 覆盖AsyncTask为您提供的onProgressUpdate方法,不要创建自己的方法。 另外,不要自己调用onProgressUpdate ! 这是一个回调方法,系统使用该方法为您提供UI线程的回调。 如果直接调用它,代码将在后台线程中运行,您无法更新UI。 相反,使用publishProgress 。 编辑:为了能够将正确的值传递给publishProgress ,您还需要在扩展AsyncTask时指定泛型类型(第二个泛型类型是进度的类型)。 试试这样: private static class My ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)