1

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


}
4

1 回答 1

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" />
于 2017-02-07T12:46:17.617 回答