为什么animationDrawable.start();不能在onCreate 里执行

rabbitinhere 2013-03-06 09:52:50
或onStart或onResume也不行。
求解。
...全文
162 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jack_Kee 2013-10-08
  • 打赏
  • 举报
回复
public void start () Added in API level 1 Starts the animation, looping if necessary. This method has no effect if the animation is running. Do not call this in the onCreate(Bundle) method of your activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged(boolean) method in your activity, which will get called when Android brings your window into focus.
rabbitinhere 2013-03-06
  • 打赏
  • 举报
回复
animationDrawable是一个AnimationDrawable(废话……) 定义如下: imageView.setBackgroundResource(R.drawable.anim_list); animationDrawable=(AnimationDrawable)imageView.getBackground(); ---------------放在一个监听器就能播放动画-------------------------------- imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub animationDrawable.start(); } }); ---------------放在一个onCreate里就不行-------------------------------- @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); animationDrawable.start(); }
rabbitinhere 2013-03-06
  • 打赏
  • 举报
回复
呀喝,你也喜欢有码的?
xn4545945 2013-03-06
  • 打赏
  • 举报
回复
无码无真相。
Android之泡泡效果bubble package com.ray.bubble; import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnTouchListener; import android.widget.FrameLayout; import android.widget.ImageView; public class BubbleExplosion extends Activity { private FrameLayout fl; private ExplosionView exv1; private AnimationDrawable exa1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set full screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); fl = new FrameLayout(this); fl.setBackgroundResource(R.drawable.bg); exv1 = new ExplosionView(this); exv1.setVisibility(View.INVISIBLE); exv1.setBackgroundResource(R.anim.explosion); exa1 = (AnimationDrawable) exv1.getBackground(); fl.addView(exv1); fl.setOnTouchListener(new LayoutListener()); setContentView(fl); } class ExplosionView extends ImageView { public ExplosionView(Context context) { super(context); } // handle the location of the explosion public void setLocation(int top, int left) { this.setFrame(left, top, left + 40, top + 40); } } class LayoutListener implements OnTouchListener { public boolean onTouch(View v, MotionEvent event) { // first u have to stop the animation,or if the animation // is starting ,u can start it again! exv1.setVisibility(View.INVISIBLE); exa1.stop(); float x = event.getX(); float y = event.getY(); exv1.setLocation((int) y - 20, (int) x - 20); exv1.setVisibility(View.VISIBLE); exa1.start(); return false; } } }
package com.alva.vcd; import java.util.ArrayList; import java.util.List; import com.alva.vcd.adapter.RecorderAdapter; import com.alva.vcd.pojo.Recorder; import com.alva.vcd.view.AudioRecorderButton; import com.alva.vcd.view.AudioRecorderButton.AudioFinishRecorderListener; import com.alva.vcd.view.MediaManager; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.graphics.drawable.AnimationDrawable; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ListView; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity implements AudioFinishRecorderListener, OnItemClickListener { private ListView mListview; private AudioRecorderButton mAudioButton; private ArrayAdapter mAdapter; private List mDatas = new ArrayList();; private View mAnimView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); initViews(); initDatas(); initEvents(); } private void initViews() { mListview = (ListView) findViewById(R.id.listview_chat); mAudioButton = (AudioRecorderButton) findViewById(R.id.recorderButton); } private void initDatas() { mAudioButton.setAudioFinishRecorderListener(this); mAdapter = new RecorderAdapter(getApplicationContext(), mDatas); mListview.setAdapter(mAdapter); } private void initEvents() { mListview.setOnItemClickListener(this); } @Override public void onFinsh(float seconds, String filePath) { Recorder mRecorder = new Recorder(); mRecorder.setFilePath(filePath); mRecorder.setTime(seconds); mDatas.add(mRecorder); mAdapter.notifyDataSetChanged(); mListview.setSelection(mDatas.size() - 1); } @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { Log.e("MediaManager", mDatas.get(arg2).getFilePath() + "sssss"); if (mAnimView != null) { mAnimView.setBackgroundResource(R.drawable.adj); mAnimView = null; } // 播放动画 mAnimView = arg1.findViewById(R.id.id_view); mAnimView.setBackgroundResource(R.drawable.play_anim); AnimationDrawable anim = (AnimationDrawable) mAnimView.getBackground(); anim.start(); // 播放音频 MediaManager.playSound(mDatas.get(arg2).getFilePath(), new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { mAnimView.setBackgroundResource(R.drawable.adj); } }); } @Override protected void onPause() { super.onPause(); MediaManager.pause(); } @Override protected void onResume() { super.onResume(); MediaManager.resume(); } @Override protected void onDestroy() { super.onDestroy(); MediaManager.release(); } }

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