看了看效果,跑了一下。感觉还不错。
作者不知道是谁了,收藏一下:
Gallery3DActivity
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import com.zgy.android.view.MyGalleryView; public class Gallery3DActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_gallery); Integer[] images = { R.drawable.sample_5, R.drawable.sample_7, R.drawable.sample_5, R.drawable.sample_7, R.drawable.sample_5, R.drawable.sample_7, R.drawable.sample_5, R.drawable.sample_7 }; ImageAdapter adapter = new ImageAdapter(this, images); adapter.createReflectedImages();// 创建倒影效果 MyGalleryView galleryFlow = (MyGalleryView) this.findViewById(R.id.Gallery01); galleryFlow.setFadingEdgeLength(0); galleryFlow.setSpacing(-100); // 图片之间的间距 galleryFlow.setAdapter(adapter); galleryFlow.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show(); } }); galleryFlow.setSelection(4); } }
ImageAdapter.java
import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Bitmap.Config; import android.graphics.Shader.TileMode; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.zgy.android.view.MyGalleryView; public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds; private ImageView[] mImages; public ImageAdapter(Context c, Integer[] ImageIds) { mContext = c; mImageIds = ImageIds; mImages = new ImageView[mImageIds.length]; } /** * 创建倒影效果 * * @return */ public boolean createReflectedImages() { // 倒影图和原图之间的距离 final int reflectionGap = 4; int index = 0; for (int imageId : mImageIds) { // 返回原图解码之后的bitmap对象 Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId); int width = originalImage.getWidth(); int height = originalImage.getHeight(); // 创建矩阵对象 Matrix matrix = new Matrix(); // 指定一个角度以0,0为坐标进行旋转 // matrix.setRotate(30); // 指定矩阵(x轴不变,y轴相反) matrix.preScale(1, -1); // 将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); // 创建一个宽度不变,高度为原图+倒影图高度的位图 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); // 将上面创建的位图初始化到画布 Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(originalImage, 0, 0, null); Paint deafaultPaint = new Paint(); deafaultPaint.setAntiAlias(false); // canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); paint.setAntiAlias(false); /** * 参数一:为渐变起初点坐标x位置, 参数二:为y轴位置, 参数三和四:分辨对应渐变终点, 最后参数为平铺方式, * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变 */ LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); // 设置阴影 paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); // 用已经定义好的画笔构建一个矩形阴影渐变效果 canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint); // 创建一个ImageView用来显示已经画好的bitmapWithReflection ImageView imageView = new ImageView(mContext); imageView.setImageBitmap(bitmapWithReflection); // 设置imageView大小 ,也就是最终显示的图片大小 imageView.setLayoutParams(new MyGalleryView.LayoutParams(300, 400)); // imageView.setScaleType(ScaleType.MATRIX); mImages[index++] = imageView; } return true; } @SuppressWarnings("unused") private Resources getResources() { return null; } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { return mImages[position]; } public float getScale(boolean focused, int offset) { return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); } }
这个类里的倒影效果挺有意思的。
MyGalleryView.java
import android.content.Context; import android.graphics.Camera; import android.graphics.Matrix; import android.util.AttributeSet; import android.view.View; import android.view.animation.Transformation; import android.widget.Gallery; import android.widget.ImageView; public class MyGalleryView extends Gallery { private Camera mCamera = new Camera();// 相机类 private int mMaxRotationAngle = 60;// 最大转动角度 private int mMaxZoom = -300;// //最大缩放值 private int mCoveflowCenter;// 半径值 public MyGalleryView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setStaticTransformationsEnabled(true); } public MyGalleryView(Context context, AttributeSet attrs) { super(context, attrs); this.setStaticTransformationsEnabled(true); } public MyGalleryView(Context context) { super(context); this.setStaticTransformationsEnabled(true); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); } public int getMaxRotationAngle() { return mMaxRotationAngle; } public void setMaxRotationAngle(int maxRotationAngle) { mMaxRotationAngle = maxRotationAngle; } public int getMaxZoom() { return mMaxZoom; } public void setMaxZoom(int maxZoom) { mMaxZoom = maxZoom; } private int getCenterOfCoverflow() { return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft(); } private static int getCenterOfView(View view) { System.out.println("view left :" + view.getLeft()); System.out.println("view width :" + view.getWidth()); return view.getLeft() + view.getWidth() / 2; } // 控制gallery中每个图片的旋转(重写的gallery中方法) @Override protected boolean getChildStaticTransformation(View child, Transformation t) { // 取得当前子view的半径值 final int childCenter = getCenterOfView(child); System.out.println("childCenter:" + childCenter); final int childWidth = child.getWidth(); // 旋转角度 int rotationAngle = 0; // 重置转换状态 t.clear(); // 设置转换类型 t.setTransformationType(Transformation.TYPE_MATRIX); // 如果图片位于中心位置不需要进行旋转 if (childCenter == mCoveflowCenter) { transformImageBitmap((ImageView) child, t, 0); } else { // 根据图片在gallery中的位置来计算图片的旋转角度 rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); System.out.println("rotationAngle:" + rotationAngle); // 如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;) if (Math.abs(rotationAngle) > mMaxRotationAngle) { rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle; } transformImageBitmap((ImageView) child, t, rotationAngle); } return true; } protected void onSizeChanged(int w, int h, int oldw, int oldh) { mCoveflowCenter = getCenterOfCoverflow(); super.onSizeChanged(w, h, oldw, oldh); } 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); // 在Z轴上正向移动camera的视角,实际效果为放大图片。 // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。 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); } // 在Y轴上旋转,对应图片竖向向里翻转。 // 如果在X轴上旋转,则对应图片横向向里翻转。 mCamera.rotateY(rotationAngle); mCamera.getMatrix(imageMatrix); imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); mCamera.restore(); } }
这个类主要控制图片角度移动。
在 Objective-C 中的类实现中经常看到这两个关键字 ”self” 和 ”super”,以以前 oop 语言的经验,拿 c++ 为例,self 相当于 this,super 相当于调用父类的方法,这么看起来是很容易理解的。以下面的代码为例://
// main.m // SelfAndSuper // // Created by xuefeng li on 12-6-24. // Copyright (c) 2012年 nyist. All rights reserved. // #import <Foundation/Foundation.h> @interface Person:NSObject { NSString* name; } - (void) setName:(NSString*) yourName; @end @implementation Person - (void)setName:(NSString *)yourName { name = yourName; } @end @interface PersonMe:Person { NSUInteger age; } - (void) setAge:(NSUInteger)yourage; - (void) setName:(NSString*) yourName andAge:(NSUInteger)yourage; @end @implementation PersonMe - (void)setAge:(NSUInteger)yourage { age = yourage; } - (void) setName:(NSString*) yourName andAge:(NSUInteger)yourage { [self setAge:age]; [super setName:yourName]; NSLog(@"self ' class is %@", [self class]); NSLog(@"super' class is %@", [super class]); } @end int main (int argc, const char * argv[]) { @autoreleasepool { // insert code here... PersonMe* me = [[PersonMe alloc] init]; [me setName:@"abc" andAge:18]; [me release]; } return 0; }
上面有简单的两个类,在子类PersonMe中调用了自己类中的setAge和父类中的setName,这些代码看起来很好理解,没什么问题。
然后我在setName:andAge的方法中加入两行:
NSLog(@"self ' class is %@", [self class]);
NSLog(@"super' class is %@", [super class]);
这样在调用时,会打出来这两个的class,先猜下吧,会打印出什么?按照以前oop语言的经验,这里应该会输出:
self ' s class is PersonMe
super ' s class is Person
但是编译运行后,可以发现结果是:
self 's class is PersonMe
super ' s class is PersonMe
self 的 class 和预想的一样,怎么 super 的 class 也是 PersonMe?
真相
self 是类的隐藏的参数,指向当前当前调用方法的类,另一个隐藏参数是 _cmd,代表当前类方法的 selector。这里只关注这个 self。super 是个啥?super 并不是隐藏的参数,它只是一个“编译器指示符”,它和 self 指向的是相同的消息接收者,拿上面的代码为例,不论是用 [self setName] 还是 [super setName],接收“setName”这个消息的接收者都是 PersonMe* me 这个对象。不同的是,super 告诉编译器,当调用 setName 的方法时,要去调用父类的方法,而不是本类里的。
当使用 self 调用方法时,会从当前类的方法列表中开始找,如果没有,就从父类中再找;而当使用 super 时,则从父类的方法列表中开始找。然后调用父类的这个方法(从super出现的在的方法所在的类的父类开始查找。)
文档解释:
-
● self searches for the method implementation in the usual manner, starting in the dispatch table of the receiving object’s class. In the example above, it would begin with the class of the object receiving the reposition message.
-
● super starts the search for the method implementation in a very different place. It begins in the superclass of the class that defines the method where super appears. In the example above, it would begin with the superclass of the class where reposition is defined.
One more step
这种机制到底底层是如何实现的?其实当调用类方法的时候,编译器会将方法调用转成一个 C 函数方法调用,Apple 的 objcRuntimeRef 上说:
Sending Messages
When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
■ objc_msgSend sends a message with a simple return value to an instance of a class.
■ objc_msgSend_stret sends a message with a data-structure return value to an instance of
a class.
■ objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
■ objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.
可以看到会转成调用上面 4 个方法中的一个,由于 _stret 系列的和没有 _stret 的那两个类似,先只关注 objc_msgSend 和 objc_msgSendSuper 两个方法。当使用 [self setName] 调用时,会使用 objc_msgSend 的函数,先看下 objc_msgSend 的函数定义:
id objc_msgSend(id theReceiver, SEL theSelector, ...)
第一个参数是消息接收者,第二个参数是调用的具体类方法的 selector,后面是 selector 方法的可变参数。我们先不管这个可变参数,以 [self setName:] 为例,编译器会替换成调用 objc_msgSend 的函数调用,其中 theReceiver 是 self,theSelector 是 @selector(setName:),这个 selector 是从当前 self 的 class 的方法列表开始找的 setName,当找到后把对应的 selector 传递过去。
而当使用 [super setName] 调用时,会使用 objc_msgSendSuper 函数,看下 objc_msgSendSuper 的函数定义:
id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
第一个参数是个objc_super的结构体,第二个参数还是类似上面的类方法的selector,先看下objc_super这个结构体是什么东西:
struct objc_super {
id receiver;
Class superClass;
};
可以看到这个结构体包含了两个成员,一个是 receiver,这个类似上面 objc_msgSend 的第一个参数 receiver,第二个成员是记录写 super 这个类的父类是什么,拿上面的代码为例,当编译器遇到 PersonMe 里 setName:andAge 方法里的 [super setName:] 时,开始做这几个事:
里面的调用机制大体就是这样了,以上面的分析,回过头来看开始的代码,当输出 [self class] 和 [super class] 时,是个怎样的过程。
当使用 [self class] 时,这时的 self 是 PersonMe,在使用 objc_msgSend 时,第一个参数是 receiver 也就是 self,也是 PersonMe* me 这个实例。第二个参数,要先找到 class 这个方法的 selector,先从 PersonMe 这个类开始找,没有,然后到 PersonMe 的父类 Person 中去找,也没有,再去 Person 的父类 NSObject 去找,一层一层向上找之后,在 NSObject 的类中发现这个 class 方法,而 NSObject 的这个 class 方法,就是返回 receiver 的类别,所以这里输出 PersonMe。
当使用 [super class] 时,这时要转换成 objc_msgSendSuper 的方法。先构造 objc_super 的结构体吧,第一个成员变量就是 self,第二个成员变量是 Person,然后要找 class 这个 selector,先去 superClass 也就是 Person 中去找,没有,然后去 Person 的父类中去找,结果还是在 NSObject 中找到了。然后内部使用函数 objc_msgSend(objc_super->receiver, @selector(class)) 去调用,此时已经和我们用 [self class] 调用时相同了,此时的 receiver 还是 PersonMe* me,所以这里返回的也是 PersonMe。
官方例子代码:
#import <Foundation/Foundation.h> @interface High:NSObject -(void)negotiate; @end @implementation High -(void)negotiate { NSLog(@"High"); } @end @interface Mid : High -(void)makeLastingPeace; @end @implementation Mid -(void)negotiate { NSLog(@"Mid"); } -(void)makeLastingPeace { [super negotiate]; } @end @interface Low : Mid @end @implementation Low -(void)negotiate { NSLog(@"Low"); } @end int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Low* l=[[Low alloc] init]; [l makeLastingPeace]; [l release]; [pool drain]; return 0; }
//打印结果为high
另见参考:
http://blog.csdn.net/datacloud/article/details/7275170
首先,需要导入AudioToolbox.framework和OpenAL.framework。
OpenALViewController.h
#import <UIKit/UIKit.h> #import <OpenAL/alc.h> @interface OpenALViewController : UIViewController { ALCcontext *mContext; ALCdevice *mDevice; NSUInteger sourceID; NSUInteger bufferID; BOOL isPlaying; } - (IBAction)playPause:(id)sender; @end
OpenALViewController.m
#import "OpenALViewController.h" #import <AudioToolbox/AudioFile.h> #import <OpenAL/al.h> @implementation OpenALViewController - (void)initOpenAL { mDevice = alcOpenDevice(NULL); if (mDevice) { mContext = alcCreateContext(mDevice, NULL); alcMakeContextCurrent(mContext); } } - (AudioFileID)openAudioFile:(NSString *)filePath { AudioFileID outAFID; NSURL *afUrl = [NSURL fileURLWithPath:filePath]; #if TARGET_OS_IPHONE OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0, &outAFID); #else OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, fsRdPerm, 0, &outAFID); #endif if (result != 0) NSLog(@"cannot open file: %@", filePath); return outAFID; } - (UInt32)audioFileSize:(AudioFileID)fileDescriptor { UInt64 outDataSize = 0; UInt32 thePropSize = sizeof(UInt64); OSStatus result = AudioFileGetProperty(fileDescriptor, kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize); if(result != 0) NSLog(@"cannot find file size"); return (UInt32)outDataSize; } - (void)cleanUpOpenAL { alDeleteSources(1, &sourceID); alDeleteBuffers(1, &bufferID); alcDestroyContext(mContext); alcCloseDevice(mDevice); } - (IBAction)playPause:(id)sender { UIButton *btnPlay = (UIButton *)sender; if(!isPlaying) { alSourcePlay(sourceID); isPlaying = YES; [btnPlay setTitle:@"Pause" forState:UIControlStateNormal]; } else { alSourceStop(sourceID); isPlaying = NO; [btnPlay setTitle:@"Play" forState:UIControlStateNormal]; } } #pragma mark - - (void)viewDidLoad { [super viewDidLoad]; isPlaying = NO; [self initOpenAL]; NSString *fileName = [[NSBundle mainBundle] pathForResource:@"outSound" ofType:@"caf"]; AudioFileID fileID = [self openAudioFile:fileName]; UInt32 fileSize = [self audioFileSize:fileID]; unsigned char *outData = malloc(fileSize); OSStatus result = noErr; result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData); AudioFileClose(fileID); if (result != 0) NSLog(@"cannot load effect: %@", fileName); alGenBuffers(1, &bufferID); alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 8000); alGenSources(1, &sourceID); alSourcei(sourceID, AL_BUFFER, bufferID); alSourcef(sourceID, AL_PITCH, 1.0f); alSourcef(sourceID, AL_GAIN, 1.0f); alSourcei(sourceID, AL_LOOPING, AL_TRUE); if (outData) { free(outData); outData = NULL; } } - (void)dealloc { [self cleanUpOpenAL]; [super dealloc]; } @end