如何从imageview设置壁纸

时间:2012-06-11 10:31:38

标签: android android-imageview wallpaper android-gridview

我仍然无法找到从ImageView获取图片的壁纸的方法。 任何人都可以告诉我从Android ImageView设置手机壁纸的方法吗?

这是我的代码:

ImageView布局:

<ImageView
    android:contentDescription="My Wallpaper"
    android:id="@+id/full_image_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:saveEnabled="true"
    />

图像视图上的活动

请注意,FullImageActivity中显示的图片是在用户点击项目后从图片GridView获取的。

public class FullImageActivity extends Activity {

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

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}

我想在用户触摸屏幕后在上面的活动中设置壁纸我会弹出询问用户是否要将其设置为壁纸。

2 个答案:

答案 0 :(得分:2)

您需要创建一个位图,然后将其设置为壁纸

            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();
            }

其中IMAGE_ID是您的drawable的资源ID,例如R.drawable.imagename

答案 1 :(得分:1)

我发布了我的代码,无论我在我的应用程序中使用了什么。我还从GridView拍摄图像,并且当选择任何图像时。这将设置为壁纸。

但是,我的代码似乎有所不同。我从未使用任何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"/>

也许,这可以帮到你很多。