Android custom dialog to achieve dynamic text loading effect _android

Source: Internet
Author: User

Before the technical quiz above see a question "Loading ..." The back three points are dynamic, so an effect is achieved. Think about it, as if I didn't think of a good way to deal with it.
Try it out in one of the stupidest ways. Let's take a look at the effect:

I was customizing a dialog, loading the effect, is within the dialog implementation, progress or from the activity inside control.
The following is the dialog implementation class:

public class Customdialog extends Alertdialog {public Customdialog (context) {super);
 Private TextView tv_loading;

 Private ProgressBar ProgressBar;
 private timer timer;

 private int count = 1;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (r.layout.dialog_progress);
  tv_loading = (TextView) Findviewbyid (r.id.tv_loading);

  ProgressBar = (ProgressBar) Findviewbyid (R.ID.PB);
  Sets the width of the dialog display, displaying d = GetWindow (). Getwindowmanager (). Getdefaultdisplay ();
  Windowmanager.layoutparams LP = GetWindow (). GetAttributes ();
  This is set to the screen width of 80% lp.width = (int) (D.getwidth () * 0.8);

  GetWindow (). SetAttributes (LP);
  Timer = new timer ();
   Timer.schedule (New TimerTask () {@Override public void run () {handler.sendemptymessage (0);
  }, 300, 300); Setondismisslistener (New Ondismisslistener () {@Override public void Ondismiss (Dialoginterface dialog) {if (Tim Er!= null) {timer.cancel ();
 }
   }
  });
   } Handler Handler = new Handler () {@Override public void Handlemessage (msg) {count++;
   if (Count > 3) {count = 1;
     Switch (count) {case 1:tv_loading.settext ("in load.");
    Break
     Case 2:tv_loading.settext ("in Load ...");
    Break
     Case 3:tv_loading.settext ("Loading ...");
   Break

 }
  }
 };
  public void setprogress (int progress) {progressbar.setprogress (progress);
  if (progress = =) {This.dismiss ();

 }
 }

}

The layout file is a textview, a ProgressBar,
Dialog_progress.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: background= "@drawable/shape_dialog_bg"
 android:orientation= "vertical"
 android:padding= "10DP" >

 <textview
  android:id= "@+id/tv_loading"
  android:layout_width= "wrap_content"
  android:layout_ height= "Wrap_content"
  android:layout_margin= "20DP"
  android:text= loading ... "
  android:textsize=" 16sp "/>

 <progressbar
  android:id=" @+id/pb "
  style=" @android: Style/widget.progressbar.horizontal "
  android:layout_width=" match_parent "
  android:layout_height=" 10DP "
  android:max=
  " android:progressdrawable= "@drawable/layer_list_progress_drawable"/>


</LinearLayout>

Because I didn't think of other ideas, I could only use timer to change the display of TextView. (Here also hope that you can enlighten the great God, at present do not think of other ideas)
ProgressBar style, the last piece of Android Custom Level progress bar of the fillet progress is described in detail, here is not repeated.
Dialog is like this. And then it's called:
Mainactivity.class

public class Mainactivity extends Fragmentactivity {private Customdialog customdialog;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);

 Customdialog = new Customdialog (this);

 private int count = 0;
  public void Tvclick (view view) {customdialog.show ();
  Final Timer timer = new timer ();
    Timer.schedule (New TimerTask () {@Override public void run () {count = 10; Runonuithread (New Runnable () {@Override public void run () {if (Customdialog!= null && customdia
      Log.isshowing ()) {customdialog.setprogress (count);
    }
     }
    });
    if (count >=) {timer.cancel ();
  }}, 0, 500); Customdialog.setondismisslistener (New Dialoginterface.ondismisslistener () {@Override public void Ondismiss (DialogIn
    Terface dialog) {if (timer!= null) timer.cancel ();
   Count = 0;

 }
  });

 }

}

This is also used as a timer to simulate the loading progress, (in the process of writing a timer to feel more convenient than the other two ways to use more easily).
Click event I was called directly through the XML.

<textview
  android:layout_width= "wrap_content"
  android:layout_height= "Wrap_content"
  android: Layout_gravity= "Center_horizontal"
  android:clickable= "true"
  android:onclick= "Tvclick"
  android: padding= "10DP"
  android:text= "click on the Pinball frame"/>

Clickable properties are not added, some mobile phone system is not able to invoke the default (previously encountered millet, do not add this property, do not trigger click event)
In addition, this type of click event is not available in fragment and can only be triggered by Setonclicklistener.

Update a way to implement:
Thanks to It-hero, and get a use of a property animation.
Here are some of the adjustments in the custom dialog:

Private string[] Scoretext = {".", "...", "..."};

Valueanimator Valueanimator;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (r.layout.dialog_progress);
  tv_loading = (TextView) Findviewbyid (r.id.tv_loading);

  ProgressBar = (ProgressBar) Findviewbyid (R.ID.PB);
  Sets the width of the dialog display, displaying d = GetWindow (). Getwindowmanager (). Getdefaultdisplay ();
  Windowmanager.layoutparams LP = GetWindow (). GetAttributes ();
  This is set to the screen width of 80% lp.width = (int) (D.getwidth () * 0.8);

  GetWindow (). SetAttributes (LP);
   if (Valueanimator = = null) {Valueanimator = Valueanimator.ofint (0, 3). setduration (1000);
   Valueanimator.setrepeatcount (Valueanimator.infinite); Valueanimator.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void ONANIMATIONUPDA
     Te (valueanimator animation) {int i = (int) animation.getanimatedvalue (); Tv_loading.settext ("in load" + scoretext[i% scoretext.length]);
  }
   });
} valueanimator.start ();

 }//code omitted ...

Because did not find csdn edit upload resources way, so there is no Demo inside the code to add this attribute animation, the need for friends can be directly from here copy.

Click to download: source

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.