问题
I want to set wallpaper from viewpager(selected image). I have different pictures(R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6............) How can I do, I want to be dynamic,please help me, thanks
App.class;
public class App extends Activity implements OnPageChangeListener {
protected static final Context Context = null;
private Integer[] ListOfID = {
R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14, R.drawable.a15,
R.drawable.a16, R.drawable.a17, R.drawable.a18, R.drawable.a19, R.drawable.a20};
private static final int count = 22;
//private AdController myController;
View view=null;
Integer imageId;
final Activity act = this;
private int indexOfImage = 0;
public LinearLayout layout;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
final ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(12);
myPager.setOnPageChangeListener(this);
Button next = (Button) findViewById(R.id.buttonnext);
next.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
myPager.setCurrentItem(myPager.getCurrentItem()+1);
}
});
Button previous = (Button) findViewById(R.id.buttonprevious);
previous.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
myPager.setCurrentItem(myPager.getCurrentItem()-1);
}
});
Button duvarbtn = (Button) findViewById(R.id.button1);
duvarbtn.setOnClickListener(new View.OnClickListener(){
int id=ListOfID[indexOfImage];
public void onClick(View v) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(id);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int position, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int position) {
// TODO Auto-generated method stub
indexOfImage = position;
switch (position) {
case 0://
firstpage(position);
indexOfImage = position;
break;
case 1:
indexOfImage = position;
break;
case 2:
indexOfImage = position;
break;
case 3:
indexOfImage = position;
break;
case 4:
indexOfImage = position;
break;
case 5:
indexOfImage = position;
break;
case 6:
indexOfImage = position;
break;
case 7:
indexOfImage = position;
break;
case 8:
indexOfImage = position;
break;
case 9:
indexOfImage = position;
break;
case 10:
indexOfImage = position;
break;
case 11:
indexOfImage = position;
break;
case 12:
indexOfImage = position;
break;
case 13:
indexOfImage = position;
break;
case 14:
indexOfImage = position;
break;
case 15:
indexOfImage = position;
break;
case 16:
indexOfImage = position;
break;
case 17:
indexOfImage = position;
break;
case 18:
indexOfImage = position;
break;
case 19:
indexOfImage = position;
break;
case 20:
indexOfImage = position;
break;
case 21:
firstpage(position);
indexOfImage = position;
break;
}
}
public void firstpage(int position) {
// TODO Auto-generated method stub
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
int pageCount = getCount();
if (position == 0){
//
myPager.setCurrentItem(pageCount-2,false);
} else if (position == pageCount-1){
//ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setCurrentItem(1,false);
}
}
private int getCount() {
// TODO Auto-generated method stub
return count;
}
public void onClick(View v) {
// TODO Auto-generated method stub
//Filter f = (Filter) v.getTag();
// MyPagerAdapter.show(this, input, f);
}
public void onDestroy()
{
super.onDestroy();
}
}
回答1:
pager.setOnPageChangeListener(new MyPageChangeListener());
private int indexOfImage = 0;
private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
indexOfImage = position;
}
}
Setting it as wallpaper
int id=ListOfID[indexOfImage];
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setResource(id);
来源:https://stackoverflow.com/questions/13072657/how-to-set-wallpaper-viewpager