0

我正在开发一个 Android 壁纸应用程序。我正在尝试为 4.0 及更高版本的 Android 设备开发它。但即使我研究了很多,阅读了很多关于不同屏幕尺寸的 scailng 壁纸的信息,我也无法解决这个问题。这是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;
}

}

我还为小、正常、大和超大屏幕创建了不同的布局。我将不同大小的图像添加到可绘制文件夹中。但是当我尝试在 AVD 中设置壁纸时,壁纸看起来很糟糕,而不是针对设备进行缩放。请帮忙。

4

1 回答 1

0

根据这个 ,您使用的那些布局已被弃用

你说的壁纸看起来很糟糕是什么意思?它比屏幕大吗?是不是很小?您遇到问题的设备是什么?

于 2013-10-23T05:32:00.407 回答