使用 Gif 图像自定义进度条在某些设备上无法正常工作

customize progress bar with Gif image not work fine on some devices

我的进度条在某些设备上无法正常工作like here and working as expected like here and this is original file

如果有人对在进度条上使用 gif 文件有更好的想法,请与我分享。

.java

public static void displayProgressDialog(Context context, boolean cancellable) {
    ImageView imageView;

    if (context == null || (context instanceof AppCompatActivity && ((AppCompatActivity) context).isFinishing()))
        return;
    if (cancellable) {
        if (context instanceof AppCompatActivity && view == null) {
            AppCompatActivity activity = (AppCompatActivity) context;
            view = activity.getLayoutInflater().inflate(R.layout.layout_dialog_loading, null);
            ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            activity.addContentView(view, params);
        }
    } else {
        if (progressDialog != null) return;
        progressDialog = new AlertDialog.Builder(context, R.style.MyTheme).create();
        FrameLayout f1 = new FrameLayout(context);
        progressDialog.setView(f1);
        progressDialog.getLayoutInflater().inflate(R.layout.layout_dialog_loading, f1);
        progressDialog.setCancelable(false);
//        progressDialog.setIndeterminate(false);
//        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
    }
}

xml

<?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:orientation="horizontal">
    <ProgressBar
        android:id="@+id/progress"
        android:indeterminate="true"
        android:indeterminateTint="@color/bgColorDefault"
        android:indeterminateDrawable="@drawable/myprogress_interminate"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

旋转

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

动画终于来了

<?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:duration="1"
    android:toDegrees="360"
    android:drawable="@drawable/spinnerinstacare">
</rotate>

我认为这是因为在某些设备上可以更改 attrs 默认的系统宽度和高度。这可能会影响某些小部件,例如 ProgressBar。您需要在布局中指定项目的确切大小。

或者仅使用 ImageViewGlide 以在加载过程中显示 gif,设置非常简单。

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.0.0'
}

和用法:

Glide.with(context).load(GIF_URI)
    .into(new GlideDrawableImageViewTarget(IMAGE_VIEW));

先制作一张更好看的GIF

If you want to show GIF inside a custom dialog at the center of the screen use below code:

使用这个静态方法:

public static void showProgress(Context context) {  
    Dialog progressDialog = new Dialog(context, R.style.AppThemeDialog);
    progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    progressDialog.setContentView(R.layout.progers_layout);
    WindowManager.LayoutParams wmlp = progressDialog.getWindow().getAttributes();
    wmlp.gravity = Gravity.CENTER | Gravity.CENTER;
    wmlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
    wmlp.height = ViewGroup.LayoutParams.MATCH_PARENT;
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

进度布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/img_anim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>
</RelativeLayout>

像这样使用 Glide:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(this).asGif().load(R.raw.image_gif).into(imageView);

然后你可以使用Glide加载GIF