Android 应用壁纸功能未实现;不知道为什么?
Android Application wallpaper functionality not implemented; not sure why?
我正在尝试按照几个不同的教程来制作一个 ViewPager
让用户滑动图像然后将当前查看的图像设置为他们的墙纸。到目前为止,我还没有成功设置壁纸,并且当用户滑动浏览照片时,性能出现问题,性能急剧下降。
public class MainActivity extends AppCompatActivity {
ViewPager mViewPager;
int[] mImages = {R.drawable.example__1_, R.drawable.example__2_, R.drawable.example__3_,
R.drawable.example__4_, R.drawable.example__5_, R.drawable.example__6_,
R.drawable.example__7_, R.drawable.example__8_, R.drawable.example__9_,
R.drawable.example__10_, R.drawable.example__11_, R.drawable.example__12_};
item mViewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewPagerMain);
mViewPagerAdapter = new item(MainActivity.this, mImages);
mViewPager.setAdapter(mViewPagerAdapter);
final int set = mViewPager.getCurrentItem();
Button button = findViewById(R.id.SetWall);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Setting Wallpaper... 1/2", Toast.LENGTH_SHORT).show();
WallpaperManager wpm
= WallpaperManager.getInstance(getApplicationContext());
try {
Toast.makeText(MainActivity.this,
"Setting Wallpaper... 2/2", Toast.LENGTH_SHORT).show();
wpm.setResource(set);
Toast.makeText(MainActivity.this,
"Wallpaper Set!", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
Toast.makeText(MainActivity.this,
"Error", Toast.LENGTH_SHORT).show();
}
}
});
}
你在你的Manifest.xml文件中设置权限了吗?
<uses-permission android:name="android.permission.SET_WALLPAPER" />
这是因为您的页面每次离开屏幕时都会被销毁并重新创建,这种情况发生在您滑动到下一个屏幕时。
为了使 ViewPager 更流畅使用 mViewPager.setOffscreenPageLimit(NumberOfPages-1);
设置适配器后 (mViewPager.setAdapter();
)
我正在尝试按照几个不同的教程来制作一个 ViewPager
让用户滑动图像然后将当前查看的图像设置为他们的墙纸。到目前为止,我还没有成功设置壁纸,并且当用户滑动浏览照片时,性能出现问题,性能急剧下降。
public class MainActivity extends AppCompatActivity {
ViewPager mViewPager;
int[] mImages = {R.drawable.example__1_, R.drawable.example__2_, R.drawable.example__3_,
R.drawable.example__4_, R.drawable.example__5_, R.drawable.example__6_,
R.drawable.example__7_, R.drawable.example__8_, R.drawable.example__9_,
R.drawable.example__10_, R.drawable.example__11_, R.drawable.example__12_};
item mViewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewPagerMain);
mViewPagerAdapter = new item(MainActivity.this, mImages);
mViewPager.setAdapter(mViewPagerAdapter);
final int set = mViewPager.getCurrentItem();
Button button = findViewById(R.id.SetWall);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Setting Wallpaper... 1/2", Toast.LENGTH_SHORT).show();
WallpaperManager wpm
= WallpaperManager.getInstance(getApplicationContext());
try {
Toast.makeText(MainActivity.this,
"Setting Wallpaper... 2/2", Toast.LENGTH_SHORT).show();
wpm.setResource(set);
Toast.makeText(MainActivity.this,
"Wallpaper Set!", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
Toast.makeText(MainActivity.this,
"Error", Toast.LENGTH_SHORT).show();
}
}
});
}
你在你的Manifest.xml文件中设置权限了吗?
<uses-permission android:name="android.permission.SET_WALLPAPER" />
这是因为您的页面每次离开屏幕时都会被销毁并重新创建,这种情况发生在您滑动到下一个屏幕时。
为了使 ViewPager 更流畅使用
mViewPager.setOffscreenPageLimit(NumberOfPages-1);
设置适配器后 (mViewPager.setAdapter();
)