我正在创建将图片设置为壁纸的应用。我正在使用以下代码来修复每个屏幕中的图像。代码工作正常。图像适合。但是我有一个问题,如果我玩任何游戏然后回到主屏幕或我重新启动我的设备然后大小的壁纸缩放。我想阻止这个。当我从我的Android应用程序设置壁纸时,我希望图像大小适合第一次使用。
此处的代码 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
face = Typeface.createFromAsset(getAssets(), "fonts/ABEAKRG.TTF");
Intent i = getIntent();
position = i.getExtras().getInt("id");
full = (LinearLayout) findViewById(R.id.full);
btn = (Button)findViewById(R.id.btn);
btn.setTypeface(face);
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), mThumbId[position]);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(FullImageActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}});
changeBackground();
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
full.setOnTouchListener(activitySwipeDetector);
}
private void changeBackground(){
full.setBackgroundResource(mThumbId[position]);
}
提前致谢。
答案 0 :(得分:3)
以下是适用于该
的代码段MainActivity.java Code
BootReceiver.java用于在启动完成后设置壁纸.. Code
和Manifest.xml用于设置权限.. Code
由于
答案 1 :(得分:2)
昨天我完成了这项任务......从图库或相机中获取图像并将其设置为墙纸。 为此,我确实喜欢这个。 首先从图库或相机获取图像。 第二次根据需要压缩或重新缩放。 第三,在共享偏好中保存该图像,这样如果图像从图库或手机内存中删除,即使在这种情况下,它也将是一张墙纸。 最后在onCreate活动方法中将图像设置为墙纸。
public class Util {
public static final String PREFERENCES_NAME = "prefs";
public static SharedPreferences getSharedPreference(Context context) {
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public static String getBackgroundImagePath(Context context) {
return getSharedPreference(context).getString("imagepath","");
}
public static void setBackgroundImagePath(Context context, String path) {
Editor edit = getSharedPreference(context).edit();
edit.putString("imagepath", path);
edit.commit();
}
}
通过传递字符串路径和context.like来调用活动中的setBackgroundImagePath方法 //你的图像路径
String path = "";
Util.setBackgroundImagePath(getApplicationContext(), path);
来自活动的onCreate(),
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
face = Typeface.createFromAsset(getAssets(), "fonts/ABEAKRG.TTF");
Intent i = getIntent();
position = i.getExtras().getInt("id");
full = (LinearLayout) findViewById(R.id.full);
btn = (Button)findViewById(R.id.btn);
btn.setTypeface(face);
Bitmap path = StringToBitMap(Util.getBackgroundImagePath(getApplicationContext()));
if (path != null) {
full.setBackgroundDrawable(new BitmapDrawable((path)));
}else {
full.setBackgroundDrawable(R.drawable.defaultImage);
}
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), mThumbId[position]);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
full.setBackgroundDrawable(new BitmapDrawable((bitmap)));
String image_path = BitMapToString(bitmap);
Util.setBackgroundImagePath(getApplicationContext(),image_path);
WallpaperManager wallpaperManager= WallpaperManager.getInstance(FullImageActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}});
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
full.setOnTouchListener(activitySwipeDetector);
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
} }
这里我设置布局的背景 如果你有疑问..问 希望这有助于你
答案 2 :(得分:2)
尝试将此图像设置为壁纸
try {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(context);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int fullWidth = size.x;
int fullHeight = size.y;
// int fullWidth = wManager.getDesiredMinimumWidth();
// int fullHeight = wManager.getDesiredMinimumHeight();
Log.d("Debug", Integer.toString(fullWidth));
Log.d("Debug", Integer.toString(fullHeight));
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.hello));
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap, fullWidth,
fullHeight, true);
myWallpaperManager.suggestDesiredDimensions(
bitmapResized.getWidth(), bitmapResized.getHeight());
myWallpaperManager.setBitmap(bitmapResized);
} catch (IOException e) {
e.printStackTrace();
}
图像是你好的(R.drawable.hello)......
答案 3 :(得分:2)
前段时间我开始开发一款应用程序来自动更改壁纸。我没有你提到的问题。关键代码如下,也许它可以帮到你。
我认为唯一的区别是我在getRandomFile
中随机选择了一张壁纸。也许您可以更轻松地查看gitHub中的整个应用,虽然更改壁纸的班级是this
private void changeWallPaper(int h, int w){
String path = getRandomFile();
Bitmap bm = decodeSampledBitmapFromFile(path, w, h);
try {
WallpaperManager mywall = WallpaperManager.getInstance(this);
Log.i(MainActivity.TAG, "Setting wallpaper to " + path);
mywall.setBitmap(bm);
} catch (IOException e) {
Log.e(MainActivity.TAG, "Cannot set image as wallpaper", e);
}
}
public static Bitmap decodeSampledBitmapFromFile(String path, int width, int height) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
//String imageType = options.outMimeType;
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
/**
*
* @param options
* @param reqWidth
* @param reqHeight
* @return int
* @see http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
Log.d(MainActivity.TAG, " in sample Size: " + inSampleSize);
return inSampleSize;
}