Domanda

I am working on an Android wallpaper app. I am trying to develop it for 4.0 and higher Android devices. But even I research a lot, read a lot about scailng wallpaper for different screen sizes I could not fix the problem about it. Here is the main_activiy:

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

}

Also I created different layouts for small,normal,large and xlarge screens. I added different sized images to drawable folder. But when I tried to set wallpaper in AVDs the wallpaper looks bad, not scale for the device. Please help.

È stato utile?

Soluzione

As per this those layouts your using are deprecated

And what do u mean by the wallpaper looks bad exactly? is it larger than the screen? is it very small? and what are the devices your having problems with?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top