[英]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.