setBackgroundResource doesn't set the image

前端 未结 6 1578
青春惊慌失措
青春惊慌失措 2020-12-22 07:44
    Handler hnd = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        int id = sequence.get(msg.arg1);

        if(msg.arg1 % 2 == 0         


        
6条回答
  •  太阳男子
    2020-12-22 08:35

    I would suggest you to use a handler

    int drawablebkg[] ={R.drawable.ic_launcher,R.drawable.icon};
    Handler m_handler;
    Runnable m_handlerTask ; 
    ImageView iv;
    iv = (ImageView)findViewById(R.id.imageView1);
    m_handler = new Handler(); 
       m_handlerTask = new Runnable()
        {
             @Override 
             public void run() {
                 iv.setImageResource(android.R.color.transparent); 
                if(i<2)
                {
    
                  ivsetBackgroundResource(drawablebkg[i]);
                  i++;
                }
                else 
                  {
                      i=0;
                  }
                  m_handler.postDelayed(m_handlerTask, 2000);
             }
        };
        m_handlerTask.run();  
    

    In onPause() of your activity

     @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    
        //_t.cancel();
        m_handler.removeCallbacks(m_handlerTask);
    }  
    

    Another way

        iv= (ImageView)findViewById(R.id.imageView1);
        AnimationDrawable animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.ic_launcher), 2000);
        iv.setImageResource(android.R.color.transparent); 
        animation.addFrame(getResources().getDrawable(R.drawable.icon), 2000);
    
        animation.setOneShot(false);
    
        iv.setBackgroundDrawable(animation);
        //set setBackgroundDrawable(animation) is decprecreated i guess. not sure in which api
    
        // start the animation!
        animation.start();
    

    Another way

    Define background.xml in drawable folder

    
    
    
    
    
    

    I your activity onCreate();

    ImageView iv = (ImageView)findViewById(R.id.imageView1);
    iv.setBackgroundResource(R.drawable.background);
    
    AnimationDrawable animation= (AnimationDrawable)loadingRaven.getBackground();
    loadingRaven.setImageResource(android.R.color.transparent); 
    animation.start();
    

    Note to stop the animation you need to call animation.stop()

提交回复
热议问题