【问题标题】:customize progress bar with Gif image not work fine on some devices使用GIF图像自定义进度条在某些设备上不起作用很好
【发布时间】:2020-01-21 11:55:48
【问题描述】:

我的进度条在某些设备上无法正常工作 like here 并按预期工作 like here 这是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>

【问题讨论】:

    标签: android progress-bar gif


    【解决方案1】:

    先制作更好的 GIF

    如果你想在custom dialog 的中心显示GIF 屏幕使用以下代码:

    使用这个静态方法:

    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了。

    【讨论】:

    • GlideDrawableImageViewTarget 类在哪里!如何导入?
    • 很高兴它帮助了你!
    【解决方案2】:

    我认为这是因为在某些设备上可以将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));
    

    【讨论】:

    • GlideDrawableImageViewTarget 这个类在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多