0

我在 viewpager 中滑动 gridview 图像,我想将 viewpager 当前显示的图像设置为墙纸。我试过这个:但得到错误“不幸停止”

// set wallpaper on button click
        Button b1 = (Button) findViewById(R.id.wll);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v2) {
                // // TODO Auto-generated method stub

                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());
                try {
                    myWallpaperManager.setResource(R.id.view_pager);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

完整代码:

SwipeActivity.java

package com.td.gridview;

import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

public class SwipeActivity extends Activity {

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

        // get intent data
        Intent i = getIntent();

        // Selected image id
        final int position = i.getExtras().getInt("id");

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position); 

        // send image to full_view.xml screen on button click
        Button b = (Button) findViewById(R.id.xoom);
        b.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // // TODO Auto-generated method stub

                // get intent data
                Intent i3 = getIntent();

                // Selected image id
                int position = i3.getExtras().getInt("id");
                // Sending image id to FullScreenActivity
                Intent i2 = new Intent(getApplicationContext(), Full_Zoom.class);
                // passing array index
                i2.putExtra("id", position);
                startActivity(i2);
            }
        });
        //

        // set wallpaper on button click
        Button b1 = (Button) findViewById(R.id.wll);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v2) {
                // // TODO Auto-generated method stub

                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());
                try {
                    myWallpaperManager.setResource(R.id.view_pager);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
    }

    public class ImagePagerAdapter extends PagerAdapter {
        int[] icons = MainActivity.ICONS;

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = SwipeActivity.this;
            ImageView imageView = new ImageView(context);
            // int padding = context.getResources().getDimensionPixelSize(
            // R.dimen.padding_large);
            // imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setImageResource(icons[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

而且我找不到任何将viewpager图像设置为墙纸的方法,所以请在这里帮助我。

答案将得到充分的帮助,因此请尝试给出答案,而不仅仅是将我传递给其他链接。

4

2 回答 2

0

我得到了答案

package com.td.gridview;

import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
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.widget.Button;
import android.widget.ImageView;

public class SwipeActivity extends Activity {

    protected int curruntPosition;

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

        // get intent data
        Intent i = getIntent();

        // Selected image id
        final int position = i.getExtras().getInt("id");

        final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position);
        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

            }

        });

        // set wallpaper on button click
        Button b1 = (Button) findViewById(R.id.wll);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v2) {
                // // TODO Auto-generated method stub

                //

                //

                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());
                try {
                    myWallpaperManager
                            .setResource(MainActivity.ICONS[curruntPosition]);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

    }

    //

    //

    public class ImagePagerAdapter extends PagerAdapter {
        int[] icons = MainActivity.ICONS;

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = SwipeActivity.this;
            ImageView imageView = new ImageView(context);
            // int padding = context.getResources().getDimensionPixelSize(
            // R.dimen.padding_large);
            // imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setImageResource(icons[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

}
于 2014-07-10T17:27:11.983 回答
0

您需要获取当前所选页面/图像的位置。你可以通过这样的自定义来获得它OnPageChangeListener

public class MyOnPageChangeListener extends OnPageChangeListener {

    private int currentPosition = 0;

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
    }

    public final int getCurrentPosition () {
        return currentPosition ;
    }
}

将此类复制到您的SwipeActivity. 然后创建一个MyOnPageChangeListener对象并将其设置在 viewPager 上:

...
// create MyOnPageChangeListener
final MyOnPageChangeListener mListener = new MyOnPageChangeListener();
...
// set MyOnPageListener on the viewPager
viewPager.setOnPageChangeListener(mListener);
...

如果您需要当前职位,您只需致电getCurrentPosition

mListener.getCurrentPosition();

费:

b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v2) {

        WallpaperManager myWallpaperManager = WallpaperManager
                   .getInstance(getApplicationContext());
        try {
            myWallpaperManager.setResource(icons[mListener.getCurrentPosition()]);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
});
于 2014-07-09T13:19:56.977 回答