public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, OnItemSelectedListener {
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private ImageAdapter ia;
private int res_id;
private Bitmap mBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
ia = new ImageAdapter(this);
gallery.setAdapter(ia);
//Event listener
gallery.setOnItemSelectedListener(this);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_save:
mBitmap = BitmapFactory.decodeResource(getResources(),res_id);
String filename = "barca_"+res_id+".jpg";
File sd = Environment.getExternalStorageDirectory();
String full_path=sd.getAbsolutePath()+"/BarcaWallpapers/";
File temp=new File(full_path);
temp.mkdirs();
File dest = new File(temp,filename);
try {
FileOutputStream out = new FileOutputStream(dest);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
Toast.makeText(this, "Saved in Folder "+full_path, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
}
return true;
case R.id.menu_set_wallpapaer:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try{
wallpaperManager.setResource(res_id);
Toast.makeText(this,"Wallpaper Set", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(this, "error setting wallpaper", Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int imageResourceId = (Integer) ia.getItem(position);
res_id=imageResourceId;
imageSwitcher.setImageResource(imageResourceId);
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}
private class ImageAdapter extends BaseAdapter {
private Context context;
private ImageAdapter(Context context) {
this.context = context;
}
private int[] IMAGE_IDS = {
R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
};
public int getCount() {
return IMAGE_IDS.length;
}
public Object getItem(int position) {
return IMAGE_IDS[position];
}
public long getItemId(int position) {
return IMAGE_IDS[position];
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(120,120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(IMAGE_IDS[position]);
return imageView;
}
}
}
我有壁纸应用程序的上述代码。活动 xml 布局文件包含一个画廊视图和其下方的图像切换器,用户可以在其中浏览和导航画廊,并且相应的图像在图像切换器下方打开。然后用户可以将图像设置为墙纸或保存到 SD 卡。上面的代码非常适用于小图像,但对于像我打算在应用程序中使用的大图像,则失败得很惨。应用程序在内存不足时崩溃(openglrenderer 内存不足)。我该如何解决?请帮助我,我在这个问题上花了很多小时。谢谢