2014-04-14 26 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; 
}