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

发布于 2024-12-09 12:15:40 字数 2481 浏览 0 评论 0原文

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

我将我的可绘制对象/图像存储在一个数组中,但我无法真正理解如何以一种好的方式引用图像视图/数组中的当前图像。

如何更改大小写“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;
        }

    }
}

I'm building a simple wallpaper app where the user can browse through a selection of images and easily set it as their new background.

I'm storing my drawables/images in an array and I can't really wrap my head around how to reference the current image in the imageview/array in a good way.

How can I change the case "R.id.bSet" (at the bottom) to automatically pick the image from the array that I'm viewing and set it as the wallpaper?

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2024-12-16 12:15:40
// instead of this: 
bitmap = BitmapFactory.decodeResource(getResources(), 
                    R.drawable.i1); 
// try this:    
bitmap = BitmapFactory.decodeResource(getResources(), 
                    imgs[pos]); 
// instead of this: 
bitmap = BitmapFactory.decodeResource(getResources(), 
                    R.drawable.i1); 
// try this:    
bitmap = BitmapFactory.decodeResource(getResources(), 
                    imgs[pos]); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文