您的位置:首页 > 移动开发 > Android开发

android中的view.post()

2016-04-20 23:24 483 查看
对于做过安卓的人应该多少都会了解view.post()的作用。

view.post(new runnable{
run(){
//
}
});

post中的代码是会在ui主线程调用的。

但是会在什么时候调用,这个我之前也不是很清楚,直到今天群里有人问了个问题,

package com.fourdworks.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

import com.fourdworks.app.activity.MainActivity;
import com.fourdworks.app.utils.MyLog;

/**
* 启动页
*
* @author Administrator
*/
public class AppStart extends Activity {
private String TAG = "AppStart";

HorizontalScrollView hs;
ImageView indexBg;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appstart);

hs = (HorizontalScrollView) findViewById(R.id.hs);
indexBg = (ImageView) findViewById(R.id.index_bg);

// 屏蔽HorizontalScrollView的触屏事件,防止用户可以手动滚动屏幕
hs.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;// 返回true代表拦截事件
}
});

hs.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
// 位移动画的距离
int distance = hs.getWidth() - indexBg.getRight();

MyLog.d(TAG, distance + "****" + hs.getWidth() + "*****"
+ indexBg.getRight());

// 创建位移动画
// TranslateAnimation animation = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, distance*1.0f/indexBg.getRight(),
// Animation.RELATIVE_TO_SELF, 0,
// Animation.RELATIVE_TO_SELF, 0,
// Animation.RELATIVE_TO_SELF, 0);

TranslateAnimation animation = new TranslateAnimation(distance,
0, 0, 0);
animation.setDuration(6000);// 动画播放时间
animation.setFillAfter(true);// 动画播放完毕停留在动画结束的状态

// 啟動位移动画
indexBg.startAnimation(animation);

// 为位移动画设置动画播放状态监听器,当动画播放完毕之后,跳转到主页面
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Intent intent = new Intent(AppStart.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
});
}
}


他的代码中,在oncreate 里面执行了 hs.post()
如果去掉 hs.post(),那动画效果就不会执行。这个时候就会产生疑问,post里面的代码是在ui主线程执行的,所以在oncreate调用view.post,效果应该是一样的啊?

int distance = hs.getWidth() - indexBg.getRight();
因为他的动画需要使用到view的width,但是在oncreate 里面,界面还没有绘制完成,view的大小还是0,所以动画没有效果

打印了下log日志,看了下 view.post 是会在on resume之后调用的,猜测view.post应该是会在界面完成绘制之后才会执行里面的方法

然后我又试了下 runOnUiThread,这个是如果是在主线程调用的话,是会马上执行的,不同于view.post

结论:view.post如果是在ui主线程中调用,那会在onresume之后执行post里面的代码

如果在ui主线程调用runOnUiThread,那会立即执行里面的内容,加不加runOnUiThread都一样

所以如果是有什么业务是需要在界面完成后马上执行的,可以试试用view.post(),例如获取view的宽高大小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  view.post runOnUiThread