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

主要思路是用一个子线程不停的往主线程的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();
		}
    	
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值