您的位置:首页 > 编程语言 > Go语言

每隔100毫秒动态的更新控件的高度,当高度为0时,控件gone

2013-12-26 09:32 393 查看
主要思路是用一个子线程不停的往主线程的Message里面发送更新控件高度的Message,当Handler处理消息时,实时更新控件高度就行了
package cn.yyp.ironpro;
import cn.yyp.utils.DensityUtil;
import cn.yyp.utils.MainActivityUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends Activity {
private Context context;
private String posClick;
private CloseHandler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new CloseHandler();
ImageButton  guancaiButton = (ImageButton) findViewById(R.id.guancaibutton);
ImageButton  bancaiButton = (ImageButton) findViewById(R.id.bancaibutton);
ImageButton  xingcaiButton = (ImageButton) findViewById(R.id.xingcaibutton);
ImageButton  luowenButton = (ImageButton) findViewById(R.id.luowenbutton);
guancaiButton.setTag("111");
guancaiButton.setOnClickListener(new ButtonOnclickListener(1));
bancaiButton.setTag("122");
bancaiButton.setOnClickListener(new ButtonOnclickListener(2));
xingcaiButton.setTag("133");
xingcaiButton.setOnClickListener(new ButtonOnclickListener(3));
luowenButton.setTag("144");
luowenButton.setOnClickListener(new ButtonOnclickListener(4));
context = this.getApplicationContext();

Display display =getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int eleWidth = DensityUtil.dip2px(this, 60);
int margin = (width - eleWidth*4)/8;
TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.setMargins(margin, margin, margin, margin);
LinearLayout col = (LinearLayout) findViewById(R.id.col11);
col.setLayoutParams(lp);
col = (LinearLayout) findViewById(R.id.col12);
col.setLayoutParams(lp);
col = (LinearLayout) findViewById(R.id.col13);
col.setLayoutParams(lp);
col = (LinearLayout) findViewById(R.id.col14);
col.setLayoutParams(lp);
col = (LinearLayout) findViewById(R.id.col21);
col.setLayoutParams(lp);
col = (LinearLayout) findViewById(R.id.col22);
col.setLayoutParams(lp);
Log.i("mainWidth", "mainWidth:"+width+",elementWidth:"+eleWidth+"margin:"+margin);
}
private class CloseHandler extends Handler{

@Override
public void handleMessage(Message msg) {
TableRow tr = (TableRow)findViewById(R.id.row_one_hide);
GridView gv = (GridView) tr.getChildAt(0);
String height = (String)msg.getData().get("height");
if("gone".equals(height)){
tr.setVisibility(View.GONE);
LayoutParams params = gv.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
gv.setLayoutParams(params);
return;
}
LayoutParams params = gv.getLayoutParams();
params.height = Integer.valueOf(height);
gv.setLayoutParams(params);
}

}
private class ButtonOnclickListener implements OnClickListener{
private int type;

public ButtonOnclickListener(int type){
this.type = type;
}
@Override
public void onClick(View v) {
String pos = (String)v.getTag();
TableRow tr = (TableRow)findViewById(R.id.row_one_hide);
if(posClick == null || "".equals(posClick) || !pos.equals(posClick)){
if("1".equals(pos.charAt(0)+"")){
MainActivityUtils.setContent(context, tr, pos.charAt(2) + "");
tr.setVisibility(View.VISIBLE);
posClick = pos;
}
posClick = pos;
}else{
int height = 100;
try{
new CloseThread(height).start();
}catch(Throwable t){
t.printStackTrace();
}
posClick = null;
}
}
}

private class CloseThread extends Thread{
private int height;
private CloseThread(int height){
this.height = height;
}
@Override
public void run() {
while(height > 0){
Message msg = new Message();
Bundle data = new Bundle();
data.putString("height", height + "");
msg.setData(data);
handler.sendMessage(msg);
height -= 15;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Message msg = new Message();
Bundle data = new Bundle();
data.putString("height", "gone");
msg.setData(data);
handler.sendMessage(msg);
//this.stop();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息