0

コードには 1 つのボタン (マニフェスト ファイルに追加する必要があります) を含むギャラリーが含まれています。ボタン アクションに関する提案が必要です。以前にクリックして表示された画像を参照するために、「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 に答える