基本信息
源码名称:android 图片浏览器 源码下载
源码大小:3.41M
文件格式:.zip
开发语言:Java
更新时间:2015-01-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.example.aaa; import java.io.File; import java.util.ArrayList; import java.util.List; import android.support.v7.app.ActionBarActivity; //import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Rect; //import android.graphics.drawable.BitmapDrawable; //import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.WindowManager; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.Toast; //import android.os.Build; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; //import android.database.Cursor; //import android.provider.MediaStore.Images.Media; import android.app.Dialog; import android.app.AlertDialog; public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener { protected static final Context MainActivity = null; private GalleryFlow gallory = null; private List<String> ImageList; private String filePath = null; private Bitmap bmp; private int window_width, window_height;// 控件宽度 private DragImageView dragImageView;// 自定义控件 private int state_height;// 状态栏的高度 private ViewTreeObserver viewTreeObserver; //private Cursor cursor; //******************************** private int imagePosition; private Context currentContext = this; // private int ScaleAngle; //private ImageAdapter imageAdapter; //*********************************************************************** private Animation showAction, hideAction; LinearLayout mylayout; boolean menuShowed; //************************************************************************ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); ImageList = getImagesFromSD(); this.gallory = (GalleryFlow)super.findViewById(R.id.mygallery); //this.gallory.setAdapter(new ImageGalleryAdapter(this)); if(ImageList == null || ImageList.size() == 0) return; /** * 创建适配器,注册适配器 */ //imageAdapter = new ImageAdapter(this, ImageList); gallory.setAdapter(new ImageAdapter(this, ImageList)); gallory.setOnItemSelectedListener(this); /** 获取可見区域高度 **/ WindowManager manager = getWindowManager(); window_width = manager.getDefaultDisplay().getWidth(); window_height = manager.getDefaultDisplay().getHeight(); dragImageView = (DragImageView) findViewById(R.id.div_main); if(filePath==null) { bmp = BitmapUtil.ReadBitmapById(this, R.drawable.huoying, window_width, window_height); } // 设置图片 // dragImageView.setImageBitmap(bmp); // dragImageView.setmActivity(this);//注入Activity. /** 测量状态栏高度 **/ /* viewTreeObserver = dragImageView.getViewTreeObserver(); viewTreeObserver .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (state_height == 0) { // 获取状况栏高度 Rect frame = new Rect(); getWindow().getDecorView() .getWindowVisibleDisplayFrame(frame); state_height = frame.top; dragImageView.setScreen_H(window_height-state_height); dragImageView.setScreen_W(window_width); } } }); */ //*************************************************************** mylayout = (LinearLayout) findViewById(R.id.mylayout); // 这里是TranslateAnimation动画 showAction = new TranslateAnimation( Animation.RELATIVE_TO_SELF,0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); // 这里是ScaleAnimation动画 // showAction = new ScaleAnimation( 1.0f,1.0f,0.0f,1.0f,Animation.RELATIVE_TO_SELF,0.0f, Animation.RELATIVE_TO_SELF,0.0f); showAction.setDuration(500); // 这里是TranslateAnimation动画 hideAction = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f); // 这里是ScaleAnimation动画 hideAction = new ScaleAnimation(1.0f,1.0f,1.0f,0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); hideAction.setDuration(500); menuShowed = false; //mylayout.setVisibility(View.GONE); /*mylayout.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub if (menuShowed) { menuShowed = false; mylayout.startAnimation(hideAction); mylayout.setVisibility(View.GONE); } else { menuShowed = true; mylayout.startAnimation(showAction); mylayout.setVisibility(View.VISIBLE); } } });*/ //*************************************************************** } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub /** * 获取当前要显示的Image的路径 */ // ******************************************************************** imagePosition=position; // ********************************************************************* String photoURL = ImageList.get(position); Log.i("A", String.valueOf(position)); /** * 设置当前要显示的Image的路径 */ filePath = photoURL; bmp = BitmapUtil.ReadBitmapById(this, filePath, window_width, window_height); //mSwitcher.setImageURI(Uri.parse(photoURL)); dragImageView.setImageBitmap(bmp); dragImageView.setmActivity(this);//注入Activity. /** 测量状态栏高度 **/ viewTreeObserver = dragImageView.getViewTreeObserver(); viewTreeObserver .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (state_height == 0) { // 获取状况栏高度 Rect frame = new Rect(); getWindow().getDecorView() .getWindowVisibleDisplayFrame(frame); state_height = frame.top; dragImageView.setScreen_H(window_height-state_height); dragImageView.setScreen_W(window_width); } } }); } private List<String> getImagesFromSD() { List<String> imageList = new ArrayList<String>(); File f = new File("/sdcard/"); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { f = new File(Environment.getExternalStorageDirectory().toString()); } else { //Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show(); return imageList; } File[] files = f.listFiles(); if(files == null || files.length == 0) return imageList; /** * 将所有图像文件的路径存入ArrayList列表 */ for (int i = 0; i < files.length; i ) { File file = files[i]; if (isImageFile(file.getPath())) imageList.add(file.getPath()); } return imageList; } private boolean isImageFile(String fName) { boolean re; String end = fName .substring(fName.lastIndexOf(".") 1, fName.length()) .toLowerCase(); /** * 依据文件扩展名判断是否为图像文件 */ if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { //System.out.print(end); re = true; } else { re = false; } return re; } @Override public boolean onCreateOptionsMenu(Menu menu) { /* * * add()方法的四个参数,依次是: * * 1、组别,如果不分组的话就写Menu.NONE, * * 2、Id,这个很重要,Android根据这个Id来确定不同的菜单 * * 3、顺序,那个菜单现在在前面由这个参数的大小决定 * * 4、文本,菜单的显示文本 */ menu.add(Menu.NONE, Menu.FIRST 1, 1, "删除图片").setIcon( android.R.drawable.ic_menu_delete); // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以 // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的 menu.add(Menu.NONE, Menu.FIRST 2, 2, "向右旋转").setIcon( android.R.drawable.ic_menu_rotate); /*menu.add(Menu.NONE, Menu.FIRST 4, 1, "添加").setIcon( android.R.drawable.ic_menu_add);*/ menu.add(Menu.NONE, Menu.FIRST 3, 3, "图片信息").setIcon( android.R.drawable.ic_menu_info_details); menu.add(Menu.NONE, Menu.FIRST 4, 4, "分享图片").setIcon( android.R.drawable.ic_menu_send); menu.add(Menu.NONE, Menu.FIRST 5, 5, "设为壁纸").setIcon( android.R.drawable.ic_menu_set_as); menu.add(Menu.NONE, Menu.FIRST 6, 6, "显示缩略图").setIcon( android.R.drawable.ic_menu_set_as); return true; } // ********************************************************** /*public void showImageInformation(){ int photoIndex; int photoNameIndex; int photoIDIndex; int photoTitleIndex; int photoSizeIndex; String name = null; String columns[] = new String[] { Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME, Media.SIZE }; // 得到一个游标 cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null); photoIndex = cursor.getColumnIndexOrThrow(Media.DATA); photoNameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME); photoIDIndex = cursor.getColumnIndexOrThrow(Media._ID); photoTitleIndex = cursor.getColumnIndexOrThrow(Media.TITLE); photoSizeIndex = cursor.getColumnIndexOrThrow(Media.SIZE); // 获取图片的Name if(cursor.moveToNext()){ name = cursor.getString(photoNameIndex); // 获取图片的ID String number = cursor.getString(photoIDIndex); // 获取图片的Title String title = cursor.getString(photoTitleIndex); // 获取图片的大小 String size = cursor.getString(photoSizeIndex); // 获取图片存储路径 String path = cursor.getString(photoIndex); } Dialog dialog = new AlertDialog.Builder(this) .setTitle("图片信息") .setMessage("图片名称" name).create(); }*/ //**************************************************************** @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST 1: //删除 deleteImage(); //Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST 2: //旋转 rotateImage(); //Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST 3://详细 showImageInfo(); //Toast.makeText(this, filePath, Toast.LENGTH_LONG).show(); break; case Menu.FIRST 4: //发送 shareImages(); //Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST 5: //设置为壁纸 setBackground(); //Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); break; /*case Menu.FIRST 6: //Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show(); break;*/ case Menu.FIRST 6: //显示缩略图 if (menuShowed) { menuShowed = false; mylayout.startAnimation(hideAction); mylayout.setVisibility(View.GONE); } else { menuShowed = true; mylayout.startAnimation(showAction); mylayout.setVisibility(View.VISIBLE); } //Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); break; } return false; } /*@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }*/ /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } public void showImageInfo(){ String imagePath ="图片路径:" filePath; String imageName = "图片名称:" imagePath.substring(imagePath.lastIndexOf("/") 1, imagePath.length()); String imageSize = "图片尺寸:" String.valueOf(BitmapFactory.decodeFile(filePath).getWidth()) "*" String.valueOf(BitmapFactory.decodeFile(filePath).getHeight()); String[] imageInfo = new String[]{imagePath,imageName,imageSize}; Dialog dialog = new AlertDialog.Builder(this) .setTitle("图片信息") .setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).setItems(imageInfo, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); } public void deleteImage(){ Dialog dialog = new AlertDialog.Builder(this) .setTitle("确定删除?") .setMessage("您确定删除此图片?") .setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub File file = new File(filePath); file.delete(); //List<String> testList; ImageList.remove(ImageList.get(imagePosition)); //imageAdapter gallory.setAdapter(new ImageAdapter(currentContext, ImageList)); //Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); //System.out.print("a"); } }).setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); //File file = new File(filePath); } public void setBackground(){ Dialog dialog = new AlertDialog.Builder(this) .setTitle("设置桌面背景") .setMessage("您确定将此图片设置为桌面背景?") .setPositiveButton("确定",new DialogInterface.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub try{ //MainActivity.getApplicationContext().clearWallpaper(); currentContext.clearWallpaper(); currentContext.setWallpaper(bmp); Toast.makeText(currentContext, "桌面背景修改成功", Toast.LENGTH_LONG).show(); } catch(Exception e){ Toast.makeText(currentContext, "桌面背景修改失败", Toast.LENGTH_LONG).show(); } } }).setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); } public void shareImages(){ Intent intent=new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, "分享内容"); startActivity(Intent.createChooser(intent, "选择分享方式")); } public void rotateImage(){ //ScaleAngle = ScaleAngle 90; //ScaleAngle ; Matrix matrix = new Matrix(); /*使用Matrix.postScale 设置维度*/ matrix.postScale(1.0f, 1.0f); /*使用Matrix.setRotate 方法旋转Bitmap*/ matrix.setRotate(90); bmp = Bitmap.createBitmap( bmp, 0 , 0, bmp.getWidth(), bmp.getHeight(), matrix, true); dragImageView.setImageBitmap(bmp); dragImageView.setmActivity(this);//注入Activity. /*BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(resizedBitmap);*/ } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } public void showGalleryFlow(){ } }