实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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(){
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论