动态绘制动画
在我的应用程序中,我需要动态获取所有内容。所以我试图动态获取可绘制动画。我尝试了下面的代码,但没有得到任何输出。请帮助我哪里出错了。
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();
}
}
in my apps i need to get everything dynamically. so i am trying to get drawable animation dynamically.I tried for below code but am not getting any output.please help me where i did the mistake.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里我已经举了例子尝试一下并告诉我会发生什么?
异步任务::
here i have set example try it and tell me what happen?
AsyncTask ::