0

我有一個應用程序包含使用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

回答

0

您需要設置將當前圖像發送給牆紙管理員。

執行以下改變

  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

查看更新代碼 –

相關問題