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

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

问题描述:

我有一个应用程序包含使用ImageView和ViewPager不同的图像。我想通过ImageView将当前图像设置为Wallpaper。如何使用ViewPager将当前ImageView图像设置为WallPpaper?

但是,当我试图设置为壁纸时,它只会将第一张图像作为壁纸和剩余图像,使手机屏幕变黑(意味着它会使壁纸变黑),而不会将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); 
    } 


} 
+0

请仔细阅读[在什么情况下我想补充“紧急”或其他类似的短语我的问题,为了获得更快的答案?](// meta.stackoverflow.com/q/326569) - 总结是,这不是解决志愿者问题的理想方式,而且可能对获得答案起反作用。请不要将这添加到您的问题。 – halfer

您需要设置将当前图像发送给墙纸管理员。

执行以下改变

  1. 定义在BabyBoys活动你的图像[]
  2. 获取viewPager在setWall功能 INT currentImagePos = viewPager.getCurrentItem当前位置();
  3. 不是

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

使用

myWallpaperManager.setResource(images[currentImagePos]);

+0

我删除了+符号,但问题仍然存在(在删除+符号之前或之后没有变化。请帮助我。请将您的问题告诉我们。ANUJ GUPTA –

+0

请帮助我 –

+0

查看更新代码 –