我是android编程的新手,我正在创建一个具有动画计数器的应用程序。每次我按计数按钮计数将增加,我想根据计数
更改ImageView图像源我正在使用if条件同时我想动态地执行此操作因为我有9999最大计数因此如果条件适合我不能写很多
TextView textViewHeader;
static String value;
static int count;
static ImageButton imageButtonCountTasbeeh;
ImageView imageViewLsb,imageViewLsb1, imageViewLsb2, imageViewLsb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasbeeh_counter);
imageButtonCountTasbeeh = (ImageButton)findViewById(R.id.imageButtonCountTasbeeh);
textViewHeader = (TextView)findViewById(R.id.editTextTasbeehName);
imageButtonCountTasbeeh.setImageResource(R.drawable.imagebuttontasbeenpageselector);
imageViewLsb = (ImageView)findViewById(R.id.imageViewLsb);
imageViewLsb1 = (ImageView)findViewById(R.id.imageViewLsb1);
imageViewLsb2 = (ImageView)findViewById(R.id.imageViewLsb2);
imageViewLsb3 = (ImageView)findViewById(R.id.imageViewLsb3);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("tasbeehName");
}
textViewHeader.setText(value);
imageButtonCountTasbeeh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Hello you Just Clicked me", Toast.LENGTH_LONG).show();
count++;
if(count<10) {
int resID = getResources().getIdentifier("n" + count, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
}else if(count<20 && count>=10)
{
int count1 = count-10;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n1);
}else if(count<30 && count>=20)
{
int count1 = count-20;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n2);
}else if(count<40 && count>=30)
{
int count1 = count-30;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n3);
}else if(count<50 && count>=40)
{
int count1 = count-40;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n4);
}else if(count<60 && count>=50)
{
int count1 = count-50;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n5);
}else if(count<70 && count>=60)
{
int count1 = count-60;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n6);
}else if(count<80 && count>=70)
{
int count1 = count-70;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n7);
}else if(count<90 && count>=80)
{
int count1 = count-80;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n8);
}
}
});
}
}
答案 0 :(得分:1)
试试这个:
imageButtonCountTasbeeh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Hello you Just Clicked me", Toast.LENGTH_LONG).show();
count++;
if (count > 999)
imageViewLsb3.setImageResource(getResIdDrawableByNumber(count / 1000));
if (count > 99 ) {
int n100 = (count % 1000) / 100;
imageViewLsb2.setImageResource(getResIdDrawableByNumber(n100));
}
if (count > 9) {
int number10 = (count % 100) / 10;
imageViewLsb1.setImageResource(getResIdDrawableByNumber(number10));
}
imageViewLsb.setImageResource(getResIdDrawableByNumber(count % 10));
}
});
使用此方法按数字值获取相应的图像资源ID:
public int getResIdDrawableByNumber(int count){
return = getResources().getIdentifier("n" + count, "drawable", getPackageName());
}
希望这会有所帮助!!
答案 1 :(得分:0)
你可以通过这样的resource id
来完成这项工作:
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
在此,您必须将nameOfDrawable
替换为n1
,n2
,..,n9999
。
因此:
@Override
public void onClick(View v) {
count++;
int drawableResourceId = this.getResources().getIdentifier("n"+count, "drawable", this.getPackageName());
imageViewLsb.setImageResource(drawableResourceId);
}