handler的使用(主要更新UI)
来源:互联网 发布:手机刷基带软件 编辑:程序博客网 时间:2024/05/20 10:20
1.定义一个Handler 2.重写消息处理函数 3.发送消息 1.创建一个Handler 2.调用Handler.post(Runnable r)方法 3.Runnable运行在UI所在线程,所以可以直接调用View.invalidate()handler使用Message
//创建简单的View
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;
public class BounceView extends View {
float x = 40;
public BounceView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
x+=10;
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GREEN);
canvas.drawCircle(x, 40, 40, mPaint);
}
}
//创建Activity
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
public class TestHandler extends Activity {
protected static final int GUIUPDATEIDENTIFIER = 0x101;
Thread myRefreshThread = null;
BounceView myBounceView = null;
//1.定义一个Handler(一般更新View)
Handler myHandler = new Handler() {
//2.重写消息处理函数
public void handleMessage(Message msg) {
switch (msg.what) {
//判断发送的消息
case TestHandler.GUIUPDATEIDENTIFIER:
//更新View
myBounceView.invalidate();
break;
}
super.handleMessage(msg);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.myBounceView = new BounceView(this);
this.setContentView(this.myBounceView);
new Thread(new myThread()).start();
}
class myThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
//3.发送消息
Message message = new Message();
//发送消息与处理函数里一致
message.what = TestHandler.GUIUPDATEIDENTIFIER;
//内部类调用外部类的变量
TestHandler.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
} 利用handler.post()更新UI
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
public class TestHandler extends Activity {
private MyView myView;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this);
//创建一个Handler
mHandler = new Handler();
//调用Handler.post(Runnable r)方法
mHandler.post(new Runnable(){
@Override
public void run() {
//直接调用View.invalidate(),更新组件
myView.invalidate();
//延迟5毫秒后执行线程
mHandler.postDelayed(this, 5);
}
});
setContentView(myView);
}
class MyView extends View{
private float x = 0f;
public MyView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
x+=1;
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
canvas.drawRect(x, 40, x+40, 80, mPaint);
}
}
} 在线程里直接更新UI
//在新线程里更新UI,可以直接使用postInvalidate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
myView = new MyView(this);
this.setContentView(this.myView);
new Thread(new myThread()).start();
}
class myThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
//更新UI
myView.postInvalidate();
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
- handler的使用(主要更新UI)
- 使用Handler更新UI
- 使用Handler定时更新UI
- 使用Handler定时更新UI
- Handler: 更新UI的方法
- Handler:更新UI的方法
- Handler:更新UI的方法
- Handler:更新UI的方法
- Android Handler的使用 --- 循环执行更新UI显示
- 使用Handler更新UI出现的报错
- Android使用Handler异步更新UI
- Android使用Handler实时更新UI
- Android Handler使用详解之UI更新
- 使用Thread+Handler或runOnUiThread更新UI
- Handler的使用1 利用Handler来更新android的UI
- Handler的定义:主要接受子线程发送的数据, 并用此数据配合主线程更新UI.
- 利用Handler来更新android的UI
- 利用Handler来更新android的UI
- EditText的属性
- 基本UI主件
- 呵呵
- Intent的使用
- SQLiteOpenHelper的使用,对数据库操作的封装
- handler的使用(主要更新UI)
- 自定义Adapter
- 大数据量高并发的数据库优化
- 关于:为什么简单的一个select查询都要加上事务控制
- Adapter的使用
- 文件数据存储
- Service的使用
- 移动
- Broadcast广播的使用