您好我正在尝试制作我的第一个Android应用程序,我制作了一个启动画面。但是,我尝试添加加载请在底部等待,每2秒增加一次。我不确定如何实现它,因为屏幕因为这个文本而崩溃。
final TextView loadingText = (TextView)findViewById(R.id.loading_text);
Thread splashTimer = new Thread(){
public void run(){
try{
int splashTime = 0;
while(splashTime < 6000){
sleep(100);
if(splashTime < 2000){
loadingText.setText("Loading.");
}
else if(splashTime >= 2000 && splashTime < 4000 ){
loadingText.setText("Loading..");
}else if (splashTime >= 4000){
loadingText.setText("Loading...");
}
splashTime = splashTime + 100;
}
Intent intent = new Intent(MainActivity.this,myMainScreen.class);
startActivity(intent);
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
finish();
}
}
};
splashTimer.start();
}
这是我的logcat
06-12 01:36:33.646: D/HyLog(29533): I : /data/font/config/sfconfig.dat, No such file or directory (2)
06-12 01:36:33.646: D/HyLog(29533): I : /data/font/config/dfactpre.dat, No such file or directory (2)
06-12 01:36:33.646: D/HyLog(29533): I : /data/font/config/sfconfig.dat, No such file or directory (2)
06-12 01:36:33.986: I/Adreno-EGL(29533): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: ()
06-12 01:36:33.986: I/Adreno-EGL(29533): OpenGL ES Shader Compiler Version: E031.24.00.02
06-12 01:36:33.986: I/Adreno-EGL(29533): Build Date: 01/20/14 Mon
06-12 01:36:33.986: I/Adreno-EGL(29533): Local Branch: PMH2-KK_3.5-RB1-AU61-554722-586267-set2
06-12 01:36:33.986: I/Adreno-EGL(29533): Remote Branch:
06-12 01:36:33.986: I/Adreno-EGL(29533): Local Patches:
06-12 01:36:33.986: I/Adreno-EGL(29533): Reconstruct Branch:
06-12 01:36:34.026: D/OpenGLRenderer(29533): Enabling debug mode 0
06-12 01:36:34.096: I/ActivityManager(29533): Timeline: Activity_idle id: android.os.BinderProxy@42b1e788 time:14063098
06-12 01:36:34.336: W/dalvikvm(29533): threadid=11: thread exiting with uncaught exception (group=0x41ae6e48)
06-12 01:36:34.336: E/AndroidRuntime(29533): FATAL EXCEPTION: Thread-2388
06-12 01:36:34.336: E/AndroidRuntime(29533): Process: com.sachinda.myfirstapp, PID: 29533
06-12 01:36:34.336: E/AndroidRuntime(29533): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6347)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:871)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.View.requestLayout(View.java:16472)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.View.requestLayout(View.java:16472)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.View.requestLayout(View.java:16472)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.View.requestLayout(View.java:16472)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.view.View.requestLayout(View.java:16472)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.widget.TextView.checkForRelayout(TextView.java:6817)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.widget.TextView.setText(TextView.java:3947)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.widget.TextView.setText(TextView.java:3805)
06-12 01:36:34.336: E/AndroidRuntime(29533): at android.widget.TextView.setText(TextView.java:3780)
06-12 01:36:34.336: E/AndroidRuntime(29533): at com.sachinda.myfirstapp.MainActivity$1.run(MainActivity.java:45)
06-12 01:36:36.316: I/Process(29533): Sending signal. PID: 29533 SIG: 9
答案 0 :(得分:8)
崩溃是因为您正在非UI线程添加方法(如
)上修改ui组件private void setText(final CharSequence text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.loading_text)).setText(text);
}
});
}
并修改您的代码以使用该方法
while(splashTime < 6000){
sleep(100);
if(splashTime < 2000){
setText("Loading.");
}
else if(splashTime >= 2000 && splashTime < 4000 ){
setText("Loading..");
}else if (splashTime >= 4000){
setText("Loading...");
}
splashTime = splashTime + 100;
}
答案 1 :(得分:4)
你在这里做的是更新线程中的UI元素,这是不允许的!
您需要做的是使用处理程序:
new Handler().post(new Runnable() {
@Override
public void run() {
// Code here will run in UI thread
}
});
这将解决目的。
答案 2 :(得分:0)
您好,我来晚了,但这肯定会帮助其他人,@interface SomeViewController ()
{
int timerCounter;
NSTimer *timer;
}
@property (strong, nonatomic) IBOutlet UILabel *timerLbl;
@end
@implementation SomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startCountdown];
}
- (void)startCountdown
{
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:@"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:@"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:@":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}
上提供的这个简单的库- https://github.com/rajputkapil/textviewdotsanimation 可以帮助您通过调用显示3点动画只需使用其自定义视图类github
,只需调用TextAndAnimationView
答案 3 :(得分:0)
可能对任何人都有帮助:) 我找到了 2 个解决方案如何在 TextView
结尾(“...”)中为点设置动画。
两种解决方案的共同功能
private fun getDotAnimator(
textView: TextView,
dotCount: Int,
list: List<CharSequence>
): ValueAnimator {
val valueTo = dotCount + 1
return ValueAnimator.ofInt(0, valueTo).apply {
this.interpolator = LinearInterpolator()
this.duration = textView.context.resources.getInteger(R.integer.dots_anim_time).toLong()
this.repeatCount = ObjectAnimator.INFINITE
this.repeatMode = ObjectAnimator.RESTART
addUpdateListener {
val value = it.animatedValue as? Int
/**
* Sometimes [ValueAnimator] give a corner value.
*/
if (value == null || value == valueTo) return@addUpdateListener
textView.text = list.getOrNull(value)
}
}
}
在 XML 中:
<string name="dot" translatable="false">.</string>
<integer name="dots_anim_time">1200</integer>
fun getDotsAnimator(textView: TextView?, @StringRes stringId: Int): ValueAnimator? {
val context = textView?.context ?: return null
val simpleText = context.getString(stringId)
val dotText = context.getString(R.string.dot)
val dotCount = 3
val textList = mutableListOf<String>()
for (i in 0 until dotCount + 1) {
val text = StringBuilder(simpleText).apply {
repeat(i) { append(dotText) }
}.toString()
textList.add(text)
}
return getDotAnimator(textView, dotCount, textList)
}
但它有缺点。如果在您的 XML 文件中 TextView
有 android:gravity="center"
,它看起来会很丑。因为 textList
中的文本有不同的 lenght
,也有不同的 center
。此解决方案仅用于 android:gravity="start"
。
fun getDotsSpanAnimator(textView: TextView?, @StringRes stringId: Int): ValueAnimator? {
val context = textView?.context ?: return null
val simpleText = context.getString(stringId)
val dotText = context.getString(R.string.dot)
val dotCount = 3
val resultText = StringBuilder(simpleText).apply {
repeat(dotCount) { append(dotText) }
}.toString()
val textList = mutableListOf<SpannableString>()
for (i in 0 until dotCount + 1) {
val spannable = SpannableString(resultText)
val start = resultText.length - (dotCount - i)
val end = resultText.length
val flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
spannable.setSpan(ForegroundColorSpan(Color.TRANSPARENT), start, end, flag)
textList.add(spannable)
}
return getDotAnimator(textView, dotCount, textList)
}
此解决方案适用于每个 android:gravity
。
private val dotsAnimator by lazy {
getDotsSpanAnimator(loadingText, R.string.dialog_text_loading)
}
override fun onResume() {
super.onResume()
dotsAnimator?.start()
}
override fun onPause() {
super.onPause()
dotsAnimator?.pause()
}