PhotoView库和设置为壁纸的图像



我正在使用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" />

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

`公共类MainActivity扩展Activity{位图位图;int lastImageRef;private static最终字符串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;
    }
}  

}`

现在,在尝试捕捉中,我可以通过位置设置壁纸,但无需点击任何按钮!!当然,这不是一个好办法。有什么解决方案吗?也许是操作栏上的按钮?但是我找不到这个点上的数组位置。

您可以使用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()]);                  
            }
        });
    }

相关内容

  • 没有找到相关文章

最新更新