简体   繁体   English

Android动画(进度栏)无法顺序加载

[英]Android Animation (Progress Bar) Does Not Load Sequentially

I'm attempting to load a series of 5 dots to indicate progress being made however they are not loading sequentially one after another as they should and I'm not sure why. 我正在尝试加载一系列5个点,以指示正在取得进展,但是它们并没有按顺序依次加载,我不确定为什么。

SOURCE: 资源:

public class UpdateActivity extends Activity implements OnClickListener {

    public static int TotalSteps = 8;
    private TelephonyManager tm;

    private Button mUpdateButton = null;
    private Button mAssistUpdateButton = null;
    private Button mAssistInstrButton = null;
    private TextView mReadAgainButton = null;
    private int mInstructionNumber = 0;
    AlertDialog mConfirmAlert = null;
    private NetworkTask task;
    private AnimationDrawable loadingAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        int networkType = tm.getNetworkType();
        int phoneType = tm.getPhoneType();
        int version = android.os.Build.VERSION.SDK_INT;
        if (phoneType == TelephonyManager.PHONE_TYPE_CDMA
                || (phoneType != TelephonyManager.PHONE_TYPE_GSM
                        && networkType != TelephonyManager.NETWORK_TYPE_GPRS
                        && networkType != TelephonyManager.NETWORK_TYPE_EDGE
                        && networkType != TelephonyManager.NETWORK_TYPE_HSDPA
                        && networkType != TelephonyManager.NETWORK_TYPE_HSPA
                        && networkType != TelephonyManager.NETWORK_TYPE_HSPAP
                        && networkType != TelephonyManager.NETWORK_TYPE_HSUPA
                        && networkType != TelephonyManager.NETWORK_TYPE_UMTS && networkType != TelephonyManager.NETWORK_TYPE_LTE)) {
            // If the phone type is CDMA or
            // the phone phone type is not GSM and the network type is none of
            // the network types indicated in the statement
            // Display incompatibility message
            showAlert(getString(R.string.incomp_sm_dialog));
            // Network type is looked because some tablets have no phone type.
            // We rely on network type in such cases
        } else if (!(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT
                || (tm.getSimOperator())
                        .equals(getString(R.string.numeric_tmo)) || (tm
                    .getSimOperator()).equals(getString(R.string.numeric_att)))) {
            // if SIM is present and is NOT a T-Mo network SIM,
            // display Error message alert indicating to use SM SIM
            showAlert(getString(R.string.insert_sm_dialog));
        }// No SIM or SIM with T-Mo MNC MCC present
        else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
            // Initial UI setup for versions lower than ICS
            setContentView(R.layout.update);
            mUpdateButton = (Button) findViewById(R.id.update_button);

            mUpdateButton.setOnClickListener(this);
        } else {// ICS and up

            // task.execute();

            if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))
                    || (tm.getSimOperator())
                            .equals(getString(R.string.numeric_att))) {

                task = new NetworkTask();
                task.execute("");
                // Device has T-Mo network SIM card MCC and MNC correctly
                // populated
                // Reduce number of steps to 6
                TotalSteps = 6;
            }
            //
            // Initial UI setup for ICS and up

            setContentView(R.layout.updating);
            // This image view has the updating text to be progressively
            // updated
            // with dots addition
            ImageView loading = (ImageView) findViewById(R.id.loading_empty1);

            // Set updating button to drawable animation
            loading.setBackgroundResource(R.drawable.updating1);
            loadingAnimation = (AnimationDrawable) loading.getBackground();
            loadingAnimation.setOneShot(false);
            loadingAnimation.start();

            ImageView loading2 = (ImageView) findViewById(R.id.loading_empty2);

            // Set updating button to drawable animation
            loading2.setBackgroundResource(R.drawable.updating2);
            loadingAnimation = (AnimationDrawable) loading2.getBackground();


            loadingAnimation.start();

            ImageView loading3 = (ImageView) findViewById(R.id.loading_empty3);

            // Set updating button to drawable animation
            loading3.setBackgroundResource(R.drawable.updating3);
            loadingAnimation = (AnimationDrawable) loading3.getBackground();


            loadingAnimation.start();


            ImageView loading4 = (ImageView) findViewById(R.id.loading_empty4);

            // Set updating button to drawable animation
            loading4.setBackgroundResource(R.drawable.updating4);
            loadingAnimation = (AnimationDrawable) loading4.getBackground();


            loadingAnimation.start();


            ImageView loading5 = (ImageView) findViewById(R.id.loading_empty5);

            // Set updating button to drawable animation
            loading5.setBackgroundResource(R.drawable.updating5);
            loadingAnimation = (AnimationDrawable) loading5.getBackground();


            loadingAnimation.start();
            // String assistUpdate = getString(R.string.apn_app_text_cta2);
            // CharSequence styledText = Html.fromHtml(assistUpdate);
            // xtView assistText = (TextView)
            // findViewById(R.id.apn_app_text_cta2);
            // sistText.setText(styledText);
            // mAssistUpdateButton = (Button)
            // findViewById(R.id.assist_update_btn);
            //mAssistUpdateButton.setOnClickListener(this);
        }
    }

    public void onClick(View v) {

        if (v == mUpdateButton) {
            // Update button for versions lower than ICS is selected
            // setContentView(R.layout.updating);
            onClickMethod(v);

            Intent i = new Intent(this, ConfigFinalActivity.class);
            startActivity(i);
            finish();
        } else if (v == mAssistUpdateButton) {

            // Update button for ICS and up is selected
            // Get the TextView in the Assist Update UI
            TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
            String text = "";
            CharSequence styledText = text;
            switch (mInstructionNumber) {
            case 0:
                // Retrieve the instruction string resource corresponding the
                // 2nd set of instructions
                text = String.format(getString(R.string.apn_app_text_instr),
                        TotalSteps);
                styledText = Html.fromHtml(text);
                // Update the TextView with the correct set of instructions
                tv.setText(styledText);
                // Increment instruction number so the correct instructions
                // string resource can be retrieve the next time the update
                // button is pressed
                mInstructionNumber++;
                break;
            case 1:
                text = getString(R.string.apn_app_text_instr2);
                styledText = Html.fromHtml(text);
                tv.setText(styledText);
                // Increment instruction number so the correct instructions
                // string resource can be retrieve the next time the update
                // button is pressed
                mInstructionNumber++;
                break;
            case 2:
                // Final set of instructions-Change to the corresponding layout

                setContentView(R.layout.assist_instructions);
                String assistUpdateInstr = String.format(
                        getString(R.string.apn_app_text_instr3), TotalSteps);
                styledText = Html.fromHtml(assistUpdateInstr);
                TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
                assistInstrText.setText(styledText);
                mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
                mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
                mAssistInstrButton.setOnClickListener(this);
                mReadAgainButton.setOnClickListener(this);
            }
        } else if (v == mAssistInstrButton) {
            // "LET'S DO THIS" Button in final instructions screen for ICS and
            // up is selected
            // Create ConfigActivity Intent
            Intent i = new Intent(this, ConfigFinalActivity.class);
            // Invoke ConfigActivity Intent to start the assisted update
            startActivity(i);
            finish();
        } else if (v == mReadAgainButton) {
            // go back to 1st set of instructions if read again is selected
            mInstructionNumber = 0;
            setContentView(R.layout.assist_update);
            String assistUpdate = getString(R.string.apn_app_text_cta2);
            CharSequence styledText = Html.fromHtml(assistUpdate);
            TextView assistText = (TextView) findViewById(R.id.apn_app_text_cta2);
            assistText.setText(styledText);
            mAssistUpdateButton = (Button) findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(this);
        }
    }

    public void onClickMethod(View v) {
        mUpdateButton = (Button) findViewById(R.drawable.btn_update_active_hdpi);

    }

    private void showAlert(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message).setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        UpdateActivity.this.finish();
                    }
                });
        mConfirmAlert = builder.create();
        mConfirmAlert.show();
    }

}

