发送 handle 消息 更新UI
2013-04-06 20:05
225 查看
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="方块"/>
<com.trunkbow.vitertris.MainView
android:id="@+id/mainView"
android:layout_width="229dp"
android:layout_height="wrap_content"
android:padding="2px" />
</LinearLayout>
主UI类
发送消息线程
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="方块"/>
<com.trunkbow.vitertris.MainView
android:id="@+id/mainView"
android:layout_width="229dp"
android:layout_height="wrap_content"
android:padding="2px" />
</LinearLayout>
主UI类
public class MainActivity extends Activity { MainView mainView ; Timer timer = new Timer(); private LinearLayout lv ; private TimerTask mTimerTask; private int tmp_int ; private int main_array[][] =new int [20][10] ; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Log.d("","recv msg invalidate"+msg.what); mainView.invalidate(); lv.invalidate(); super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (LinearLayout)findViewById(R.id.LinearLayout1); mainView = new MainView(this, null); mTimerTask = new TimerTask() { @Override public void run() { tmp_int ++; if(tmp_int == 20) tmp_int = 0 ; mainView.number[tmp_int][5] = 1 ; if(tmp_int > 0) mainView.number[tmp_int-1][5] = 0 ; Log.d("","send msg "+tmp_int); handler.sendEmptyMessage(tmp_int); } }; timer.schedule(mTimerTask, 1000 ,1000); } @Override protected void onDestroy() { super.onDestroy(); timer.cancel(); } }
发送消息线程
public class MainView extends View{ public static int x = 0; public static int y = 0; public static int [][] number = new int[20][10]; public int [][] type_a = new int [4][4] ; public int [][] type_b = new int [4][4] ; public MainView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.GRAY);//设置颜色 for(int i = 0 ; i< 20 ;i++){ for(int j=0 ; j< 10 ;j++){ if(number[i][j]==1){ canvas.drawRect(x+j*40,y+i*40 ,x+(j+1)*40 ,y+(i+1)*40 , paint); } } } } }
相关文章推荐
- Android 子线程发送handle更新UI遇到This message is already in use解决方法
- 获取网络软件更新信息 并发送消息 通知 handler 更新UI
- 用handmessage使子线程发送消息能够更新主线程里面的ui
- Android笔记之高德地图定位(通过开启服务发送广播用handle消息机制更新位置信息)
- Handler发送消息,周期性更新UI总结
- httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)
- 36.Android之多线程和handle更新UI学习
- Looper,handler类实现其他线程接收UI线程发送的消息
- 如何在一个Activity更新另一个Activity的UI(发送广播的方法)
- 发送消息并handle
- 发送消息并handle
- 世纪佳缘信息爬取存储到mysql,下载图片到本地,从数据库选取账号对其发送消息更新发信状态
- 【Android 非人为操作的UI更新】在onCreate()和onResume()等方法中的更新UI操作方法及获取View的相关消息
- Android:在子线程中更新UI,解析异步消息处理机制(Handler)
- 世纪佳缘信息爬取存储到mysql,下载图片到本地,从数据库选取账号对其发送消息更新发信状态
- android handle 消息发送和接受!!!!!
- Handle发送消息机制
- MFC中处理更新命令UI消息控制函数的调用
- 重新发送短信倒计时更新ui超级好用
- 子线程更新UI(非Handle)