ProgressBar:自定义旋转图片

自定义旋转图分三步:

1、在layout中为ProgressBar设计布局;
2、在style中为ProgressBar设置Style;
3、新建一个类继承ProgressBar,在构造方法中设置style,加载布局;


下面是我写的一个例子,先看第一步,布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/progressdialog_background"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/xhdpi_8dp"
    android:paddingLeft="@dimen/xhdpi_18dp"
    android:paddingRight="@dimen/xhdpi_18dp"
    android:paddingTop="@dimen/xhdpi_8dp">

    <ProgressBar
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:indeterminateBehavior="repeat"
        android:indeterminateDrawable="@drawable/progressbar_drawable"
        android:indeterminateDuration="2000"
        android:indeterminateOnly="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/xhdpi_8dp"
        android:text="登入中..."
        android:textColor="@color/ad_color_white"
        android:textSize="@dimen/xhdpi_12sp" />

</LinearLayout>

android:indeterminateBehavior=”repeat”设置行为为重复,及图片会一直重复旋转;
android:indeterminateDrawable=”@anim/progressbar_drawable”设置旋转动画;
android:indeterminateDuration=”2000”这里是设置完成一次动画的时间
android:indeterminateOnly=”true”设置为不确定模式
下面是anim/progressbar_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"
    android:drawable="@mipmap/progress_orange">
</rotate>

注意如果这里设置android:duration的时间是不起作用的。
再看第二步,设置style:

    <style name="progressDialog_style" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

android:windowBackground,设置ProgressBar的背景为透明,如果不透明,会有黑色边框;
android:backgroundDimEnabled,设置窗口背景是否变暗,false为不变暗。
最后一步,新建MyProgressBar类继承ProgressBar,代码如下:

public class HlwProgressDialog extends ProgressDialog {
    public HlwProgressDialog(Context context, int theme) {
        //将自定义的style转进去,设置为我们定义的style
        super(context, R.style.progressDialog_style);
        //点击返回键可以dismissProgressBar
        setCanceledOnTouchOutside(false);
    }

    public HlwProgressDialog(Context context) {
        this(context, R.style.progressDialog_style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载我们设计的布局
        setContentView(R.layout.hlw_progressdialog);
    }
}

大功告成,现在我们就可以和使用系统的ProgessBar一样使用我们自定义的了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值