2014-04-14 25 views
1

我正在创建应用程序以将图像设置为壁纸。我正在使用以下代码在每个屏幕中修复图像。代码工作正常。图像适合。但是我有一个问题,如果我玩任何游戏,然后回到主屏幕或重新启动我的设备,然后大小的壁纸缩放。我想阻止这一点。当我从我的android应用程序设置壁纸时,我希望图像大小适合第一次。如何在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]); 
    } 

在此先感谢。

回答

2

昨天我已经完成了这项任务..从图库或通过相机中获取图像,并将其设置为墙纸。 为此,我做了这个。 首先从图库或相机中获取图像。 根据您的需要进行第二次压缩或重新调整。 第三次将该图像保存在sharedpreferences中,以便即使在这种情况下也从图库或手机内存中删除图像,它也会像墙纸一样。 最后在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(); 
} 

}

呼叫从活动通过从活动的onCreate()传递字符串路径和context.like这 //图像路径

String path = ""; 
Util.setBackgroundImagePath(getApplicationContext(), path); 

此setBackgroundImagePath方法,

@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; 

} }

这里我设置背景布局,如果您有疑问 ..向 希望这可以帮助你

+0

我知道我必须使用sharedpreferences,但不知道如何保存图像sharedpreferences。请告诉我如何保存? –

+0

我将编辑我的答案 – GvSharma

+0

这段代码对我来说有点困惑。你能告诉我,我必须在代码中添加此代码吗?查看更新。 –

3

这里是代码段的工作为

的MainActivity.java Code

的BootReceiver.java开机后设置壁纸完成.. Code

And Manifest.xml用于设置权限.. Code

感谢

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

在图像就是hello(R.drawable.hello)...

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