我正在创建一个应用程序,使用该应用程序我可以将应用程序内部的图像设置为手机的背景。我是这个领域的新手。我已经到处搜索并尝试修改我的代码,但它不起作用。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="25"
android:onClick="gotoPreviousImage"
android:padding="10dip"
android:text="<" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="50"
android:onClick="setAsWallpaper"
android:padding="10dip"
android:text="Set as Background" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="25"
android:onClick="gotoNextImage"
android:padding="10dip"
android:text=">" />
</LinearLayout>
我的MainActivity.java文件
package com.blundell.tutorial.ui.phone;
import static com.blundell.tutorial.util.HeavyLifter.FAIL;
import static com.blundell.tutorial.util.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.blundell.tutorial.R;
import com.blundell.tutorial.util.HeavyLifter;
public class MainActivity extends Activity {
private static final List<Integer> backgrounds = new ArrayList<Integer>();
/** The total number of backgrounds in the list */
private static final int TOTAL_IMAGES;
static {
backgrounds.add(R.drawable.background1);
backgrounds.add(R.drawable.background2);
backgrounds.add(R.drawable.background3);
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;
private HeavyLifter chuckNorris;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backgroundPreview = (ImageView) findViewById(R.id.backgroundPreview);
// Set the default image to be shown to start with
changePreviewImage(currentPosition);
chuckNorris = new HeavyLifter(this, chuckFinishedHandler);
}
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
* @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
*/
public void changePreviewImage(int pos) {
currentPosition = pos;
backgroundPreview.setImageResource(backgrounds.get(pos));
Log.d("Main", "Current position: "+pos);
}
/**
*/
private Handler chuckFinishedHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SUCCESS:
Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
break;
case FAIL:
Toast.makeText(MainActivity.this, "Uh oh, can't do that right now", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
};
}
My another Heavylifter.java file
package com.blundell.tutorial.util;
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;
/ ** * 此类使用线程 *此类的替代方法是使用ASyncTask * @author blundell * * / 公共类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 reource 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 eith {@link HeavyLifter#SUCCESS} or {@link HeavyLifter#FAIL}
*
* @param resourceId id of a file within our /res/drawable/ 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", "Cant set wallpaper");
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.
* You may need certain size resources within each of your /hdpi/ /mdpi/ /ldpi/ folders in order
* to have the quality and look you want on each different phone.
*/
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;
}
}