如果想要设置壁纸,为什么我必须重新访问图像

时间:2013-12-14 17:57:21

标签: android wallpaper

我在这里有严重的问题。我正在开发一个壁纸应用程序,其中我有drawable图像。图像随2个按钮(下一个和后一个)而变化。第3个按钮用于设置壁纸。当我使用下一个按钮更改图像时,图像会发生变化,但第三个按钮无法设置壁纸图像。这一直持续到最后一个图像和第三个按钮不起作用。如果我使用后退按钮重新访问图像,则第3个按钮开始工作。然后它正常工作,我可以设置壁纸我想要的图像。请帮我解决这个问题。

 public class Main extends Activity { 
    private ImageView hImageViewPic; 
    private Button iButton, gButton; 
  private int currentImage = 0;
   int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
   setContentView(R.layout.main);
   hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic); 
   iButton = (Button) findViewById(R.id.bnext);
   gButton = (Button) findViewById(R.id.bprev);
   //Just set one Click listener for the image
  iButton.setOnClickListener(iButtonChangeImageListener); 
  gButton.setOnClickListener(gButtonChangeImageListener); 
  }
   View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
     public void onClick(View v) { 
   //Increase Counter to move to next Image
     currentImage++;
     currentImage = currentImage % images.length; 
    hImageViewPic.setImageResource(images[currentImage]); 
   }
    };
    View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
      public void onClick(View v) { 
    //Increase Counter to move to next Image
     currentImage--; 
     currentImage = currentImage % images.length; 
     hImageViewPic.setImageResource(images[currentImage]); 

     // Here using 3rd button

    Button bSetWallpaper=(Button) findViewById (R.id.bSet);
     imageView imagePreview=(imageView) findViewById(R.id.idImageViewPic); 
     imagePreview.setOnClickListener (images [currentImage]); 
     bSetWallper.setOnClickListener (new Button.onClickListener (){ 
  @Override public void onClick (View arg0){ 
      WallpaperManager myWallpaperManager = WallpaperManager.getInstance (getApplicationContext ()); 
      try { 
          myWallpaperManager.setResource (images [currentImage]);
         } catch (IOException e) {
       e.printStackTrace (); 
     }
     }
   }); 
 } 
 }; }   

1 个答案:

答案 0 :(得分:0)

您已在

中实施了3:rd按钮
View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
      public void onClick(View v) { 

因此,如果您不点击该按钮,它就不会产生功能。

修改

替换此(您的代码):

View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
  public void onClick(View v) { 
//Increase Counter to move to next Image
 currentImage--; 
 currentImage = currentImage % images.length; 
 hImageViewPic.setImageResource(images[currentImage]); 

 // Here using 3rd button

Button bSetWallpaper=(Button) findViewById (R.id.bSet);
 imageView imagePreview=(imageView) findViewById(R.id.idImageViewPic); 
 imagePreview.setOnClickListener (images [currentImage]); 
 bSetWallper.setOnClickListener (new Button.onClickListener (){ 
@Override public void onClick (View arg0){ 
  WallpaperManager myWallpaperManager = WallpaperManager.getInstance   (getApplicationContext ()); 
  try { 
      myWallpaperManager.setResource (images [currentImage]);
     } catch (IOException e) {
   e.printStackTrace (); 
 }
 }
}); 
} 
}; }

使用此代码:

View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
  public void onClick(View v) { 
//Increase Counter to move to next Image
 currentImage--; 
 currentImage = currentImage % images.length; 
 hImageViewPic.setImageResource(images[currentImage]);     
} 
};



 // Here using 3rd button

Button bSetWallpaper=(Button) findViewById (R.id.bSet);
 imageView imagePreview=(imageView) findViewById(R.id.idImageViewPic); 
 imagePreview.setOnClickListener (images [currentImage]); 
 bSetWallper.setOnClickListener (new Button.onClickListener (){ 
@Override public void onClick (View arg0){ 
  WallpaperManager myWallpaperManager = WallpaperManager.getInstance   (getApplicationContext ()); 
  try { 
      myWallpaperManager.setResource (images [currentImage]);
     } catch (IOException e) {
   e.printStackTrace (); 
 }
 }
});
 }