从ViewPager设置壁纸

时间:2012-12-02 15:58:47

标签: android eclipse gallery android-viewpager wallpaper

我有一个ViewPager,我在其中使用swype更改图像。现在我想制作按钮,将用户看到的当前图像设置为壁纸。如何实现onPageSelected或类似的工作?

代码:

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 个答案:

答案 0 :(得分: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;