如何将图像设置为viewpager中的壁纸?

时间:2014-03-27 10:27:54

标签: android android-viewpager wallpaper

我有这个代码用于在一些带有viewpager的图片之间滑动

imageadapter.java

package com.example.gallery3;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.pic6,
        R.drawable.pic7,
        R.drawable.pic8,
        R.drawable.pic9,
        R.drawable.pic10,
        R.drawable.pic11,
        R.drawable.pic12,
        R.drawable.pic13,
        R.drawable.pic14,
        R.drawable.pic15,
        R.drawable.pic16,
        R.drawable.pic17,
        R.drawable.pic18,
        R.drawable.pic19,
        R.drawable.pic20,

        R.drawable.pic22,

        R.drawable.pic24,
        R.drawable.pic25,
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}

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

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

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

我的MainActivity是

package com.example.gallery3;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);}}

我有一个按钮,我希望当我点击此按钮时,每个图像都在viewpager上设置为壁纸

请帮助我

1 个答案:

答案 0 :(得分:1)

这是代码

<强> MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
    Button b;
private int[] GalImages = new int[] { R.drawable.pic1, R.drawable.pic2,
        R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
        R.drawable.pic7, R.drawable.pic8, R.drawable.pic9,
        R.drawable.pic10, R.drawable.pic11, R.drawable.pic12,
        R.drawable.pic13, R.drawable.pic14, R.drawable.pic15,
        R.drawable.pic16, R.drawable.pic17, R.drawable.pic18,
        R.drawable.pic19, R.drawable.pic20,

        R.drawable.pic22,

        R.drawable.pic24, R.drawable.pic25, };

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

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this, GalImages);
    viewPager.setAdapter(adapter);
    viewPager.setOnPageChangeListener(new MyPageChangeListener());
            b = (Button) findViewById(R.id.btn);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                   int id=ListOfID[indexOfImage];
                   WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
                            myWallpaperManager.setResource(id);
        }
    });
}

private int indexOfImage = 0;

private class MyPageChangeListener extends
        ViewPager.SimpleOnPageChangeListener {
    @Override
    public void onPageSelected(int position) {
        indexOfImage = position;
    }
  }
}

<强> ImageAdapter.java

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
Context context;
int[] GalImages;

ImageAdapter(Context context, int[] GalImages) {
    this.context = context;
    this.GalImages = GalImages;
}

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

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

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

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

OnButtonClick编写以下函数

int id=ListOfID[indexOfImage];
WallpaperManager myWallpaperManager= WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setResource(id);