从ImageView获取ID并在点击时设置为壁纸

时间:2013-12-26 10:26:34

标签: android button wallpaper

我正在创建壁纸应用程序,我遇到了一个小问题。 我用图像视图和按钮设置为壁纸。但有一个问题。当我打开图片并单击设置为壁纸按钮时,我希望它从OPENED图片中获取ID并将该图片设置为wallaper。 这是我的代码

public class FullImageActivity extends Activity {
int toPhone;
ImageAdapter display;


@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]);

    Button buttonSetWallpaper = (Button)findViewById(R.id.setwallpaper);




       buttonSetWallpaper.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager
             = WallpaperManager.getInstance(getApplicationContext());
            try {
             myWallpaperManager.setResource(R.drawable.pic_1);
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }

        }
    });
}

1 个答案:

答案 0 :(得分:1)

我认为你应该从imageview获取位图并使用该位图来设置壁纸。就像这样: -

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

Button buttonSetWallpaper = (Button)findViewById(R.id.setwallpaper);
   buttonSetWallpaper.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        WallpaperManager myWallpaperManager
         = WallpaperManager.getInstance(getApplicationContext());
        try {
         Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
         if(bitmap!=null)
         myWallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

    }
});