在选择图像上,设置为壁纸

时间:2017-02-07 12:37:22

标签: android android-wallpaper

我是Android开发的新手。 出于学习目的,我使用了github的应用程序。

此应用由壁纸组成,当用户滑动时会更改。

我想知道,如果有一种方法,在该图像的长按上, 用户可以将该图像设置为设备的壁纸(使用裁剪选项)。

这是我的图片视图代码。

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class ShivaGallery extends Activity implements ViewSwitcher.ViewFactory,OnItemSelectedListener {
    /** Called when the activity is first created. */
    private Gallery gallery;  
    private ImageSwitcher imageSwitcher;  
    int i=0;

    private ImageAdapter ia;   
    int[] IMAGE_IDS = {  
            R.drawable.shivji1, R.drawable.shivji2, R.drawable.shivji3, R.drawable.shivji4, R.drawable.shivji5,
            R.drawable.shivji6, R.drawable.shivji7, R.drawable.shivji8,R.drawable.shivji9,
            R.drawable.shivji10,R.drawable.shivji11, R.drawable.shivji12, R.drawable.shivji13, R.drawable.shivji14, 
            R.drawable.shivji15 
      };  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.shivagallery);  

         gallery = (Gallery) findViewById(R.id.gallery);  
         imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);  

         imageSwitcher.setFactory(this);  
         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                   android.R.anim.slide_in_left));  
         imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
                   android.R.anim.slide_out_right));  


         ia = new ImageAdapter(this);  
         gallery.setAdapter(ia);  

         //Event listener
         gallery.setOnItemSelectedListener(this);  
    }  

    private class ImageAdapter extends BaseAdapter {  
         private Context context;  

         private ImageAdapter(Context context) {  
              this.context = context;  
         }  



         public int getCount() {  
              return IMAGE_IDS.length;  
         }  

         public Object getItem(int position) {  
              return IMAGE_IDS[position];  
         }  

         public long getItemId(int position) {  
              return position;  
         }  

         public View getView(int position, View convertView, ViewGroup parent) {  
              ImageView iv = new ImageView(context);  
              iv.setImageResource(IMAGE_IDS[position]);  
              iv.setScaleType(ScaleType.FIT_XY);
             // iv.setLayoutParams(new Gallery.LayoutParams(1000, 800));

              return iv;  
         }  
    }  

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
         int imageResourceId = (Integer) ia.getItem(position);  
         imageSwitcher.setImageResource(imageResourceId);  
         i=position;
    }  

    public void onNothingSelected(AdapterView<?> parent) {  
    }  

    public View makeView() {  
         ImageView i = new ImageView(this);  
         i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
         return i;  
    }  


}

1 个答案:

答案 0 :(得分:2)

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(R.drawable.five);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}});

这里点击的代码将使其成为背景图像。对于裁剪选项,您可以使用其他打开的库或您想要执行的其他视图。

并且不要忘记许可:

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