gpt4 book ai didi

java - PhotoView 库和图像设置为壁纸

转载 作者:行者123 更新时间:2023-12-01 13:08:17 27 4
gpt4 key购买 nike

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

<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)类MainActivity扩展了Activity{ 位图位图; int 最后图像引用; 私有(private)静态最终字符串 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 中,我可以通过位置设置壁纸,但无需点击任何按钮!当然这不是一个好方法。有什么解决办法吗?也许是操作栏中的按钮?但我当时找不到数组位置。

最佳答案

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

关于java - PhotoView 库和图像设置为壁纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23101612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com