[英]set background from imageview on button click - Android
我在项目中有一个Imageview和一个Button,我希望该按钮将图像设置为主屏幕背景,但是当我按下该按钮时,它什么也没做,没有错误或强制关闭,
谢谢
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());
};
}
使用获取墙纸管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
要获取imageView位图并将其设置为墙纸,请使用
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
wallpaperManager.setBitmap(bitmap);
或者,如果您具有图像资源ID(如代码示例中所示),则可以直接使用
wallpaperManager.setResource(imageAdapter.mThumbIds[position]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.