每隔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
    评论
飞行器高度控件,主要应用于模拟飞行器的高度仪表,可用于模拟训练,仿真等软件界面。飞行器高度控件占用资源小,执行效率高。经测试其刷新周期小于5毫秒,均值为2毫秒。本控件提供丰富的接口。 飞行器高度控件格式为OCX。 基本参数 SetRealAltitude 设置实际飞行高度 GetRealAltitude 获取实际飞行高度 SetPlanAltitude 设置计划飞行高度 GetPlanAltitude 获取计划飞行高度 背景 SetBKMode 设置背景模式 GetBKMode 获取背景模式 SetBKColor 设置背景颜色 GetBKColor 获取背景颜色 SetBKPictureMode 设置背景贴图模式 GetBKPictureMode 获取背景贴图模式 SetBKFiltrateColor 设置背景图片过滤颜色 GetBKFiltrateColor 获取背景图片过滤颜色 SetBKPicutrePath 设置背景图片路径 GetBKPicutrePath 获取背景图片路径 边框 SetBorderWidth 设置边框宽度 GetBorderWidth 获取边框宽度 SetBorderColor 设置边框颜色 GetBorderColor 获取边框颜色 标尺 SetAutoCenter 设置是否为自动计算标尺中心坐标 SetCenter 设置自定义标尺中心坐标 GetCenter 获取标尺中心坐标 SetRulerDirection 设置标尺方向 GetRulerDirection 获取标尺方向 SetMaxMinValue 设置标尺的最大值和最小值 GetMaxMinValue 获取标尺的最大值和最小值 SetRulerStyle 设置标尺的样式 GetRulerStyle 获取标尺的样式 SetUnitAltitude 设置一个单元格代表的海拔高度 SetUnitPixel 设置一个单元格所占的像素数目 SetUnitGroup 设置一组包含单元格的数目 SetRulerDegreeWidth 设置标尺刻度线的宽度 AddIntervalColor 添加区间颜色 ClearIntervalColor 清空区间颜色 SetRulerFontStyle 设置标尺文字样式 标牌 SetSignOffset 设置标牌偏移量 SetSignSize 设置标牌大小 SetSignArrowSize 设置标牌箭头大小 SetSignBKColor 设置标牌背景颜色 SetSignBorderStyle 设置标牌边框样式 SetSignFontInterval 设置标牌文字偏移量 SetSignFontStyle 设置标牌文字样式 SetSignFontColor 设置标牌文字颜色 SetSignDigit 设置标牌文字有效数字个数 SetCartoon 设置标牌文字是否采用动画 SetCartoonFontStyle 设置动画字体样式 SetCartoonFontColor 设置动画字体颜色 SetAutoCartoonFontSize 设置是否采用用户自定义的字体高度 SetCartoonFontHeight 设置用户自定义的字体高度 计划飞行高度提示 SetDrawPlan 设置是否绘制计划飞行高度指示 SetPlanDrawText 设置是否绘制计划飞行高度指示文字 SetDrawPlanTrend 设置是否绘制计划飞行高度趋势线 SetPlanOffsetX 设置计划飞行高度指示X偏移量 SetPlanArrowSize 设置计划飞行高度指示箭头大小 SetPlanArrowStyle 设置计划飞行高度指示箭头样式 SetPlanBKMode 设置计划飞行高度指示箭头背景模式 SetPlanBKColor 设置计划飞行高度指示箭头背景颜色 SetPlanFontStyle 设置计划飞行高度指示文字样式 SetPlanFontColor 设置计划飞行高度指示文字颜色 SetPlanTextOffsetX 设置计划飞行高度指示文字X方向偏移量 SetPlanTrendOffsetX 设置计划飞行高度指示趋势线X方向偏移量 SetPlanTrendArrowSize 设置计划飞行高度指示趋势线箭头大小 SetPlanTrendArrowStyle 设置计划飞行高度指示趋势线箭头样式 注释文字 AddNoteInfo 添加注释文字信息 ClearNoteInfo 清空注释文字信息 SetNoteArrowSize 设置注释箭头大小 SetNoteArrowWidth 设置注释箭线宽度 SetNoteArrowStyle 设置注释箭线样式 SetNoteFontStyle 设置注释文字样式 SetNoteFontColor 设置注释文字颜色 SetNoteArrowOffsetX 设置注释箭线X方向偏移量 SetNoteTextOffsetX 设置注释文字X方向偏移量

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值