0
私のアプリで私はすべてを動的に取得する必要があります。私はdrawableアニメーションを動的に取得しようとしています。下のコードを試しましたが、output.pleaseを取得していないのは、私が間違いを犯した場所を助けています。Drawble animation動的に
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setLayoutParams(lp);
relativeLayout.addView(img);
strtbtn= new Button(this);
strtbtn.setId(1);
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
strtbtn.setLayoutParams(lp1);
relativeLayout.addView(stpbtn);
stpbtn=new Button(this);
stpbtn.setId(2);
RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
stpbtn.setLayoutParams(lp2);
relativeLayout.addView(stpbtn);
setContentView(relativeLayout);
BitmapDrawable frame0 = (BitmapDrawable)getResources().getDrawable(R.drawable.f0);
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.f1);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.f2);
BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.f3);
BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.f4);
BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.f5);
int reasonableDuration = 750;
mAnimation = new AnimationDrawable();
mAnimation.addFrame(frame0, reasonableDuration);
mAnimation.addFrame(frame1, reasonableDuration);
mAnimation.addFrame(frame2, reasonableDuration);
mAnimation.addFrame(frame3, reasonableDuration);
mAnimation.addFrame(frame4, reasonableDuration);
mAnimation.addFrame(frame5, reasonableDuration);
img.setBackgroundDrawable(mAnimation);
strtbtn = (Button) findViewById(1);
strtbtn.setOnClickListener(this);
stpbtn = (Button) findViewById(2);
stpbtn.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()== 3) {
mAnimation.start();
mAnimation.setOneShot(false);
}
else
mAnimation.stop();
}
}
'' findViewById(1)とは何ですか?実際のIDは数字ではなくボタンに入力する必要があります(R.id ...で始まる識別子)。また、 'v.getId()== 3'は何も意味しません。再び 'v.getId()'と 'v.getId()== R.id.btn1'のようなボタンの実際のIDを比較してください。 – Dalmas