国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android開發教程之handler異步更新ui

2020-04-11 11:56:03
字體:
來源:轉載
供稿:網友

其實文字游戲程序很簡單,就是一個view和一個Activity,在利用下handier和postInvalidate()更新UI

調用Handler.post(Runnable r)方法,Runnable運行在UI所在線程,所以可以直接調用View.invalidate()

復制代碼 代碼如下:

packagecom.Test.androidtest;

importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.view.View;

publicclassTestHandlerextendsActivity{
privateMyViewmyView;
privateHandlermHandler;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
myView=newMyView(this);
mHandler=newHandler();
mHandler.post(newRunnable(){
@Override
publicvoidrun(){
myView.invalidate();
mHandler.postDelayed(this,5);
}
});
setContentView(myView);27}

classMyViewextendsView{30privatefloatx=0f;31publicMyView(Contextcontext){
super(context);33
}
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);37x+=1;
PaintmPaint=newPaint();
mPaint.setColor(Color.BLUE);
canvas.drawRect(x,
,x+40,80,mPaint);41}

}
}

在新線程里更新UI,可以直接postInvalidate()

復制代碼 代碼如下:

publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

myView=newMyView(this);
this.setContentView(this.myView);
newThread(newmyThread()).start();
}

classmyThreadimplementsRunnable{
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
try{
myView.postInvalidate();
Thread.sleep(100);
}catch(InterruptedExceptione){
Thread.currentThread().interrupt();
}
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郸城县| 来宾市| 洞口县| 仁怀市| 长子县| 土默特左旗| 淮南市| 临海市| 永年县| 崇明县| 鄄城县| 抚松县| 安塞县| 望江县| 土默特左旗| 绥棱县| 磐石市| 乌鲁木齐市| 辽源市| 锦州市| 融水| 安吉县| 刚察县| 桐庐县| 青龙| 龙口市| 防城港市| 西峡县| 宜良县| 和静县| 札达县| 阿合奇县| 高碑店市| 扎鲁特旗| 锡林郭勒盟| 平谷区| 临江市| 荣成市| 丁青县| 兴安县| 北流市|