Android-将imageview中的当前图像设置为系统壁纸

Android-将imageview中的当前图像设置为系统壁纸,android,arrays,wallpaper,Android,Arrays,Wallpaper,我正在构建一个简单的墙纸应用程序,用户可以在其中浏览选定的图像,并轻松地将其设置为新背景 我将我的绘图/图像存储在一个数组中,但我无法真正了解如何以良好的方式在imageview/数组中引用当前图像 如何更改大小写“R.id.bSet”(在底部)以自动从正在查看的阵列中拾取图像并将其设置为壁纸 package com.marcus.background; import java.io.IOException; import android.app.Activity; import andro

我正在构建一个简单的墙纸应用程序,用户可以在其中浏览选定的图像,并轻松地将其设置为新背景

我将我的绘图/图像存储在一个数组中,但我无法真正了解如何以良好的方式在imageview/数组中引用当前图像

如何更改大小写“R.id.bSet”(在底部)以自动从正在查看的阵列中拾取图像并将其设置为壁纸

package com.marcus.background;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GalleryView extends Activity implements OnClickListener {

    int pos = 0;
    int amount = 10;
    int max = amount - 1;
    int min = 0;
    Button prev, next, set;
    ImageView image;
    Bitmap bitmap;
    final int[] imgs = new int[] { R.drawable.i1, R.drawable.i2, R.drawable.i3,
            R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
            R.drawable.i8, R.drawable.i9, R.drawable.i10, };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView) findViewById(R.id.iView);
        prev = (Button) findViewById(R.id.bPrev);
        next = (Button) findViewById(R.id.bNext);
        set = (Button) findViewById(R.id.bSet);
        image.setImageResource(R.drawable.i1);
        prev.setOnClickListener(this);
        next.setOnClickListener(this);
        set.setOnClickListener(this);

    }

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.bPrev:
            if (pos > min) {
                pos--;
                image.setImageResource(imgs[pos]);
                ;
            } else {
            }
            break;
        case R.id.bNext:
            if (pos < max) {
                pos++;
                image.setImageResource(imgs[pos]);
                ;
            } else {
            }
            break;
        case R.id.bSet:

            // MAGIC GOES HERE ;)

            bitmap = BitmapFactory.decodeResource(getResources(), 
                    R.drawable.i1);         
            try {
                getApplicationContext().setWallpaper(bitmap);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // --------------- //

            break;
        }

    }
}
package com.marcus.background;
导入java.io.IOException;
导入android.app.Activity;
导入android.app.wallperManager;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ImageView;
公共类GalleryView扩展活动实现OnClickListener{
int pos=0;
整数金额=10;
int max=金额-1;
int min=0;
按钮prev,next,set;
图像视图图像;
位图;
final int[]imgs=new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,
R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,
R.drawable.i8,R.drawable.i9,R.drawable.i10,};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image=(ImageView)findViewById(R.id.iView);
prev=(按钮)findViewById(R.id.bPrev);
下一步=(按钮)findViewById(R.id.bNext);
set=(按钮)findviewbyd(R.id.bSet);
setImageResource(R.drawable.i1);
prev.setOnClickListener(此);
next.setOnClickListener(this);
set.setOnClickListener(此);
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.bPrev:
如果(位置>分钟){
pos--;
image.setImageResource(imgs[pos]);
;
}否则{
}
打破
案例R.id.B下一步:
如果(位置<最大值){
pos++;
image.setImageResource(imgs[pos]);
;
}否则{
}
打破
案例R.id.B集:
//这里有魔法;)
位图=BitmapFactory.decodeResource(getResources(),
R.可抽出式(i1);
试一试{
getApplicationContext().setWallpaper(位图);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
// --------------- //
打破
}
}
}

非常有魅力,非常感谢:)。不知道我怎么会错过它,虽然x)可以设置墙纸不裁剪??
// instead of this: 
bitmap = BitmapFactory.decodeResource(getResources(), 
                    R.drawable.i1); 
// try this:    
bitmap = BitmapFactory.decodeResource(getResources(), 
                    imgs[pos]);