1

我有一个 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();
                    }
          }
       });
4

1 回答 1

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;
于 2012-12-02T16:11:02.127 回答