我正在使用view paper
图片库,能够在滑动时看到所有图片。
在这里,我想通过点击"设置为壁纸"将图像设置为壁纸。按钮。
我可以将图像设置为壁纸成功,但图像不变
例如:R.drawable.picture3.But在运行时加载图像
转动不同的图像将显示,因此不能给这个恒定的值
R.drawable.picture3
。
如何获取显示的运行时图像ID? 试图点击"设置为壁纸"应该设置当前图像 作为壁纸。 注意:v.getId()= R.drawable.picture1在这里不起作用都给出了不同的值
以下是我的代码:
Context context;
Integer[] imageIDs = {
R.drawable.picture1,
R.drawable.picture2,
R.drawable.picture3,
R.drawable.picture4,
R.drawable.picture5,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setResource(R.drawable.picture3);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),
"Image is clicked-"+v.getBackground(), Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends PagerAdapter{
Context context;
int currentPosition;
private int[] GalImages = new int[] {
R.drawable.picture1,
R.drawable.picture2,
R.drawable.picture3,
R.drawable.picture4,
R.drawable.picture5,
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_content_inset_material);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#cccccc"
android:text="@string/set_as_wallpaper" />
</RelativeLayout>
我不想在图像适配器中的instantiateItem方法中实现此目的。
答案 0 :(得分:0)
这很简单。如果我弄错了,那么你想在点击按钮后将图像设置为壁纸。然后这就是逻辑
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
curruntPosition=arg0; //Here you can the position
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
然后
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setResource(imageIDs[curruntPosition]);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),
"Image is clicked-"+v.getBackground(), Toast.LENGTH_SHORT).show();
}
});
修改:
如果您想从viewpager
获取当前可见图像维护一个动态添加的图像列表,如imageList
然后
int currentItem =viewPager.getCurrentItem();
Drawable drawable = getResource.getDrawable(imageList[currentItem]);
Bitmap bm =((BitmapDrawable) drawable).getBitmap();