【问题标题】:How can i make a button as Set Wallpaper button?如何将按钮设置为设置壁纸按钮?
【发布时间】:2013-01-16 23:13:10
【问题描述】:

代码包含一个带有一个按钮的图库(必须添加到您的清单文件中),我只需要关于按钮操作的建议;在 "imageIDs* 或 bitmap 中放置什么以引用之前单击并显示的图像。

完整代码如下:

package net.keivan.gallery;

import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity { 
//---the images to display---
Integer[] imageIDs = {
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.pic6,
        R.drawable.pic8,
        R.drawable.pic9,
        R.drawable.pic10,


};








/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {

            //---Displays the name of images as just as i click on them---

            Toast.makeText(getBaseContext(),
                    "pic" + (position + 1) + " selected",
                    Toast.LENGTH_SHORT).show();

            //---display the images selected---
            ImageView imageView = (ImageView) findViewById(R.id.image1);
            imageView.setImageResource(imageIDs[position]);


        }
    });
}


//---Set Wallpaper button---

public void onClick(View v) {




    try {


/*at this part can you suggest what to put inside "imageIDs* or *bitmap* to reference 
          to the image which previously is clicked and shown*/

/* first.the activity is created second.the image which i clicked is shown third.whin
           the user click on the button the image which previously is clicked on is set as background.*/




                    WallpaperManager.getInstance(this).setResource(imageIDs[position]);

                    //WallpaperManager.getInstance(this).setBitmap(mBitmap);



    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();
    }

}


///////////////////////////////////////////////

public class ImageAdapter extends BaseAdapter
{
    Context context;
    int itemBackground;

    public ImageAdapter(Context c)
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);            

        itemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);                

        a.recycle();
    }


    { getApplicationContext().getWallpaper(); }
    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }


    //---returns the item---
    public Object getItem(int position) {
        return position;
    }

     //---returns the ID of an item---
    public long getItemId(int position) {
        return position;
    }      

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));                  
        } else {
            imageView = (ImageView) convertView;
        }            
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}

}

【问题讨论】:

    标签: android gallery wallpaper


    【解决方案1】:
    try {
        // Set background from a resource
        WallpaperManager.getInstance(this).setResource(imageIDs[position]);
        // or set background from a bitmap
        //WallpaperManager.getInstance(this).setBitmap(mBitmap);
    
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    

    在您的清单文件中:

    <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
    

    【讨论】:

    • 非常感谢,非常有用且完整。
    • 关于图片还有另一个问题;正如您提到的,有两种类型可以设置背景 1.byimageIDs 2. by mBitmaps.so 如果您愿意,我需要您的建议,我该如何放置我之前在第一部分中选择的 id图像名称。提前感谢
    • 对于你分享的代码,我只能说:WallpaperManager.getInstance(this).setResource(imageIDs[position]);
    • 哦,我只是发布完整的源代码。关于按钮操作的任何建议;在“imageIDs*”或位图中放置什么以引用先前单击并显示的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多