2014-10-09 91 views
0

我有一個可繪製的進度條。當設置完成後,我希望它能夠緩慢填充。這就是我應該能夠看到它充滿,而不是一剎那。另外我在進度條裏面有一個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> 

它給這樣的警告:佈局文件中找到意外的文字: 「機器人:fromdegrees =」 0" 安卓pivotx =「50 % 「 機器人:pivoty = 」50%「 的Android版本:todegrees = 」360「 機器人:以...」

回答

1

使用此代碼它會幫助你。

必需的東西:

  1. CustomProgressDialog
  2. 阿尼姆
  3. drawble
  4. 佈局

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)動畫

<?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)可繪製

<?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
0

您可以使用HandlerRunnable

使處理程序以void next()或其他方法執行postDelayed(yourRunnable, DELAY);

run()yourRunnable方法中,將進度位置增加X量(很少,比如1%或取決於您的願望的東西),以及invalidate()您的進度條。當進度條增加X的進度位置時,再次調用next(),這將再次執行yourRunnable.run(),這會增加X的進度位置(或者可以使用更復雜的數學來逐步增加數量,而不是靜態的X數量),所以循環直到進度達到您想要的數量。

private ProgressAnimator animator; 

@Override 
public void onDraw(Canvas c) { 
    super.onDraw(c); 
    if (animator.started) { 
    animator.next(); 
    } 
} 

public void animateProgress(float end, IAnimator.PostAnimationAction postPostAnimationAction) { 
    animator.start(end, postPostAnimationAction); 
} 

private class ProgressAnimator { 

    static final long DELAY = 10; 

    // NOTE: 
    // "MULTIPLY_FACTOR" IS OPTIONAL. 
    // I USED THIS TO INCREASE THE PROGRESS SMOOTHLY AT 
    // DYNAMIC SPEED, NOT AT STATIC SPEED. 
    // IF YOU WANT STATIC INCREASE OF THE PROGRESS, 
    // DONT USE THIS. 
    static final float MULTIPLY_FACTOR = 0.2f; 

    float endPosition; 
    float remaining; 
    boolean started; 
    PostAnimationAction postAnimationAction; 


    Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 

    @Override 
    public void run() { 
     if (Math.abs(remaining) > 1) { 
     raiseProgress(/* pass the current progress position here*/); 
     } else { 
     end(); 
     } 
    } 
    }; 

    /** 
    * Constructor. 
    * 
    */ 
    ProgressSweepAnimator() { 
    init(); 
    } 

    @Override 
    public void init() { 
    started = false; 
    } 

    // NOTE: 
    // "PostPostAnimationAction" IS OPTIONAL. I CREATED AND USED THIS 
    // BECAUSE I WANTED TO DO SOMETHING AFTER THE PROGRESS 
    // ANIMATION IS FINISHED. 
    public void start(float endPosition, PostAnimationAction postPostAnimationAction) { 
    this.endPosition = endPosition; 
    start(postPostAnimationAction); 
    } 

    @Override 
    public void start(PostAnimationAction postAnimationAction) { 
    // Initialize status. 
    started = true; 
    this.postAnimationAction = postAnimationAction; 

    // Find out the current progress. 
    float start = /* get the current progress position here*/; 

    remaining = end - start; 
    raiseProgress(start); 
    } 

    @Override 
    public void next() { 
    handler.postDelayed(runnable, DELAY); 
    } 

    private void raiseProgress(float from) { 
    float toRaise = remaining * MULTIPLY_FACTOR; 
    remaining -= toRaise; 

    raiseProgressTo(from + toRaise); 
    } 


    @Override 
    public void end() { 
    raiseTo(end); 
    init(); 

    // NOTE: 
    // THIS IS OPTIONAL BUT MAY BE VERY USEFUL, SO I 
    // JUST LEFT THIS OUT HERE. 
    // Notify the subscriber about the completion of the Burst action. 
    postAnimationAction.onActionComplete(); 
    } 
} 
0

我不知道該文本旋轉,但緩慢增加進度條,你可以試試這個:

mProgressBar是抱着你的進步實例全局進度條變量bar

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); 
    } 
} 
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); 
    } 
}; 

創建和您ProgressBar(mProgressBar)啓動該財產ObjectAnimator

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

您可以Interpolator costumize動畫s,持續時間不同,ecc

您還可以使用:

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

沒有declarying屬性,但我不喜歡這種方式那麼多。

+0

真正偉大的答案! – AndyRoid 2016-03-21 09:24:39

0

下面是我使用逐漸填充圓形progressbar-

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);