如何设置从ImageView的壁纸 [英] How to set wallpaper from imageview
问题描述
我仍然无法找到设置壁纸,图像从ImageView的获取方式。 任何人都可以告诉我,从机器人的ImageView设为手机壁纸的方式吗?
I am still can't find the way to set wallpaper that the image getting from ImageView. Anyone can show me the way to set phone wallpaper from android ImageView?
下面是我的code:
<ImageView
android:contentDescription="My Wallpaper"
android:id="@+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:saveEnabled="true"
/>
的图像查看活动
请注意,图像显示在 FullImageActivity
从图像获取的GridView
用户点击之后。
Activity on Image View
Please noted that the image that showing in FullImageActivity
is getting from image GridView
after user clicked on item.
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
我想在屏幕上的用户触摸后设置从图像中上述活动的壁纸,我会弹出询问用户是否希望将其设置为墙纸或不。
I want to set wallpaper from image in above activity after user touch on screen i will popup to ask user whether they want to set it as wallpaper or not.
推荐答案
我已经张贴不管我使用在我的应用我的code。我也把图像从的GridView
,当被选择的图像任何人。这将设置为墙纸。
I've posted my code whatever i'm using in my application. I've also take the image from GridView
and, when anyone of the image is being selected. That'll set as wallpaper.
但是,我的code似乎只是不同而已。我从来没有使用过任何的ImageView
But, my code seems just different. I never used any ImageView
MainActivity.this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageAdapter i = (ImageAdapter)parent.getAdapter();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return mFullSizeIds[position];
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 250));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.wallpaper1t,
R.drawable.wallpaper2t,
R.drawable.wallpaper3t,
R.drawable.wallpaper4t,
R.drawable.wallpaper5t,
R.drawable.wallpaper6t,
R.drawable.wallpaper7t,
R.drawable.wallpaper8t
};
private Integer[] mFullSizeIds = {
R.drawable.wallpaper1,
R.drawable.wallpaper2,
R.drawable.wallpaper3,
R.drawable.wallpaper4,
R.drawable.wallpaper5,
R.drawable.wallpaper6,
R.drawable.wallpaper7,
R.drawable.wallpaper8
};
}
的main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
也许,这可以帮助你很多。
Maybe, this can be helps you lot.
这篇关于如何设置从ImageView的壁纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!