0

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

这是完整的代码:

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

}
4

1 回答 1

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>
于 2013-01-06T00:31:47.610 回答