根据“+”号,获取数字图片,数值变大变小,温度上升,降低

笨死了,学的东西全白了

用了最笨的方法实现图片切换,数值变大变小功能,分享一下,希望能帮助一些人吧

布局就不贴了:就是上下两个imageButton,用于响应事件;和5个imageView,用于显示温度,实现温度的变化。看效果图就明白了。

1、效果图:



2、将0-9的图片放入resource.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name = "temperatureNum_imgsArray">
        <item >@drawable/as1</item>
        <item >@drawable/as2</item>
        <item >@drawable/as3</item>
        <item >@drawable/as4</item>
        <item >@drawable/as5</item>
        <item >@drawable/as6</item>
        <item >@drawable/as7</item>
        <item >@drawable/as8</item>
        <item >@drawable/as9</item>
        <item >@drawable/as0</item>
    </integer-array>
</resources>



3、功能实现的方法AirConditionHomeActivity:


	private String TAG = "Demo666";
   
	ImageButton btn_temperature_plus_right; //右侧温度升降
	ImageButton btn_temperature_min_right;
	ImageView img_num_first_right;
	ImageView img_num_second_right; 
	ImageView img_num_third_right;
	ImageView img_num_points_right;
	ImageView img_num_unit_right; 
	  
	double temRightCount = 18.0;
	double tempMax = 32.0;
	double tempMin = 18.0;
	int numFirst = 0;
	int numSecond = 0;
	int numThird = 0; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.air_condition_home_activity);
       
        btn_temperature_plus_right = (ImageButton)findViewById(R.id.temperature_plus_right);
        btn_temperature_min_right = (ImageButton)findViewById(R.id.temperature_min_right);
        img_num_first_right = (ImageView)findViewById(R.id.num_first_right);
        img_num_second_right = (ImageView)findViewById(R.id.num_second_right); 
        img_num_third_right = (ImageView)findViewById(R.id.num_third_right);
        img_num_points_right = (ImageView)findViewById(R.id.num_points_right);
        img_num_unit_right = (ImageView)findViewById(R.id.num_unit_right);   
        
          
        btn_temperature_plus_right.setOnTouchListener(AirConditionHomeActivity.this);
        btn_temperature_min_right.setOnTouchListener(AirConditionHomeActivity.this); 
    }
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			switch(v.getId()){ 
			case R.id.temperature_plus_right: 
				temRightCount = temRightCount+0.5;
				getRightTempNum();
				if(temRightCount >= tempMax) {  
					Toast mToast = Toast.makeText(getApplicationContext(), "已至最高气温", Toast.LENGTH_SHORT);
					mToast.show();
				} 	
				break;
			case R.id.temperature_min_right: 
				temRightCount = temRightCount-0.5;
				getRightTempNum();
				if(temRightCount <= tempMin) {  
					Toast mToast = Toast.makeText(getApplicationContext(), "已至最低气温", Toast.LENGTH_SHORT);
					mToast.show();
				}
				break; 
			}
		}
		return false;
	}  
	
    //获取气温右侧数值位数
    public void getRightTempNum(){ 
		if(temRightCount * 10 > 100){  //计算出个位、十位急小数点后的位数,如23.5 :  200/100=2; 35/10=3;余5
			numFirst =(int)(temRightCount*10)/100 ; //温度的第一张图片位置:十位
			numSecond = (int)((temRightCount*10)-numFirst*100)/10;//温度的第二张图片位置:个位
			numThird  = (int)((temRightCount*10)-numFirst*100)%10;//温度的第三张图片位置:小数点后位
		} 
		if(temRightCount >= tempMin && temRightCount <=  tempMax){  // 18 <= temLeftCount <= 32
			switch(numFirst){ //第一位数值:1,2,3
			case 3:
				img_num_first_right.setBackgroundResource(R.drawable.as3);
				break;
			case 2:
				img_num_first_right.setBackgroundResource(R.drawable.as2);
				break;
			case 1:
				img_num_first_right.setBackgroundResource(R.drawable.as1);
				break; 
			}
			switch(numSecond){//第二位数值:1-9
			case 0:
				img_num_second_right.setBackgroundResource(R.drawable.as0);
				break;
			case 1:
				img_num_second_right.setBackgroundResource(R.drawable.as1);
				break;
			case 2:
				img_num_second_right.setBackgroundResource(R.drawable.as2);
				break;
			case 3:
				img_num_second_right.setBackgroundResource(R.drawable.as3);
				break;
			case 4:
				img_num_second_right.setBackgroundResource(R.drawable.as4);
				break;
			case 5:
				img_num_second_right.setBackgroundResource(R.drawable.as5);
				break;
			case 6:
				img_num_second_right.setBackgroundResource(R.drawable.as6);
				break;
			case 7:
				img_num_second_right.setBackgroundResource(R.drawable.as7);
				break;
			case 8:
				img_num_second_right.setBackgroundResource(R.drawable.as8);
				break;
			case 9:
				img_num_second_right.setBackgroundResource(R.drawable.as9);
				break;
			}
			switch(numThird){//第三位数值:0和0.5
			case 0:
				img_num_third_right.setBackgroundResource(R.drawable.as0);
				break;
			case 5:
				img_num_third_right.setBackgroundResource(R.drawable.as5);
				break; 
			}
		}
     
   	} 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值