我的项目中有一个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());
};
}
答案 0 :(得分:0)
使用
获取WallpaperManagerWallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
要获取imageView位图并设置为墙纸,请使用
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
wallpaperManager.setBitmap(bitmap);
或者,如果您有图像资源ID(如代码示例中所示),则可以直接使用
wallpaperManager.setResource(imageAdapter.mThumbIds[position]);