updating.xml update.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="wrap_content"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/updating_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/updating_text"
        android:textColor="#000000"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/loading_empty1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:layout_toLeftOf="@+id/loading_empty2"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/loading_empty"
         />

    <ImageView
        android:id="@+id/loading_empty3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/loading_empty2"
        android:layout_centerHorizontal="true"
        android:background="@drawable/loading_empty"
        android:layout_marginRight="15dp"
        />

    <ImageView
        android:id="@+id/loading_empty2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/loading_empty1"
        android:layout_toLeftOf="@+id/loading_empty3"
        android:background="@drawable/loading_empty"
        android:layout_marginRight="15dp"
         />

    <ImageView
        android:id="@+id/loading_empty4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/loading_empty3"
        android:layout_toRightOf="@+id/loading_empty3"
        android:background="@drawable/loading_empty"
        android:layout_marginRight="15dp"
         />

    <ImageView
        android:id="@+id/loading_empty5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/loading_empty4"
        android:layout_toRightOf="@+id/loading_empty4"
        android:background="@drawable/loading_empty"
         />

</RelativeLayout>

updating1.xml: updates1.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">    
    <item android:drawable="@drawable/loading_empty" android:duration="500" />    
    <item android:drawable="@drawable/loading_full" android:duration="1000" />    
</animation-list>

updating2.xml: updates2.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">    
    <item android:drawable="@drawable/loading_empty" android:duration="1500" />    
    <item android:drawable="@drawable/loading_full" android:duration="2000" />    
</animation-list>

updating3.xml: updates3.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">    
    <item android:drawable="@drawable/loading_empty" android:duration="2500" />    
    <item android:drawable="@drawable/loading_full" android:duration="3000" />    
</animation-list>

updating4.xml: updates4.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">    
    <item android:drawable="@drawable/loading_empty" android:duration="3500" />    
    <item android:drawable="@drawable/loading_full" android:duration="4000" />    
</animation-list>

updating5.xml: updates5.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">    
    <item android:drawable="@drawable/loading_empty" android:duration="4500" />    
    <item android:drawable="@drawable/loading_full" android:duration="5000" />    
</animation-list>

You're using duration wrong. 您使用的持续时间错误。 It represents the length of the animation, not the start time. 它代表动画的长度,而不是开始时间。 So, get rid of all but one of your xml animations. 因此,除去所有XML动画之外的所有动画。 You can reuse the same for each animation. 您可以为每个动画重用相同的内容。 The key is, the animations need to start at a different time for each dot. 关键是,每个点的动画需要在不同的时间开始。

To start the animations at different times, use a Handler , and call postDelayed with a Runnable for every dot, performing the animation on the correct View for the current dot, and reusing the same animation xml. 要在不同时间启动动画,请使用Handler ,并为每个点调用带有Runnable postDelayed ,在当前点的正确View上执行动画,并重新使用相同的动画xml。 For the first dot use a delay of 0ms or something low, for the second dot use a delay of the 1st dot + total duration of the animation (500ms? 1000ms? whatever you set in your xml...), for the nth dot use a delay of the n-1 dot and the aforementioned total duration of the animation 对于第一个点,请使用0ms或更低的延迟,对于第二个点,请使用第一个点+动画的总持续时间(500ms?1000ms?无论您在xml中设置的是什么)的延迟,对于第n个点使用n-1点的延迟和上述动画的总持续时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM