博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android自定义进度条_Android中的自定义进度栏
阅读量:2532 次
发布时间:2019-05-11

本文共 5752 字,大约阅读时间需要 19 分钟。

android自定义进度条

Custom progress bar in android application gives it a personal touch. In this tutorial, we’ll create a custom progress bar by implementing a spinning logo icon in our application. Most of the time, we end up using a as the loading icon while the data gets loaded. Going by the current trend, apps like Reddit, UBER, Foodpanda and Twitter have replaced the commonly used Progress Bar with their application’s icon as the loading icon. This gives their application as well as logo brand a touch that makes them stand out from the rest.

android应用程序中的自定义进度栏使其具有个人风格。 在本教程中,我们将通过在应用程序中实现旋转徽标图标来创建自定义进度栏。 大多数情况下,在加载数据时,我们最终使用作为加载图标。 按照当前的趋势,Reddit,UBER,Foodpanda和Twitter之类的应用已将其常用的进度栏替换为应用程序的图标作为加载图标。 这为他们的应用程序和徽标品牌提供了一种触感,使它们与众不同。

Android中的自定义进度栏 (Custom Progress Bar in Android)

Let’s see the classical way of showing a loading icon in our application’s activity.

android progress bar example

让我们看看在应用程序的活动中显示加载图标的经典方式。

The code for the layout above should look like this:

上面布局的代码应如下所示:

We’ve set three circular ProgressBars that rotate endlessly in the above layout. Now let’s try to add a ProgressBar that spins an icon indeterminately.

我们设置了三个圆形的ProgressBar,它们在上述布局中不断旋转。 现在,让我们尝试添加一个不确定地旋转图标的ProgressBar。

自定义进度栏Android Studio项目结构 (Custom Progress Bar Android Studio Project Structure)

Android自定义进度条码 (Android Custom Progress Bar Code)

The ProgressBar class contains an attribute indeterminateDrawable which replaces the default indicator with the drawable specified. Let’s see what happens when we place an icon in the ProgressBar.

ProgressBar类包含一个indeterminateDrawable属性,该属性用指定的drawable替换默认指示器。 让我们看看在进度栏中放置一个图标会发生什么。

The code for activity_main.xml looks like this:

activity_main.xml的代码如下所示:

The output that the above layout reflects in our application is given below.

android progressbar drawable output

上面的布局在我们的应用程序中反映的输出如下。

Oops! What’s wrong with the ProgressBar? Why isn’t it rotating?

糟糕! ProgressBar有什么问题? 为什么不旋转?

Well we need to set a RotateDrawable as the value of the attribute.

好吧,我们需要将RotateDrawable设置为属性的值。

A RotateDrawable is defined in the xml by encapsulating the current drawable and assigning it the angle and degrees of rotation. The tag <rotate> is used to do so in the xml as shown below.

通过封装当前可绘制对象并为其分配旋转角度和角度,在xml中定义了RotateDrawable 。 标记<rotate>用于在xml中这样做,如下所示。

The code for the progress_icon.xml RotateDrawable is given below.

下面给出了progress_icon.xml RotateDrawable的代码。

The attribute android:fillAfter indicates that the transformation is applied after the

animation is over.

android:fillAfter属性指示在

动画结束了。

android:toDegrees value can be increased or decreased to change the speed of rotation. Generally it’s recommended to set it in multiples of 360.

android:toDegrees值可以增加或减少以改变旋转速度。 通常,建议将其设置为360的倍数。

Let’s set the above drawable in the ProgressBar present in the activity_main.xml.

让我们在activity_main.xml中存在的ProgressBar中设置上述可绘制对象。

The output reflected in the application is shown below.

android spinning image, android rotate image animation

应用程序中反映的输出如下所示。

Let’s create a basic application that displays a string in a TextView from an after a delay.

让我们创建一个基本的应用程序,该应用程序在延迟后在的TextView中显示一个字符串。

The code for the xml layout file activity_main.xml is given below.

xml布局文件activity_main.xml的代码如下。

Take note of the efficient use of in the above code.

请注意上面代码中的有效使用。

The code for the MainActivity.java is given below.

MainActivity.java的代码如下。

package com.journaldev.spinninglogo;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    TextView textView;    List
quotesList; ProgressBar progressBar; int i = 0; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); quotesList = new ArrayList<>(); quotesList.add("Hi"); quotesList.add("Happy New Year"); quotesList.add("Hope you have a good day"); quotesList.add("Merry Christmas"); Button btnTap = findViewById(R.id.button); textView = findViewById(R.id.textView); progressBar = findViewById(R.id.progressBar); btnTap.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressBar.setVisibility(View.VISIBLE); textView.setVisibility(View.GONE); handler.postDelayed(new Runnable() { @Override public void run() { if (i == quotesList.size()) i = 0; textView.setVisibility(View.VISIBLE); textView.setText(quotesList.get(i++)); progressBar.setVisibility(View.GONE); } }, 3000); } }); }}

The output of the above application in action is given below.

下面给出了上面应用程序的输出。

Note: Ignore the flickering rotation in the gifs. It’s absolutely smooth when running on a device.

注意:忽略gifs中的闪烁旋转。 在设备上运行时绝对流畅。

This brings an end to this tutorial. We’ve implemented a RotationDrawable inside a ProgressBar to achieve a spinning logo like indicator. This should be good for showing a loading progress in apps.

本教程到此结束。 我们已经在ProgressBar内实现了RotationDrawable,以实现类似指示器的旋转徽标。 这对于显示应用程序的加载进度应该很好。

You can download the final Android Custom Progress Bar project from the link below.

您可以从下面的链接下载最终的Android Custom Progress Bar项目

Reference:

参考:

翻译自:

android自定义进度条

转载地址:http://xumzd.baihongyu.com/

你可能感兴趣的文章
[oc学习笔记]多态
查看>>
Tomcat connectionTimeout问题定位处理
查看>>
【PP系列】SAP 取消报工后修改日期
查看>>
ZooKeeper学习第四期---构建ZooKeeper应用(转)
查看>>
JNday4-am
查看>>
UI控件(复习一下)
查看>>
window下自己主动备份数据库成dmp格式的bat写法
查看>>
Memcache存储大数据的问题
查看>>
HDU 5050 Divided Land(进制转换)
查看>>
python进阶学习笔记(三)
查看>>
javascript语法之Date对象与小案例
查看>>
Day45 jquery表格操作、轮播图
查看>>
POJ 2079 Triangle 旋转卡壳求最大三角形
查看>>
【模板】树链剖分
查看>>
计算机博弈研究——六子棋
查看>>
在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)...
查看>>
Android驱动开发第七章
查看>>
ISO 9141-2 and ISO 14230-2 INITIALIZATION and DATA TRANSFER
查看>>
特征点检测--基于CNN:TILDE: A Temporally Invariant Learned DEtector
查看>>
CSS3_实现圆角效果box-shadow
查看>>