如何使用 ViewPager 将当前 ImageView 图像设置为 WallPpaper?

How to set current ImageView Image as WallPpaper using ViewPager?

我有一个包含使用 ImageView 和 ViewPager 的不同图像的应用程序。我想将ImageView显示的当前图片设置为墙纸

但是当我尝试设置为墙纸时,它只会将第一张图像设置为墙纸,而剩余的图像会使手机屏幕变黑(意味着它会使墙纸变黑),不会将 ImageView 中的其他图像设置为墙纸。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.testimagesbaby.BabyBoys"
tools:showIn="@layout/activity_baby_boys"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.15"
    >
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      android:id="@+id/viewPager">
</android.support.v4.view.ViewPager>

</RelativeLayout>
</LinearLayout>

Java 文件:

public class BabyBoys extends AppCompatActivity {

    ViewPager viewPager;
    private AlertDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_boys);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager) findViewById(R.id.viewPager);

        ViewPagerAdapter viewPagerAdapter= new ViewPagerAdapter(this);
        viewPager.setAdapter(viewPagerAdapter);


    }

    public static Bitmap viewToBitmap (View view, int width, int height){
        Bitmap bitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        Canvas canvas= new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_boys_tab, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_setwall) {
            setwall(); }
        else if (id == R.id.action_save) {

            }

        return super.onOptionsItemSelected(item);
    }

    public void setwall()
    {

        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            wallpaperManager.setBitmap(viewToBitmap(viewPager, viewPager.getWidth(),viewPager.getHeight()));
            Toast.makeText(this, "Sucessfully Wallpaper Set", Toast.LENGTH_LONG).show();
        } catch (IOException e){
            e.printStackTrace();
        }

    }
 }

ViewPagerAdapter 文件

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private Integer [] images= {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6,R.drawable.b7,R.drawable.b8,R.drawable.b9,R.drawable.b10,R.drawable.b11};

    public ViewPagerAdapter(Context context) {
        this.context = context;
    }

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

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


    }
    @Override
    public Object instantiateItem(ViewGroup container, int position){

        LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_layout, null);


        ImageView imageView=(ImageView) view.findViewById(R.id.imageView2);
        imageView.setImageResource(images[+position]);
        ViewPager vp= (ViewPager) container;
        vp.addView(view,0);
        return view;

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


}

您需要将当前图片设置为壁纸管理器。

进行以下更改

  1. 在 BabyBoys activity
  2. 中定义您的图像[]
  3. setWall函数获取viewPager的当前位置 int currentImagePos = viewPager.getCurrentItem();
  4. 而不是

wallpaperManager.setBitmap(viewToBitmap(viewPager,viewPager.getWidth(),viewPager.getHeight()));

使用

myWallpaperManager.setResource(images[currentImagePos]);