【问题标题】:Progress bar (circle) not rotating in Android进度条(圆圈)在 Android 中不旋转
【发布时间】:2015-02-28 04:19:02
【问题描述】:

当我的数据从初始屏幕中的数据库加载时,我正在使用进度条(圆圈)。我使用它的标签在 xml 中使用了进度条...但在活动中它没有旋转...建议帮助..它有什么问题?

xml:



          <?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/imgLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/splash" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/loading"
                android:layout_centerHorizontal="true"
                android:gravity="center_horizontal"
                android:text="Loading data..."
                android:visibility="visible"
                android:textAppearance="?android:attr/textAppearanceLarge" />

           <ProgressBar
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="92dp"
            android:gravity="center"
            android:indeterminateDrawable="@drawable/custom_bar" />
    </RelativeLayout>

    Java: 

    public class SplashScreen extends Activity {
        static String DATABASE_NAME = "ItemDB";
        static DBACESS mDbAccess = null;
        public static SQLiteDatabase mDB;
        public static ListItems objitem;
        private static int SPLASH_TIME_OUT = 100;
        private PackageManager packageManager = null;
        static DataEntryClass data = new DataEntryClass();
        static List<ItemClass> item_table;
        ProgressBar progressBar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_splash);
            progressBar = (ProgressBar) findViewById(R.id.loading);
            // ////////////////////////
            /////////////////////////
            CheckConnectionState(getApplicationContext());
            packageManager = getPackageManager();

            if (objitem == null)
                objitem = new ListItems(getApplicationContext(), mDB);

            if (!objitem.ItemsExist(mDB))
                try {
                    data.addItems(getApplicationContext());
                } catch (Exception e) {
                    // TODO: handle exception
                }

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                    data.getNewUpdates(getApplicationContext(), packageManager);
                    item_table = new ArrayList<ItemClass>();
                    item_table = objitem.LoadItems(mDB);

                    Intent i = new Intent(SplashScreen.this, TabActivity.class);
                    startActivity(i);

                    // close this activity
                    finish();
                }
            }, SPLASH_TIME_OUT);
        }



        public static void CheckConnectionState(Context ct) {
            try {

                if (mDB == null || !mDB.isOpen()) {
                    DBACESS.DATABASE_NAME = DATABASE_NAME;
                    mDbAccess = new DBACESS(ct);
                    mDB = mDbAccess.getReadableDatabase();
                }
            } catch (Exception e) {

            }
        }
    }

custom_bar.xml:
<?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:toDegrees="360"
android:repeatCount="infinite"
android:drawable="@drawable/menu" >
</rotate> 

【问题讨论】:

  • 发布您的相关代码。
  • 您必须在活动中以编程方式为该进度条设置旋转动画。
  • 您是如何从数据库中加载数据的?如果您在 UI 线程中执行该工作,那么您的 UI 可能会卡住,直到加载数据。如果是这种情况,那么您应该使用一些后台数据加载机制,例如 AsyncTasks 在后台线程中加载数据
  • @dishan 你说得对……我就这样解决了。

标签: android xml splash-screen android-progressbar


【解决方案1】:

这个问题很老,但是还有另一个考虑因素。为开发者设备找出错误非常容易,也非常困难。

只需确保您的动画在您的开发者选项中活动。激活这些选项:

Window animation scale: Animation scale 1x
Transition animation scale: Animation scale 1x
Animator duration scale: Animation scale 1x

【讨论】:

    【解决方案2】:

    对于旋转图像,您可以在 drawable 文件夹中使用 custom_progress_bar.xml

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

    设置drawable到进度条

       <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" 
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/custom_progress_bar"/>
    

    【讨论】:

    • 你需要在drawable文件夹中创建custom_progress_bar.xml,然后你需要像我的例子一样将此custom_progress_bar.xml设置到progressba中......
    • 我做到了。但同样的问题。
    • 在风格 ?android:attr/progressBarStyleLarge 上工作得很好。我不需要android:indeterminate="true"
    • 也适用于style="@android:style/Widget.ProgressBar.Inverse,但最好创建自己的风格或使用Android资源中的progress_medium_holo.xml
    • 什么是@drawable/gif_loader_img1
    【解决方案3】:
     <ProgressBar
            android:indeterminateDrawable="@drawable/loader4_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginTop="5dp"
            />
    

    创建一个可绘制的 xml loader4_progress

     <?xml version="1.0" encoding="utf-8"?>
    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/spinner"
        android:duration="1"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="1080"
        />
    

    更多详情请查看这里 http://custom-android-dn.blogspot.in/2013/01/how-to-create-custom-progress-bar-and.htmlHow to Customize a Progress Bar In Android

    【讨论】:

    • 什么是@drawable/spinner
    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多