2012-12-02 152 views
1

我有一個ViewPager,我用Swype更改圖像。現在我想製作一個按鈕,它將設置用戶正在看到的當前圖像作爲壁紙。如何實現onPageSelected或類似的工作?從ViewPager設置壁紙

代碼:

package com.example.app; 

import java.io.IOException; 
import android.app.Activity; 
import android.app.WallpaperManager; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.ViewSwitcher; 
import android.view.View.OnClickListener; 
import android.widget.ViewSwitcher.ViewFactory; 

public class MainActivity extends Activity { 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 


      ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
      ImagePagerAdapter adapter = new ImagePagerAdapter(); 
      viewPager.setAdapter(adapter); 

     } 

      private class ImagePagerAdapter extends PagerAdapter { 
      private int[] mImages = new int[] { 
       R.drawable.image1, 
       R.drawable.image2, 
       R.drawable.image3, 
       R.drawable.image4 

      }; 


      @Override 
      public int getCount() { 
       return mImages.length; 
      } 

      @Override 
      public boolean isViewFromObject(View view, Object object) { 
       return view == ((ImageView) object); 
      } 

      @Override 
      public Object instantiateItem(ViewGroup container, final int position) { 
       Context context = MainActivity.this; 
       ImageView imageView = new ImageView(context); 
       int padding = context.getResources().getDimensionPixelSize(
        R.dimen.padding_medium); 
       imageView.setPadding(padding, padding, padding, padding); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
       imageView.setImageResource(mImages[position]); 
       ((ViewPager) container).addView(imageView, 0); 
       return imageView; 
      } 


      @Override 
      public void destroyItem(ViewGroup container, final int position, Object object) { 
       ((ViewPager) container).removeView((ImageView) object); 
      } 
      } 
    } 
} 

按鈕的代碼,我以前用過,但不知道現在該怎麼實現它在ViewPager:

Button button = (Button) this.findViewById(R.id.button1); 
button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());    
           try { 
       myWallpaperManager.setResource(mImages[position]); 
         Toast.makeText(MainActivity.this, "Wallpaper ok!", Toast.LENGTH_SHORT).show(); 
       } catch (IOException e) { 
        Toast.makeText(MainActivity.this, "Wallpaper error!", Toast.LENGTH_SHORT).show(); 
        } 
      } 
     }); 

回答

1

您可以像使用

viewPager.setOnPageChangeListener(new OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int arg0) { 
      // TODO Auto-generated method stub 
      //Here you can set the wallpaper 
        curruntPosition=arg0; 

     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 

然後

Button button = (Button) this.findViewById(R.id.button1); 
button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());    
           try { 
       myWallpaperManager.setResource(mImages[curruntPosition]); 
         Toast.makeText(MainActivity.this, "Wallpaper ok!", Toast.LENGTH_SHORT).show(); 
       } catch (IOException e) { 
        Toast.makeText(MainActivity.this, "Wallpaper error!", Toast.LENGTH_SHORT).show(); 
        } 
      } 
     }); 

,不要忘了聲明一個全局變量

int curruntPosition=0; 
+0

我不唱詩班知道如何實現這就是你在代碼中寫道:我張貼。如果您將代碼粘貼到原始代碼的正確位置,我將不勝感激。提前致謝! –

+0

在這行下添加上面的代碼'viewPager.setAdapter(adapter);'你真的是程序員?它和你的名字一樣簡單:) –

+0

謝謝先生。唯一的問題是,onClick方法不工作,因爲它應該在onPageSelected內。 –