PhotoView库和图像设置为壁纸

时间:2014-04-16 06:25:23

标签: java android wallpaper photoviewer

我正在使用PhotoView库来获得图像的幻灯片视图。这很简单。然后我想将壁纸设置为我想要的图像,也许点击一个按钮但我有问题!该库似乎不允许创建布局,但只允许以这种方式。

<com.ex.paper.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" />

所以我无法添加任何按钮。我需要检测正确的数组位置,并以某种方式将该图像设置为壁纸..到目前为止代码是这样的:

`public class MainActivity扩展Activity {     位图位图;     int lastImageRef;     private static final String ISLOCKED_ARG =“isLocked”;

private ViewPager mViewPager;
private static MenuItem menuLockItem;
private WallpaperManager wallpaper;

private static int[] sDrawables = { R.drawable.wallpapertwo, R.drawable.twixkatfirst, R.drawable.wallpaper,
    R.drawable.sfondo, R.drawable.wallpapertre};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
    setContentView(mViewPager);

    mViewPager.setAdapter(new SamplePagerAdapter());

    if (savedInstanceState != null) {
        boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
        ((HackyViewPager) mViewPager).setLocked(isLocked);
    }

}

static class SamplePagerAdapter extends PagerAdapter {


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

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());
        photoView.setImageResource(sDrawables[position]);
        WallpaperManager wallpaper = WallpaperManager.getInstance(container.getContext());
        /*Toast number = Toast.makeText(container.getContext(), "wallpaper number "+position, Toast.LENGTH_LONG);
        number.show();*/

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        try
        {
            wallpaper.setResource(sDrawables[position]);
        }
        catch (IOException e)
        {

        }
        return photoView;
    }

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

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

}  

}`

现在,在try catch中,我可以通过位置设置壁纸,但不需要点击任何按钮!当然,这不是一个好方法。有解决方案吗可能是动作栏中的按钮?但是那时我找不到阵列的位置。

1 个答案:

答案 0 :(得分:1)

您可以使用FrameLayout:

<FrameLayout...>
    <com.ex.paper.HackyViewPager/>
    <Button/>
<FrameLayout/>

onCreate

    setContentView(your.layout.with.frameLayout);
    mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               wallpaper.setResource(sDrawables[mViewPager.getCurrentItem()]);                  
            }
        });
    }