【问题标题】:Android custom progress bar with .gif file带有 .gif 文件的 Android 自定义进度条
【发布时间】:2014-12-05 19:00:46
【问题描述】:

在我的应用程序中,我有一个自定义进度条

进度.xml

 <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:progressDrawable="@drawable/loader" />

我的 gif 文件

我想用这张图片创建一个进度条

换句话说,每当我从服务器下载数据并在下载数据后将其删除时,我都想显示此图像

或者你可以说,如何将此图像显示为进度条

【问题讨论】:

  • 您找到解决方案了吗?
  • Android 不支持 git 图片。您必须在 Drawable 文件夹中使用不同的图像并对它们进行动画处理。关注这个问题:- link
  • 如果您需要再次使用gif文件,我建议您使用this库而不是使用多个图像。它过于简单和高效。 :)
  • 我从未尝试过,但我认为您可以使用 WebView 而不是使用外部库。祝你好运。

标签: android android-custom-view android-progressbar


【解决方案1】:
  • 首先将您的 Gif 图像转换为 png Slice 图像序列。

  • 将您的进度条声明为图像视图。

     <ImageView
         android:id="@+id/main_progress"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:visibility="visible" />
    
  • 使用从 gif 生成的 .png 序列图像在 drawable 文件夹中创建 .xml 文件。在这种情况下,Loading_web_animation.xml

     <?xml version="1.0" encoding="utf-8"?>
         <animation-list    xmlns:android="http://schemas.android.com/apk/res/android"
         android:oneshot="false">
     <item
         android:drawable="@mipmap/wblod_0"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_1"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_2"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_3"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_4"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_5"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_6"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_7"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_8"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_9"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_10"
         android:duration="40" />
     <item
         android:drawable="@mipmap/wblod_11"
         android:duration="40" />
     </animation-list>
    
  • 在 Main Activity 中设置类似的代码,

     private AnimationDrawable animationDrawable;
     private ImageView mProgressBar;
     mProgressBar.setBackgroundResource(R.drawable.loading_web_animation);
     animationDrawable = (AnimationDrawable)mProgressBar.getBackground();
     mProgressBar.setVisibility(View.VISIBLE);
     animationDrawable.start();
     mProgressBar.setVisibility(View.GONE);
     animationDrawable.stop();`
    

【讨论】:

  • 这如何阻止用户界面?
【解决方案2】:

我想我回答这个问题有点晚了,不过你也可以试试这个。

XML

<FrameLayout
                android:id="@+id/progress_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true">
                <ProgressBar
                    android:id="@+id/circular_progress"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:indeterminateDrawable="@drawable/my_progress_indeterminate"
                    />
                <TextView
                    android:id="@+id/circular_progress_counter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textSize="@dimen/text_size_big"
                    android:textColor="@color/white"
                    android:text="10"/>
            </FrameLayout>

my_progress_indeterminate.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/process_icon"
    android:pivotX="50%"
    android:pivotY="50%" />

在 Java 文件登录中显示计时器。这里我使用了 10 秒计时器。

private void progressTimer() {
        handler = new Handler();

        if (maxCount >= 0) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    /*
                    Logic to set Time inside Progressbar
                     */

                    mCircularProgressCounter.setText(maxCount+"");

                    maxCount = maxCount - 1;
                    pickupTimer();

                }
            }, 1000);
        } 
    }

Result

【讨论】:

  • 它给出的动画比预期的慢。有什么解决办法吗?
【解决方案3】:

将你的 gif 图片放在 /res/raw 文件夹中

在你的类中声明 mProgressDialog

TransparentProgressDialog mProgressDialog;

然后使用以下代码显示进度对话框

if (mProgressDialog == null)
    mProgressDialog = new TransparentProgressDialog(this);
if (mProgressDialog.isShowing())
    mProgressDialog.dismiss();
    mProgressDialog.setTitle(getResources().getString(R.string.title_progress_dialog));
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();

创建一个可以使用 Glide 库加载 .gif 的类 TransparentProgressDialog。

public class TransparentProgressDialog extends Dialog {

private ImageView iv;

public TransparentProgressDialog(Context context) {
    super(context, R.style.TransparentProgressDialog);
    WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    wlmp.gravity = Gravity.CENTER_HORIZONTAL;
    getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(false);
    setOnCancelListener(null);
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    iv = new ImageView(context);
    GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(iv);
    Glide.with(context).load(R.raw.gif_loader).into(imageViewTarget);

    layout.addView(iv, params);
    addContentView(layout, params);
}

@Override
public void show() {
    super.show();
}

}

【讨论】:

    【解决方案4】:

    我之前在这篇文章中轻松解决了这个问题: Custom progress bar with GIF (animated GIF) 使用显示动画 GIF 的 ImageView,当需要显示等待时使其可见,当所有工作都完成时,让它消失!

    【讨论】:

      【解决方案5】:

      我的解决方案是使用动画图标。

      1°) 在 Drawable 中创建一个 XML“动画”:

      <?xml version="1.0" encoding="utf-8"?>
      <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:drawable="@drawable/micro_1" android:duration="200" />
          <item android:drawable="@drawable/micro_2" android:duration="200" />
          <item android:drawable="@drawable/micro_3" android:duration="200" />
          <item android:drawable="@drawable/micro_4" android:duration="200" />
          <item android:drawable="@drawable/micro_3" android:duration="200" />
          <item android:drawable="@drawable/micro_2" android:duration="200" />
      </animation-list>
      

      2°) 在你的布局中放置一个 imageview

      3°) 将以下代码放入您的活动中:

      import androidx.appcompat.app.AppCompatActivity;
      import android.app.ProgressDialog;
      import android.graphics.drawable.AnimationDrawable;
      import android.os.Bundle;
      import android.os.Handler;
      import android.view.View;
      import android.widget.ImageView;
      
      public class MainActivity extends AppCompatActivity {
          ProgressDialog progressBar;
          private int progressBarStatus = 0;
          private Handler progressBarHandler = new Handler();
          private ImageView micButton;
          //-- for testing progress bar
          private long fileSize = 0;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              //-- declare animation
              final AnimationDrawable[] rocketAnimation = new AnimationDrawable[1];
              final ImageView micButton = findViewById(R.id.mic_button);
              micButton.setBackgroundResource(R.drawable.micro_1);
              //-- button listener
              micButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      //-- init button with animation
                      micButton.setBackgroundResource(R.drawable.mic_image);
                      rocketAnimation[0] = (AnimationDrawable) micButton.getBackground();
                      startprogress(rocketAnimation[0]);
                  }
              });
          }
      
      
          public void startprogress(final AnimationDrawable rocketAnimation) {
               rocketAnimation.start();
              progressBar = new ProgressDialog(MainActivity.this);
               //--reset filesize for demo
              fileSize = 0;
              //-- thread for demo
              new Thread(new Runnable() {
                  public void run() {
                      while (progressBarStatus < 100) {
                          // process some tasks
                          progressBarStatus = doSomeTasks();
                          // your computer is too fast, sleep 1 second
                          try {
                              Thread.sleep(1000);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                          // Update the progress bar
                          progressBarHandler.post(new Runnable() {
                              public void run() {
                                  progressBar.setProgress(progressBarStatus);
                              }
                          });
                      }
      
                      // ok, file is downloaded,
                      if (progressBarStatus >= 100) {
                          // sleep 2 seconds, so that you can see the 100%
                          try {
                              Thread.sleep(2000);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
      
                          // close the progress bar dialog
                          progressBar.dismiss();
                          if(rocketAnimation.isRunning()){
                              rocketAnimation.stop();
                          }
                      }
                  }
              }).start();
      
          }
      
          // file download simulator... a really simple
          public int doSomeTasks() {
      
              while (fileSize <= 1000000) { //1000000
      
                  fileSize++;
      
                  if (fileSize == 100000) {
                      return 10;
                  } else if (fileSize == 200000) {
                      return 20;
                  } else if (fileSize == 300000) {
                      return 30;
                  } else if (fileSize == 400000) {
                      return 40;
                  } else if (fileSize == 500000) {
                      return 50;
                  } else if (fileSize == 600000) {
                      return 60;
                  }
              }
      
              return 100;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 2018-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多