【问题标题】:Android how to fit a picture into progressBarAndroid如何将图片放入progressBar
【发布时间】:2021-02-03 10:23:16
【问题描述】:

这是我的progressBar 的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:indeterminate="false"
        android:indeterminateDrawable="@drawable/loading"
        style="?android:attr/progressBarStyleLarge" />
</androidx.constraintlayout.widget.ConstraintLayout>

这里是 loading.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/picture">
    </item>
    <item>
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270">
            <shape
                android:innerRadiusRatio="2.5"
                android:shape="ring"
                android:thickness="3dp"
                android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->
                <gradient
                    android:angle="0"
                    android:endColor="#007DD6"
                    android:startColor="#007DD6"
                    android:type="sweep"
                    android:useLevel="false" />
            </shape>

        </rotate>
    </item>
</layer-list>

结果我得到:

progressBar 的半径小于图片的半径。有人知道如何解决吗?谢谢。

【问题讨论】:

  • 在布局中具有与progressBar 相同大小的圆形图像视图。
  • @ADM,在这种情况下,我得到的只是一张图片......没有progressBar

标签: android android-layout progress-bar android-progressbar


【解决方案1】:

你也可以这样做-

loading.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270">
            <shape
                android:innerRadiusRatio="2.5"
                android:shape="ring"
                android:thickness="3dp"
                android:useLevel="true">
                <gradient
                    android:angle="0"
                    android:endColor="#007DD6"
                    android:startColor="#007DD6"
                    android:type="sweep"
                    android:useLevel="false"/>
            </shape>
        </rotate>
    </item>
</layer-list>

还有activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">

    <androidx.cardview.widget.CardView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:cardCornerRadius="100dp">
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="false"
            android:indeterminateDrawable="@drawable/loading"
            android:background="@drawable/ic_background"/>
    </androidx.cardview.widget.CardView>

</RelativeLayout>

【讨论】:

  • 感谢您的建议。但在这种情况下,我选择了相同的结果......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多