如何使进度条缓慢填充

时间:2014-10-09 11:06:32

标签: java android animation layout progress-bar

我有一个drawable的进度条。我希望它在设置进度完成时缓慢填充。这就是我应该能够看到它填充,而不是一瞬间。此外,我在进度条内部有一个texview,我希望textview继续旋转,一旦设置进度填充完成,textview应该停止旋转并显示进度。

这是我的代码:

布局:

    <ProgressBar
        android:id="@+id/circularProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:layout_centerInParent="true"
        android:indeterminate="false"
        android:max="100"
        android:progress="0"
        android:progressDrawable="@drawable/progressbar" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="47sp"
        android:textStyle="bold"
        android:textColor="@color/Theme" />
</RelativeLayout>

progressbar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0"
            android:useLevel="false" >
            <solid android:color="@color/Red" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0" >
            <solid android:color="@color/Theme" />
        </shape>
    </item>

</layer-list>

另外,我在这个进度条中有一个textview,我希望它继续旋转直到进度完成。怎么做,我有一个动画,但它不起作用。 这是代码:

rotate.xml

<rotate xmlns:android="http://schemas.android.com/apk/res/android" >
android:duration="4000"
android:fromdegrees="0"
android:pivotx="50%"
android:pivoty="50%"
android:todegrees="360"
android:toyscale="0.0"

</rotate>

它会发出如下警告:在布局文件中找到意外的文本:&#34; android:fromdegrees =&#34; 0&#34;机器人:pivotx =&#34; 50%&#34;  机器人:pivoty =&#34; 50%&#34;机器人:todegrees =&#34; 360&#34;机器人:为...&#34;

5 个答案:

答案 0 :(得分:1)

为进度创建Property

final Property<ProgressBar, Integer> progressProperty = new Property<ProgressBar, Integer>(
        int.class, "progress") {
    @Override
    public Integer get(ProgressBar object) {
        return object.getProgress();
    }

    @Override
    public void set(ProgressBar object, Integer value) {
        object.setProgress(value);
    }
};

ObjectAnimator(mProgressBar)上为该属性创建并启动ProgressBar

private void animateProgressBar(int targetProgress) {
    ObjectAnimator anim = ObjectAnimator.ofInt(mProgressBar, progressProperty, targetProgress);
    anim.setInterpolator(new DecelerateInterpolator());
    anim.setDuration(300);
    anim.start();
}

您可以使用Interpolator s,不同的持续时间,ecc

来制作动画

您也可以使用:

private void animateProgressBar(int targetProgress) {
    ObjectAnimator anim = ObjectAnimator.ofInt(mProgressBar, "progress", targetProgress);
    anim.setInterpolator(new DecelerateInterpolator());
    anim.setDuration(300);
    anim.start();
}

没有声明属性,但我不喜欢这种方法。

答案 1 :(得分:1)

使用此代码可以帮助您。

