How to set wallpaper from imageview

别说谁变了你拦得住时间么 提交于 2019-12-01 02:09:48

I've posted my code whatever i'm using in my application. I've also take the image from GridView and, when anyone of the image is being selected. That'll set as wallpaper.

But, my code seems just different. I never used any ImageView

MainActivity.this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            ImageAdapter i = (ImageAdapter)parent.getAdapter();                
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));

            WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

            try {
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return mFullSizeIds[position];
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if (convertView == null) {  
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 250));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.wallpaper1t, 
            R.drawable.wallpaper2t, 
            R.drawable.wallpaper3t, 
            R.drawable.wallpaper4t, 
            R.drawable.wallpaper5t, 
            R.drawable.wallpaper6t, 
            R.drawable.wallpaper7t, 
            R.drawable.wallpaper8t
    };

    private Integer[] mFullSizeIds = {
            R.drawable.wallpaper1, 
            R.drawable.wallpaper2, 
            R.drawable.wallpaper3, 
            R.drawable.wallpaper4, 
            R.drawable.wallpaper5, 
            R.drawable.wallpaper6, 
            R.drawable.wallpaper7, 
            R.drawable.wallpaper8
    };
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

Maybe, this can be helps you lot.

Md Abdul Gafur

You need to create a bitmap then set it as the wallpaper

            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), IMAGE_ID);
            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getApplicationContext());

            try {
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(SetWallPaper.this, "Wallpaper set",
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(SetWallPaper.this,
                        "Error setting wallpaper", Toast.LENGTH_SHORT)
                        .show();
            }

Where IMAGE_ID is the resource ID of your drawable, e.g. R.drawable.imagename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!