从按钮点击的imageview设置背景 - Android

时间:2014-03-23 05:53:27

标签: android button imageview

我的项目中有一个Imageview和一个按钮,我希望按钮将图像设置为主屏幕背景但是当我按下按钮时,它没有做任何事情,没有错误或强制关闭,

由于

package com.example.androidhive;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

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


 public void setaswall(View view) { // SET AS WALLPAPER BUTTON
     WallpaperManager myWallpaperManager 
     = WallpaperManager.getInstance(getApplicationContext());
     };
  }

1 个答案:

答案 0 :(得分:0)

使用

获取WallpaperManager
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

要获取imageView位图并设置为墙纸,请使用

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
wallpaperManager.setBitmap(bitmap);

或者,如果您有图像资源ID(如代码示例中所示),则可以直接使用

wallpaperManager.setResource(imageAdapter.mThumbIds[position]);