在 Android 中实现类似于 iOS 的旋转活动指示器
我正在尝试实现类似于我在 Android 中放置的旋转活动。我相信我应该使用 ProgressDialog。我的问题源于如何实际操作 ProgressDialog 以使其看起来像活动指示器。
欢迎任何想法。一个例子的链接就更好了。
谢谢。
REEDIT:
myProgress.java
public class myProgress extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog d = (ProgressDialog)findViewById(R.id.progres);
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progres"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ProgressBar
android:id="@+id/progressBar"
android:indeterminate="true"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
I am trying to implement the spinning activity similar to the the one I have placed below in Android. I believe I should use the ProgressDialog. My issue arises from how to actually manipulate the ProgressDialog to appear like the activity indicator.
Any thoughts are welcome. A link to an example would even be better.
Thanks.
REEDIT:
myProgress.java
public class myProgress extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog d = (ProgressDialog)findViewById(R.id.progres);
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progres"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ProgressBar
android:id="@+id/progressBar"
android:indeterminate="true"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我编写了自己的自定义 LoadingIndicatorView。
它由两个文件组成:
优点:
缺点 :
免责声明:
您会注意到我的编码风格和结构与我的 iOS 编程代码非常相似。我以编程方式完成所有事情,如果可以的话,就不使用 XML。
如何使用这个 Loading Indicator
将所有三个类的源代码复制并粘贴到其 Java 文件中后,您想要使用
LoadingIndicatorView
类,您不需要接触其他类,除非您想要自定义每个条形的颜色或圆角。在您的 Activity 中创建一个
LoadingIndicatorView
实例:一旦您准备好显示它(例如在按钮单击侦听器中),则您可以调用:
当您想要停止并隐藏指示器时,请调用:
你最终会得到这样的结果:
LoadingIndicatorView.java
LoadingIndicatorBarView.java
Toolbox.java
这个 Toolbox 类是我的方便帮助程序类,用于在我的所有项目中创建圆角形状等。
希望有帮助:D
I wrote my own custom LoadingIndicatorView.
It consists of two files:
Pros:
Cons:
Disclaimer:
You'll notice my coding style and structure resemble my iOS programming codes a lot. I do everything programmatically, no XML if I can get away with it.
How to use this Loading Indicator
After you've copied and pasted all three class source codes into their Java file, you want to use the
LoadingIndicatorView
class, you shouldn't need to touch the other class, unless you want to customise the colour or rounded corner of each bar.Create an instance of
LoadingIndicatorView
like this in your Activity:Once you're ready to show it, e.g. in a button click listener, then you call:
When you want to stop and hide the indicator, call:
You end up with something like this:
LoadingIndicatorView.java
LoadingIndicatorBarView.java
Toolbox.java
This Toolbox class is my convenience helper class to create rounded corner shapes etc in all my projects.
Hope that helps :D
这就是我实现它的方法,
这里的代码
是 loading.xml
调用对话框,并
隐藏它
使用UPDATE
,如果您愿意,自定义指示器您可以在中执行以下操作layout.xml。
ProgressBar
替换为ImageView
background
设置为 AnimationDrawableonPrepareDialog
中启动动画this is how i achieve it
here is the code
here is the loading.xml
call the dialog with
hide it using
UPDATE
if you want and custom indicator you can do the following in the layout.xml.
ProgressBar
with anImageView
background
of the ImageView to a AnimationDrawableonPrepareDialog
我相信你正在寻找进步对话框。您可以通过此链接开始设置。
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
You are looking for progressDialog i believe. This link can you set you start with it.
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
看看这个图书馆就知道了。 IOSDialog/Spinner 库
它非常易于使用并解决您的问题。有了它,您可以像在 IOS 中一样轻松创建和使用 Spinner。
代码示例:
结果
结果:标准 IOS 对话框
Just look at this library. IOSDialog/Spinner library
It is very easy to use and solves your problem. With it, you can easily create and use spinner like in IOS.
The example of code:
Result
Result: stadard IOS Dialog
谢谢@zhang 的解决方案!就我而言,根据我的项目要求,我有不同的动画(没有背景恒定的不透明度):
在这里查看第一个动画
另外,我有不同的颜色, radius,想要动画自动启动并修改颜色,半径来自 xml。为此,我修改了
LoadingIndicatorView.java
和LoadingIndicatorBarView.java
并在attrs.xml
、Toolbox.java
中添加了样式code> 未修改(见下文)。使用示例:
如果您有类似的需求,那么这是我的代码:
LoadingIndicatorView.java
LoadingIndicatorBarView.java
Toolbox.java
attrs .xml
Thank you @zhang for you solution! In my case, according to my project's requirements, I had different animation (without background constant opacity):
see the first animation here
Also, I had different color, radius, wanted animation to start automatically and modify color, radius from the xml. To achieve that, I modified
LoadingIndicatorView.java
andLoadingIndicatorBarView.java
and added style inattrs.xml
,Toolbox.java
is unmodified (see below).Example of usage:
If you have similar requirements, then this is my code:
LoadingIndicatorView.java
LoadingIndicatorBarView.java
Toolbox.java
attrs.xml