lundi 13 avril 2015

Android imagechanger dependent on variable

I'm making an app which measures the dB level of sound. To support this number a image will be shown of a situation to which the sound level is comparable to. (eg a image of a highway to show sound of about 100dB).


To change the images I wrote some if-statements that change the source of the imageview.



int pic=1;

// Imageview changer //

ImageView IconView = (ImageView) findViewById(R.id.imageView1);
TextView Sound = (TextView) findViewById(R.id.dBTextView);
int Soundlvl = Integer.valueOf(Sound.getText().toString());

if( Soundlvl < 40)
{
if(pic != 1){
IconView.setImageResource(R.drawable.recycle49);
Log.i("LevelMeterActivity","****Switch image 1****");
pic=1;
}
}

else if ( Soundlvl < 60)
{
if(pic != 2){
IconView.setImageResource(R.drawable.two220);
Log.i("LevelMeterActivity","****Switch image 2****");
pic=2;
}
}

else if ( Soundlvl < 80)
{
if(pic !=3){
IconView.setImageResource(R.drawable.television4);
Log.i("LevelMeterActivity","****Switch image 3****");
pic=3;
}
}

else if ( Soundlvl < 100)
{
if(pic !=4){
IconView.setImageResource(R.drawable.highway2);
Log.i("LevelMeterActivity","****Switch image 4****");
pic=4;
}
}

else if ( Soundlvl < 120)
{
if(pic !=5){
IconView.setImageResource(R.drawable.construction);
Log.i("LevelMeterActivity","****Switch image 5****");
pic=5;

}
}

else if ( Soundlvl < 1000)
{
if(pic !=6){
IconView.setImageResource(R.drawable.impaired);
pic=6;
Log.i("LevelMeterActivity","****Switch image 6****");
}
}


The code stops working after 1 loop. I checked it without the if statements and then it works fine so the problem should be in here. Am I using the correct way to set the images? The images change quite fast so maybe that causes it to stop..


Aucun commentaire:

Enregistrer un commentaire