您的位置:首页 > 移动开发 > Android开发

Android特效之一-Gallery应用

2011-07-21 10:00 417 查看
/*
1.ShowAcitivity可以为MainActivity
2.ImageAdapterGeneral为Gallery对应的图片的处理类
3.show.xml为对应的布局文件
4.GalleryFlow为一个继承Gallery的自己实现的类
5.menu.xml为对应的菜单布局文件

package com.wal.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wal.control.GalleryFlow;
import com.wal.control.ImageAdapterGeneral;
import com.wal.controlclass.Preview;

/**
* @author jace
*
* Function:
*  		1.实现读取照片,并显示到页面的ImageView上
*
*  		2.实现Gallery特效图片显示,并实现对应的图片操作链接
*
*/

public class ShowAcitivity extends Activity{
private byte[] mbyte;
private Bitmap mBitmap;
private ImageView mImageView;
private GalleryFlow mGallery;
private Matrix mMatrix;
private static String dirPath;
private static EditText receiverText;
private static EditText picSubjectText;
private TextView mTextViewForOperation;

static Intent intent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

//		//实现照相相片翻转
//		ShowAcitivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

// 设置全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// 去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);

//显示布局
setContentView(R.layout.show);

//获取ImageView对象
mImageView = (ImageView)findViewById(R.id.mImageView);

//获取TextView对象
mTextViewForOperation = (TextView)findViewById(R.id.mTextViewPicOperation);

//取得需要显示在ImageView的照片的名字
String strdirValue = Preview.picName;
//设置需要读取的照片的路径
dirPath = "/sdcard/Myphotos/" + strdirValue + ".jpg";

//将指定路径的照片显示到ImageView上
mImageView.setImageBitmap(getBitmapFromSDcard(dirPath));

//		mProgressDialog.cancel();
//		TakePicActivity mTakePicActivity = new TakePicActivity();
//		mTakePicActivity.mProgressDialog.cancel();
//		System.out.println("销毁ProgressDialog");

System.out.println("Ok------");

//取得并显示提供操作的Gallery的操作列表
ImageAdapterGeneral mImageAdapterGeneral = new ImageAdapterGeneral(ShowAcitivity.this);
//	     ImageAdapter mImageAdapterGeneral = new ImageAdapter(this, ConstantOrVariable.imagesForOperation);
//	     mImageAdapterGeneral.createReflectedImages();

mGallery = (GalleryFlow)findViewById(R.id.mGallery);
mGallery.setAdapter(mImageAdapterGeneral);
mGallery.setSelection(2);

//设置mGallery操作列表的事件监听
mGallery.setOnItemClickListener(picClickListener);

//设置mGallery操作提示列表的事件监听
mGallery.setOnItemSelectedListener(operationClickListener);

System.out.println("------end------");
}

/* 读取sdcard上面的指定目录下的图片 */
public Bitmap getBitmapFromSDcard(String dir){
if(dir == null || dir.equals("")){
System.out.print("---null---");
return null;
}
File file  = new File(dir);
FileInputStream is = null;
try {
is  = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mBitmap = BitmapFactory.decodeStream(is);

//获取显示屏幕的解析像素大小
int screenXY[] = new int[2];
screenXY = getDisplayXY();

if(mBitmap != null){
//获取读取到的图片的宽高信息
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();

//如果读取的图片像素超过屏幕的大小,就按照指定的比例进行处理
if(width > screenXY[0] && height > screenXY[1]){
int newWidth = screenXY[0];
int newHeight = screenXY[1];

float scaleWidth = ((float)newWidth) / width;
float scaleHeight = ((float)newHeight) /height;
float minScale = Math.min(scaleWidth, scaleHeight);
float maxScale = Math.max(scaleWidth, scaleHeight);

mMatrix = new Matrix();
mMatrix.postScale(maxScale, minScale);

mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, mMatrix, true);
}

}else{
Toast.makeText(ShowAcitivity.this,"获取的Bitmap 为空 ",
Toast.LENGTH_SHORT).show();
System.out.println("Bitmap ---> null");
}

return mBitmap;
}

/* 取得手机屏幕的解析像素 */
public int[] getDisplayXY(){
int[] screenXY = new int[2];

DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
screenXY[0] = mDisplayMetrics.widthPixels;
screenXY[1] = mDisplayMetrics.heightPixels;

for(int i = 0; i < 2; i++){
System.out.println("screen = "  + screenXY[i]) ;
}
return screenXY;
}

//mGallery中单一图片对应的操作链接
private OnItemClickListener picClickListener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub

