0

我正在尝试制作一个墙纸应用程序,用户可以在其中滑动图像,当他找到合适的图像时,他可以将图像设置为墙纸。但是我无法在此处设置墙纸管理器,这是我的代码

自定义刷卡适配类`package com.example.neelaysrivastava.pr12;

 import android.app.WallpaperManager;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.support.v4.view.PagerAdapter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.View;
 import android.widget.ImageView;
    import android.widget.LinearLayout;
 import android.widget.Button;

 import java.io.IOException;

 public class CustomeSwipeAdapter extends PagerAdapter {
 private int [] image_resource=   {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
        R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,
        R.drawable.img14,R.drawable.img15,R.drawable.img16,
        R.drawable.img17,R.drawable.img18,R.drawable.img19,R.drawable.img20};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomeSwipeAdapter(Context ctx){
    this.ctx=ctx;
}
@Override
public int getCount() {
    return image_resource.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view==(LinearLayout)object);
}
 Bitmap goal;
@Override
public Object instantiateItem(ViewGroup container, final int position) {
    layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view =layoutInflater.inflate(R.layout.second,container,false);
    ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
    Button button = (Button)item_view.findViewById(R.id.button);
    imageView.setImageResource(image_resource[position]);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         goal= BitmapFactory.decodeResource(Resources.getSystem(),image_resource[position]);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctx.getApplicationContext());
            try {
                wallpaperManager.setBitmap(goal);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    container.addView(item_view);
    return item_view;
}

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

`

4

2 回答 2

0

放置图像,您可以使用

myWallpaperManager.setResource(0);    



myWallpapaerManager.setResource(R.drawable.yourimage)
于 2016-03-08T05:34:48.850 回答
0

你可以尝试这样的事情:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctx.getApplicationContext());
                try {
                    int height = wallpaperManager.getDesiredMinimumHeight();
                    int width = wallpaperManager.getDesiredMinimumWidth();
                    Bitmap bm = BitmapFactory.decodeResource(v.getContext().getResources(), image_resource[position]);
                    goal =  Bitmap.createScaledBitmap(bm, width, height, true); v.getContext().getResources().getDrawable(image_resource[position]);
                    wallpaperManager.setBitmap(goal);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

只是为了确保,不要忘记许可:

<uses-permission android:name="android.permission.SET_WALLPAPER" />
于 2016-03-08T05:45:23.793 回答