来自Viewflipper的setWallpaper

时间:2012-09-03 02:31:02

标签: android wallpaper

我使用viewFlipper制作了一个简单的应用。现在我想要setWallpaper。但是当我按下setWallpaper按钮时,我的coad无法正常工作。这是我的代码:

public class WallpaperChooserActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */

ViewFlipper vp;
Bitmap bmp;
ImageView image;
int i;
int galleryId [] ={R.drawable.bela1,R.drawable.bela2,R.drawable.bela3,
        R.drawable.bela4,R.drawable.bela5};


private void setFlipperImage1(int res) {
       // Log.i("Set Filpper Called", res+"");
        image = new ImageView(getApplicationContext());
        image.setImageResource(res);
        vp.addView(image);
}  



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initilizeVariable();   

    for(int i=0;i<galleryId.length;i++)
    {
        setFlipperImage1(galleryId[i]);
    }



}

private void initilizeVariable() {
    // TODO Auto-generated method stub

    vp =(ViewFlipper)findViewById(R.id.ViewFlipper01);
    Button setWall = (Button)findViewById(R.id.btnSetWall);
     Button moreApp = (Button) findViewById(R.id.btnMore);
     Button share = (Button) findViewById(R.id.btnShare);
     Button rate = (Button)findViewById(R.id.btnRate);
     ImageButton next = (ImageButton)findViewById(R.id.ibNext);
     ImageButton prev = (ImageButton)findViewById(R.id.ibPrev);

     setWall.setOnClickListener(this);
    moreApp.setOnClickListener(this);
    share.setOnClickListener(this);
    rate.setOnClickListener(this);
    next.setOnClickListener(this);
    prev.setOnClickListener(this);

}


public void onClick(View v) {
    // TODO Auto-generated method stub

 switch (v.getId()){

    case R.id.ibNext:

        vp.showNext();
    break;

    case R.id.ibPrev:

        vp.showPrevious();
        break;

    case R.id.btnSetWall:

        //Bitmap wallpaper = BitmapFactory.decodeStream(getResources().openRawResource(galleryId [i]));
        WallpaperManager myWallpaperManager  = WallpaperManager.getInstance(getApplicationContext()); 

         try {

                 myWallpaperManager.setResource(galleryId [i]);


         } catch (IOException e) {
                 // TODO Auto-generated catch block
              e.printStackTrace();
         }break;

}

 }
}

1 个答案:

答案 0 :(得分:0)

编辑* - 您没有更新您的其他int i,只更新onCreate()中for循环内的那个。而不是gallery [i]它应该像gallery [currentFlipperIndex]“currentFlipper”索引当然是你的视图鳍状肢的当前索引。