必需材料:

  1. CustomProgressDialog
  2. 动画
  3. drawble
  4. 布局
  5. 1)CustomProgressDialog

    package com.est.framework.android.ui;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.graphics.drawable.AnimationDrawable;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.sc.restclient.R;
    
    // TODO: Auto-generated Javadoc
    /**
     * The Class CustomProgressDialog.
     */
    public class CustomProgressDialog extends Dialog {
    
        /**
         * Instantiates a new custom progress dialog.
         *
         * @param context the context
         * @param theme the theme
         */
        public CustomProgressDialog(Context context, int theme) {
            super(context, theme);
        }
    
        /* (non-Javadoc)
         * @see android.app.Dialog#onWindowFocusChanged(boolean)
         */
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            ImageView imageView = (ImageView) findViewById(R.id.loadingImageView);
            AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground();
            yourAnimation.start();
        }
    
        /**
         * Creates the dialog.
         *
         * @param context the context
         * @param title the title
         * @param message the message
         * @return the custom progress dialog
         */
        public static CustomProgressDialog createDialog(Context context,String title) {
            CustomProgressDialog dialog = new CustomProgressDialog(context,android.R.style.Theme_Panel);
            dialog.setContentView(R.layout.dialogimg);
    
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
    
            TextView mloadMsg = (TextView)dialog.findViewById(R.id.loadMsg);
            if (title != null) {
                mloadMsg.setText(title);
            }else{
                mloadMsg.setVisibility(View.GONE);
            }
            dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            dialog.setCancelable(false);
            return dialog;
        }
    }
    

    2)布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/pop_bg_bitmap"
        android:gravity="center"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/loadingImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@anim/loading_anim" />
    
        <TextView
            android:id="@+id/loadMsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/btn_stroke_text"
            android:text="Loading..." />
    
    </LinearLayout>
    

    3)Anim

    <?xml version="1.0" encoding="utf-8"?>
        <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
        <item android:drawable="@drawable/progress_1" android:duration="500" />
        <item android:drawable="@drawable/progress_2" android:duration="500" />
        <item android:drawable="@drawable/progress_3" android:duration="500" />
        <item android:drawable="@drawable/progress_4" android:duration="500" />
    
    </animation-list>
    

    4)Drawable

      <?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    
        android:src="@drawable/fancybox_overlay"
        android:antialias="false"
        android:dither="true"
        android:tileMode="repeat"
        >
    </bitmap>
    

    图片:

    1. fancybox_overlay.png
    2. progress_1.png
    3. progress_2.png
    4. progress_3.png
    5. progress_4.png

答案 2 :(得分:0)

您可以使用HandlerRunnable

让处理程序在名为postDelayed(yourRunnable, DELAY);的新方法中执行void next()

run()的{​​{1}}方法中,将进度位置增加X量(非常少,例如1%或者根据您的需要),以及yourRunnable您的进度条。在进度条将进度位置增加X量后,再次调用invalidate(),这将再次执行yourRunnable.run(),这将使进度位置增加X量(或者您可以使用一点点逐渐增加量更复杂的数学,而不是静态X数量),循环也是如此,直到进度填满你想要的数量。

next()

答案 3 :(得分:0)

我不知道文字轮换,但是为了慢慢增加进度条,你可以试试这个:

mProgressBar是一个全局进度条变量,它保存进度条的实例

mProgress是一个初始设置为0的全局整数变量,用于监控进度条的当前级别。

pSize是您希望从当前级别增加进度条的数量。

pSleepTime是您希望进度条填满的速度。我通常使用50。

private void setProgressBar(int pSize, int pSleepTime) throws InterruptedException {

    for(int i=0;i<pSize && mProgress<100;i++){
        mProgress++;
        Thread.sleep(pSleepTime);
        mProgressBar.setProgress(mProgress);
    }
}

答案 4 :(得分:0)

以下是我用于逐步填写循环进度条的代码 -

XML

<ProgressBar
        android:id="@+id/progress"
        android:layout_width="100dp"
        android:layout_height="100dp"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="false"
        android:layout_centerInParent="true"
        android:max="100"
        android:visibility="gone"
        android:progressDrawable="@drawable/background" />

background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:useLevel="true"
            android:thicknessRatio="14.0">

            <gradient
                android:startColor="#999999"
                android:endColor="#999999"
                android:centerColor="#999999"
                android:type="sweep" />
        </shape>
    </item>
    <item android:id="@android:id/background">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="14.0"
            android:useLevel="false" >
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270" >
            <shape
                android:innerRadiusRatio="3"
                android:shape="ring"
                android:useLevel="true"
                android:thicknessRatio="14.0">
                <rotate
                    android:fromDegrees="0"
                    android:toDegrees="360"
                    android:pivotX="50%"
                    android:pivotY="50%" />
                <gradient
                    android:startColor="#8E58D4"
                    android:endColor="#8E58D4"
                    android:centerColor="#8E58D4"
                    android:type="sweep" />
            </shape>
        </rotate>
    </item>
</layer-list>

代码

 mProgressBar = (ProgressBar) activity.findViewById(R.id.progress);
 Resources res = getResources();
 Drawable drawable = res.getDrawable(R.drawable.background);
mProgressBar.setProgressDrawable(drawable);
ObjectAnimator animation = ObjectAnimator.ofInt(mProgressBar, "progress", 0, 100);
 animation.setDuration(10000);