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

时间:2017-10-06 03:01:39

标签: android android-viewpager imageview android-imageview android-wallpaper

我正在使用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 个答案:

答案 0 :(得分:0)

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

进行以下更改

  1. 在BabyBoys活动中定义您的图片[]
  2. 在setWall函数中获取viewPager的当前位置 int currentImagePos = viewPager.getCurrentItem();
  3. 而不是
  4. wallpaperManager.setBitmap(viewToBitmap(viewPager,viewPager.getWidth(),viewPager.getHeight()));

    使用

    myWallpaperManager.setResource(images[currentImagePos]);