2013-10-22 107 views
0

我正在開發一款Android壁紙應用。我正在嘗試爲4.0或更高版本的Android設備開發它。但即使我研究了很多,閱讀了很多關於不同屏幕尺寸的scailng壁紙,我無法解決它的問題。這裏是main_activiy:Android壁紙應用屏幕尺寸

package com.abcd.iphone5sduvarkagidi; 

import static com.abcd.iphone5sduvarkagidi.HeavyLifter.FAIL; 
import static com.abcd.iphone5sduvarkagidi.HeavyLifter.SUCCESS; 
import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.Toast; 
import com.abcd.iphone5sduvarkagidi.R; 
import com.abcd.iphone5sduvarkagidi.HeavyLifter; 

public class MainActivity extends Activity { 
/** 
* A list containing the resource identifiers for all of our selectable 
* backgrounds 
*/ 
private static final List<Integer> backgrounds = new ArrayList<Integer>(); 
/** The total number of backgrounds in the list */ 
private static final int TOTAL_IMAGES; 
/** 
* Instantiate the list statically, so this will be done once on app load, 
* also calculate the total number of backgrounds 
*/ 
static { 
    backgrounds.add(R.drawable.iphone5s_1); 
    backgrounds.add(R.drawable.iphone5s_big_2_600_1024); 

    // We -1 as lists are zero indexed (0-2 is a size of 2) - we'll make use 
    // of this to implement a browsing loop 
    TOTAL_IMAGES = (backgrounds.size() - 1); 
} 

/** the state of what wallpaper is currently being previewed */ 
private int currentPosition = 0; 
/** our image wallpaper preview */ 
private ImageView backgroundPreview; 
/** 
* A helper class that will do the heavy work of decoding images and 
* actually setting the wallpaper 
*/ 
private HeavyLifter chuckNorris; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    backgroundPreview = (ImageView) findViewById(R.id.imageView1); 

    // Set the default image to be shown to start with 
    changePreviewImage(currentPosition); 

    // Load are heavy lifter (goes and does work on another thread), to get 
    // a response after the lifters thread 
    // has finished we pass in a Handler that will be notified when it 
    // completes 
    chuckNorris = new HeavyLifter(this, chuckFinishedHandler); 
} 

/** 
* Called from XML when the previous button is pressed Decrement the current 
* state position If the position is now less than 0 loop round and show the 
* last image (the array size) 
* 
* @param v 
*/ 
public void gotoPreviousImage(View v) { 
    int positionToMoveTo = currentPosition; 
    positionToMoveTo--; 
    if (positionToMoveTo < 0) { 
     positionToMoveTo = TOTAL_IMAGES; 
    } 
    changePreviewImage(positionToMoveTo); 
} 

/** 
* Called from XML when the set wallpaper button is pressed Thie retrieves 
* the id of the current image from our list It then asks chuck to set it as 
* a wallpaper! The chuckHandler will be called when this operation is 
* complete 
* 
* @param v 
*/ 
public void setAsWallpaper(View v) { 
    int resourceId = backgrounds.get(currentPosition); 
    chuckNorris.setResourceAsWallpaper(resourceId); 
} 

/** 
* Called from XML when the next button is pressed Increment the current 
* state position If the position is now greater than are array size loop 
* round and show the first image again 
* 
* @param v 
*/ 
public void gotoNextImage(View v) { 
    int positionToMoveTo = currentPosition; 
    positionToMoveTo++; 
    if (currentPosition == TOTAL_IMAGES) { 
     positionToMoveTo = 0; 
    } 

    changePreviewImage(positionToMoveTo); 
} 

/** 
* Change the currently showing image on the screen This is quite an 
* expensive operation as each time the system has to decode the image from 
* our resources - alternatives are possible (a list of drawables created at 
* app start) 
* 
* @param pos 
*   the position in {@link MainActivity#backgrounds} to select the 
*   image from 
*/ 
public void changePreviewImage(int pos) { 
    currentPosition = pos; 
    backgroundPreview.setImageResource(backgrounds.get(pos)); 
    Log.d("Main", "Current position: " + pos); 
} 

/** 
* This is the handler that is notified when are HeavyLifter is finished 
* doing an operation 
*/ 
private Handler chuckFinishedHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case SUCCESS: 
      Toast.makeText(MainActivity.this, "Duvar kağıdı ayarlandı", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     case FAIL: 
      Toast.makeText(MainActivity.this, "Bir hata oluştu", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     default: 
      super.handleMessage(msg); 
     } 
    } 
}; 

}

Here's the Heavy Lifter.java 

package com.abcd.iphone5sduvarkagidi; 

import java.io.IOException; 

import android.app.WallpaperManager; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Handler; 
import android.util.Log; 

/** 
* <b>This class uses Threads</b> 
* An alternative to this class would be to use an ASyncTask 
* @author aflazi 
* 
*/ 
public class HeavyLifter { 

public static final int SUCCESS = 0; 
public static final int FAIL = 1; 

private final Context context; 
private final Handler callback; 
private WallpaperManager manager; 

/** 
* Setup the HeavyLifter 
* @param context the context we are running in - typically an activity 
* @param callback the handler you want to be notified when we finish doing an operation 
*/ 
public HeavyLifter(Context context, Handler callback) { 
    this.context = context; 
    this.callback = callback; 
    this.manager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); 
} 

/** 
* Takes a resource id of a file within our /res/drawable folder<br/> 
* It then spawns a new thread to do its work on<br/> 
* The resource is decoded and converted to a byte array<br/> 
* This array is passed to the system which can use it to set the phones wallpaper<br/> 
* Upon completion the callback handler is sent a message with {@link HeavyLifter#SUCCESS} or {@link HeavyLifter#FAIL} 
* 
* @param resourceId id of a file within our /res/drawable/ldpi/mdpi/hdpi/xhdpi folder 
*/ 
public void setResourceAsWallpaper(final int resourceId) { 
    new Thread() { 
     @Override 
     public void run() { 
      try { 
       manager.setBitmap(getImage(resourceId)); 
       callback.sendEmptyMessage(SUCCESS); 
      } catch (IOException e) { 
       Log.e("Main", "Duvar kağıdı ayarlanamadı"); 
       callback.sendEmptyMessage(FAIL); 
      } 
     } 
    }.start(); 
} 

/** 
* Decodes a resource into a bitmap, here it uses the convenience method 'BitmapFactory.decodeResource', but you can decode 
* using alternatives these will give you more control over the size and quality of the resource. 
*/ 
private Bitmap getImage(int resourceId) { 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, null); 
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, manager.getDesiredMinimumWidth(), manager.getDesiredMinimumHeight(), true); 
    bitmap.recycle(); 
    bitmap = null; 
    return scaledBitmap; 
} 

}

我也創建了不同的佈局小,正常,大和XLARGE屏幕。我添加了不同大小的圖像到可繪製的文件夾。但是當我試圖在AVD中設置壁紙時,壁紙看起來很糟糕,而不是設備的縮放比例。請幫忙。

回答

0

this 那些使用已廢棄

而且什麼ü意思壁紙很糟糕正是佈局?它比屏幕大嗎?它是非常小的? 和你有什麼問題的設備?

+0

感謝您的回答。我要檢查佈局。我的意思是我使用的壁紙擴展到整個屏幕。在我看到的每個設備中。可以繪製-9patch修復? –

+0

對我來說,我有特定的設備相同的問題,然後我解決了這個問題,例如,如果尺寸是984X1056我把它擴大了一倍,我把它加倍到 1968X2112,它的工作完美,並適合所有屏幕 – user1283633

+0

我明白它,我會嘗試。非常感謝。 –