switch (position) {
case 0:
Toast.makeText(ShowAcitivity.this,"你开启了第 "+(position+1)+" 项操作",
Toast.LENGTH_SHORT).show();
ShowAcitivity.this.finish();
//后退,返回相机状态
//				System.exit(0);

break;

case 1:
Toast.makeText(ShowAcitivity.this,"你开启了第 "+(position+1)+" 项操作",
Toast.LENGTH_SHORT).show();
break;

case 2:
Toast.makeText(ShowAcitivity.this,"你开启了第 "+(position+1)+" 项操作",
Toast.LENGTH_SHORT).show();

//				File file  = new File("/sdcard/Myphotos/m.jpg");
//				File file = new File("/sdcard/xiaowei.mp3");
File file = new File(dirPath);
intent = new Intent(Intent.ACTION_SEND);

//设置邮件主题
//				intent.putExtra("subject", file.getName());

//设置邮件内容
intent.putExtra("body", "android123-amail sender");

//设置邮件接收人
//				intent.putExtra("to", "chyl_lyf@163.com");

//设置邮件发送的附件
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

//设置附件文件类型
intent.setType("application/octet-stream");

showDialogForUser();

break;
case 3:
Toast.makeText(ShowAcitivity.this,"你开启了第 "+(position+1)+" 项操作",
Toast.LENGTH_SHORT).show();
break;

case 4:
Toast.makeText(ShowAcitivity.this,"你开启了第 "+(position+1)+" 项操作",
Toast.LENGTH_SHORT).show();
break;

}
}
};

//mGllery中单一图片对应的操作的提示
private OnItemSelectedListener operationClickListener = new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
switch (position) {
case 0:
mTextViewForOperation.setText("后退");
break;

case 1:
mTextViewForOperation.setText("编辑照片");
break;

case 2:
mTextViewForOperation.setText("Email-发送照片");
break;

case 3:
mTextViewForOperation.setText("深度编辑");
break;

case 4:
mTextViewForOperation.setText("短信发送照片");
break;
}
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
};

/* 点击传送邮件信息操作,激发的事件处理 */
private void showDialogForUser(){

LayoutInflater mLayoutInflater = LayoutInflater.from(ShowAcitivity.this);

final View DialogView = mLayoutInflater.inflate(R.layout.dialog, null);

receiverText = (EditText)DialogView.findViewById(R.id.receiverEditText);
picSubjectText = (EditText)DialogView.findViewById(R.id.subjectEditText);

//创建对话框
AlertDialog mAlertDialog = new AlertDialog.Builder(ShowAcitivity.this)
.setTitle("请填写...")
.setView(DialogView)
.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
//设置邮件接收人
//							System.out.println("receiver --- " + receiverText.getText().toString());
intent.putExtra("to", receiverText.getText().toString());
//							intent.putExtra("to", "chyl_lyf@163.com");

//设置邮件主题
intent.putExtra("subject", picSubjectText.getText().toString());

startActivity(intent);

}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated metmAlertDialog.cancel();
//							mAlertDialog.dismiss();
}
})
.create();
//显示mAlertDialog
mAlertDialog.show();

}

/* 创建ShowActivity页面的菜单 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mMenuInflater = getMenuInflater();

//设置menu界面为menu01.xml
mMenuInflater.inflate(R.menu.menu01, menu);

System.out.println(" Menu-----");
return true;
}

/* 处理菜单事件 */

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//获取当前选中的MenuItem的ID
int itemId = item.getItemId();

switch (itemId) {
case R.id.explain:

break;

case R.id.exitShow:
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
break;
}

return true;
}
}

/——————————————————————————————————————/

package com.wal.control;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapterGeneral extends BaseAdapter{
private Context context;
public ImageAdapterGeneral(Context context) {
this.context = context;
}

//获取图片个数
public int getCount() {
// TODO Auto-generated method stub
return ConstantOrVariable.imagesForOperation.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

ImageView mImageView = new ImageView(context);

//给ImageView 设置资源
mImageView.setImageResource(ConstantOrVariable.imagesForOperation[position]);

//设置布局图片的大小
mImageView.setLayoutParams(new Gallery.LayoutParams(200, 130));

//设置显示比例的类型
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

//设置图片的透明度
mImageView.setAlpha(150);

//设置每张图片之间的左上右下的相隔参数
mImageView.setPadding(26, 0, 26, 0);

return mImageView;
}

}

/********************************************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ImageView>
<com.wal.control.GalleryFlow
android:id="@+id/mGallery"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<TextView
android:id="@+id/mTextViewPicOperation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#CCFF99"
android:gravity="center"
android:layout_alignParentTop="true"
android:background="@drawable/gallerybackground"
/>
</RelativeLayout>

/***************************************************************/

package com.wal.control;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
im
3ff0
port android.widget.ImageView;

public class GalleryFlow extends Gallery {

/**
* Graphics Camera used for transforming the matrix of ImageViews
*
* \u037c\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdImageViews\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u05ea\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd
*/
private Camera mCamera = new Camera();

/**
* The maximum angle the Child ImageView will be rotated by
*
* \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u05ea
*/
private int mMaxRotationAngle = 60;

/**
* The maximum zoom on the centre Child
*
* \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u0135\ufffd\ufffd\ufffd\ufffd\u0174\ufffd\u0427\ufffd\ufffd
*/
private int mMaxZoom = -120;

/**
* The Centre of the Coverflow
*
* Coverflow\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd
*/
private int mCoveflowCenter;

public GalleryFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}

public GalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}

public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}

/**
* Get the max rotational angle of the image
*
* \ufffd\ufffd\u05ea\ufffd\u01f6\u0235õ\ufffd\ufffd\ufffd\ufffd\ufffd\u04f0\ufffd\ufffd
*
* @return the mMaxRotationAngle
*/
public int getMaxRotationAngle() {
return mMaxRotationAngle;
}

/**
* Set the max rotational angle of each image
*
* \u03aaÿ\u04bb\ufffd\ufffd\u037c\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u05ea\ufffd\u01f6\ufffd
*
* @param maxRotationAngle
*            the mMaxRotationAngle to set
*/
public void setMaxRotationAngle(int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}

/**
* Get the Max zoom of the centre image
*
* @return the mMaxZoom
*/
public int getMaxZoom() {
return mMaxZoom;
}

/**
* Set the max zoom of the centre image
*
* @param maxZoom
*            the mMaxZoom to set
*/
public void setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}

/**
* Get the Centre of the Coverflow
*
* @return The centre of this Coverflow.
*/
private int getCenterOfCoverflow() {
return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
+ getPaddingLeft();
}

/**
* Get the Centre of the View
*
* @return The centre of the given view.
*/
private static int getCenterOfView(View view) {
return view.getLeft() + view.getWidth() / 2;
}

/**
* {@inheritDoc}
*
* @see #setStaticTransformationsEnabled(boolean)
*/
protected boolean getChildStaticTransformation(View child, Transformation t) {

final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;

t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);

if (childCenter == mCoveflowCenter) {
transformImageBitmap((ImageView) child, t, 0);
} else {
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap((ImageView) child, t, rotationAngle);
}

return true;
}

/**
* This is called during layout when the size of this view has changed. If
* you were just added to the view hierarchy, you're called with the old
* values of 0.
*
* @param w
*            Current width of this view.
* @param h
*            Current height of this view.
* @param oldw
*            Old width of this view.
* @param oldh
*            Old height of this view.
*/
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}

/**
* Transform the Image Bitmap by the Angle passed
*
* @param imageView
*            ImageView the ImageView whose bitmap we want to rotate
* @param t
*            transformation
* @param rotationAngle
*            the Angle by which to rotate the Bitmap
*/
private void transformImageBitmap(ImageView child, Transformation t,
int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = t.getMatrix();
final int imageHeight = child.getLayoutParams().height;
final int imageWidth = child.getLayoutParams().width;
final int rotation = Math.abs(rotationAngle);

// \ufffd\ufffdZ\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u01b6\ufffdcamera\ufffd\ufffd\ufffd\u04fd\u01e3\ufffd\u02b5\ufffd\ufffd\u0427\ufffd\ufffd\u03aa\ufffd\u0174\ufffd\u037c\u01ac\ufffd\ufffd
// \ufffd\ufffd\ufffd\ufffd\ufffdY\ufffd\ufffd\ufffd\ufffd\ufffd\u01b6\ufffd\ufffd\ufffd\ufffd\ufffd\u037c\u01ac\ufffd\ufffd\ufffd\ufffd\ufffd\u01b6\ufffd\ufffd\ufffdX\ufffd\ufffd\ufffd\u03f6\ufffd\u04e6\u037c\u01ac\ufffd\ufffd\ufffd\ufffd\ufffd\u01b6\ufffd\ufffd\ufffd
mCamera.translate(0.0f, 0.0f, 100.0f);

// As the angle of the view gets less, zoom in
if (rotation < mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
mCamera.translate(0.0f, 0.0f, zoomAmount);
}

// \ufffd\ufffdY\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u05ea\ufffd\ufffd\ufffd\ufffd\u04e6\u037c\u01ac\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufded\u05ea\ufffd\ufffd
// \ufffd\ufffd\ufffd\ufffd\ufffdX\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u05ea\ufffd\ufffd\ufffd\ufffd\ufffd\u04e6\u037c\u01ac\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufded\u05ea\ufffd\ufffd
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
mCamera.restore();
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
//		return super.onFling(e1, e2, velocityX, velocityY);
return false;
}
}

/******************************************************************/
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/explain"
android:title="@string/explain"
/>
<item
android:id="@+id/exitShow"
android:title="@string/exitout"
/>
</menu>

/************************/
备注:由于部分文件,是我原来在Windows下的环境写的,后面导进Linux下的环境了,
变了乱码,不过安写都是部分的注释,应该没什么问题!
Gallery带有倒影,透明等效果,希望与大家交流下....
由于这是个人项目中的部分,功能可能会牵涉到其他一些相关内容,
只要自己变通下,就可以的了

感谢....支持........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Gallery 特效 倒影 透明