package com.image.galleryFlow; |
import android.app.Activity; |
import android.os.Bundle; |
import android.view.GestureDetector; |
import android.view.GestureDetector.SimpleOnGestureListener; |
import android.view.MotionEvent; |
public class GalleryFlowActivity extends Activity { |
private GalleryFlow galleryFlow; |
private ImageAdapter adapter; |
private GestureDetector detector; |
/** Called when the activity is first created. */ |
@Override |
public void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.layout_gallery); |
detector = new GestureDetector( new MyGestureListener()); |
int [] images = { R.drawable.p01, R.drawable.p02, R.drawable.p03, |
R.drawable.p04, R.drawable.p05 }; |
adapter = new ImageAdapter( this , images); |
adapter.createReflectedImages(); |
galleryFlow = (GalleryFlow) findViewById(R.id.gallery_flow); |
galleryFlow.setAdapter(adapter); |
galleryFlow.setSelection( 1 ); |
} |
private class MyGestureListener extends SimpleOnGestureListener { |
@Override |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, |
float velocityY) { |
// TODO Auto-generated method stub |
int currentPosition = galleryFlow.getSelectedItemPosition(); |
if (e1.getX() - e2.getX() > 50 && Math.abs(velocityX) > 100 ) { |
if (currentPosition + 1 == adapter.getCount()) { |
galleryFlow.setSelection( 0 ); |
} else { |
galleryFlow.setSelection(currentPosition + 1 ); |
} |
} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 100 ) { |
if (currentPosition - 1 < 0 ) { |
galleryFlow.setSelection(adapter.getCount() - 1 ); |
} else { |
galleryFlow.setSelection(currentPosition - 1 ); |
} |
} |
return super .onFling(e1, e2, velocityX, velocityY); |
} |
} |
@Override |
public boolean onTouchEvent(MotionEvent event) { |
// TODO Auto-generated method stub |
return detector.onTouchEvent(event); |
} |
} |
package com.image.galleryFlow; |
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 GalleryFlow extends Gallery { |
private Camera mCamera = new Camera(); |
private int mMaxRotationAngle = 60 ; // 最大旋转角度 |
private int mMaxZoom = - 430 ; // 最大缩放值 |
private int mCoverflowCenter; |
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 ); |
} |
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) { |
return view.getLeft() + view.getWidth() / 2 ; |
} |
@Override |
protected boolean getChildStaticTransformation(View child, Transformation t) { |
// TODO Auto-generated method stub |
final int childCenter = getCenterOfView(child); |
final int childWidth = child.getWidth(); |
int rotationAngle = 0 ; |
t.clear(); |
t.setTransformationType(Transformation.TYPE_MATRIX); |
if (childCenter == mCoverflowCenter) { |
transformImageBitmap((ImageView) child, t, 0 ); |
} else { |
rotationAngle = ( int ) ((( float ) (mCoverflowCenter - childCenter) / childWidth) * mMaxRotationAngle); |
if (Math.abs(rotationAngle) > mMaxRotationAngle) { |
rotationAngle = (rotationAngle < 0 ) ? -mMaxRotationAngle |
: mMaxRotationAngle; |
} |
transformImageBitmap((ImageView) child, t, rotationAngle); |
} |
return true ; |
} |
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); |
mCamera.translate( 0 .0f, 0 .0f, 100 .0f); |
if (rotation < mMaxRotationAngle) { |
float zoomAmount = ( float ) (mMaxZoom + (rotation * 1.5 )); |
mCamera.translate( 0 .0f, 0 .0f, zoomAmount); |
} |
mCamera.rotateY(rotationAngle); |
mCamera.getMatrix(imageMatrix); |
imageMatrix.preTranslate(-(imageWidth / 2 ), -(imageHeight / 2 )); |
imageMatrix.postTranslate((imageWidth / 2 ), (imageHeight / 2 )); |
mCamera.restore(); |
} |
protected void onSizeChanged( int w, int h, int oldw, int oldh) { |
mCoverflowCenter = getCenterOfCoverflow(); |
super .onSizeChanged(w, h, oldw, oldh); |
} |
} |
package com.image.galleryFlow; |
import android.content.Context; |
import android.graphics.Bitmap; |
import android.graphics.Bitmap.Config; |
import android.graphics.BitmapFactory; |
import android.graphics.Canvas; |
import android.graphics.LinearGradient; |
import android.graphics.Matrix; |
import android.graphics.Paint; |
import android.graphics.PorterDuff.Mode; |
import android.graphics.PorterDuffXfermode; |
import android.graphics.Shader.TileMode; |
import android.view.View; |
import android.view.ViewGroup; |
import android.widget.BaseAdapter; |
import android.widget.ImageView; |
public class ImageAdapter extends BaseAdapter { |
int mGalleryItemBackground; |
private Context mContext; |
private int [] mImageIds; |
private ImageView[] mImages; |
public ImageAdapter(Context c, int [] ImageIds) { |
mContext = c; |
mImageIds = ImageIds; |
mImages = new ImageView[mImageIds.length]; |
} |
public void createReflectedImages() { |
// TODO Auto-generated method stub |
final int reflectionGap = 4 ; |
int index = 0 ; |
for ( int imageId : mImageIds) { |
Bitmap originalImage = BitmapFactory.decodeResource( |
mContext.getResources(), imageId); |
int width = originalImage.getWidth(); |
int height = originalImage.getHeight(); |
Matrix matrix = new Matrix(); |
matrix.preScale( 1 , - 1 ); |
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 defaultPaint = new Paint(); |
canvas.drawRect( 0 , height, width, height + reflectionGap, |
defaultPaint); |
canvas.drawBitmap(reflectionImage, 0 , height + reflectionGap, null ); |
Paint paint = new Paint(); |
LinearGradient shader = new LinearGradient( 0 , |
originalImage.getHeight(), 0 , |
bitmapWithReflection.getHeight() + reflectionGap, |
0x70ffffff , 0x00ffffff , TileMode.CLAMP); |
paint.setShader(shader); |
paint.setXfermode( new PorterDuffXfermode(Mode.DST_IN)); |
canvas.drawRect( 0 , height, width, bitmapWithReflection.getHeight() |
+ reflectionGap, paint); |
ImageView imageView = new ImageView(mContext); |
imageView.setImageBitmap(bitmapWithReflection); |
imageView.setLayoutParams( new GalleryFlow.LayoutParams( 160 , 240 )); |
mImages[index++] = imageView; |
} |
} |
public int getCount() { |
// TODO Auto-generated method stub |
return mImageIds.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 |
return mImages[position]; |
} |
public float getScale( boolean focused, int offset) { |
return Math.max( 0 , 1 .0f / ( float ) Math.pow( 2 , Math.abs(offset))); |
} |
} |
<? xml version = "1.0" encoding = "utf-8" ?> |
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
android:layout_width = "fill_parent" |
android:layout_height = "fill_parent" |
android:background = "#ffffff" |
android:orientation = "vertical" > |
< com.image.galleryFlow.GalleryFlow |
android:id = "@+id/gallery_flow" |
android:layout_width = "fill_parent" |
android:layout_height = "fill_parent" |
android:layout_centerInParent = "true" |
android:spacing = "1dp" |
android:text = "@string/hello" /> |
</ RelativeLayout > |
by: 泽素余 发表于:2014-04-03 11:21:38 顶(0) | 踩(0) 回复
为什么我运行了有错啊?
04-03 03:21:08.020: I/Process(339): Sending signal. PID: 339 SIG: 9
04-03 03:22:25.199: D/AndroidRuntime(367): Shutting down VM
04-03 03:22:25.209: W/dalvikvm(367): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-03 03:22:25.249: E/AndroidRuntime(367): FATAL EXCEPTION: main
04-03 03:22:25.249: E/AndroidRuntime(367): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gallery/com.example.gallery.GalleryFlowActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.image.galleryFlow.GalleryFlow
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.os.Looper.loop(Looper.java:123)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-03 03:22:25.249: E/AndroidRuntime(367): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 03:22:25.249: E/AndroidRuntime(367): at java.lang.reflect.Method.invoke(Method.java:521)
04-03 03:22:25.249: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-03 03:22:25.249: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-03 03:22:25.249: E/AndroidRuntime(367): at dalvik.system.NativeStart.main(Native Method)
04-03 03:22:25.249: E/AndroidRuntime(367): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.image.galleryFlow.GalleryFlow
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-03 03:22:25.249: E/AndroidRuntime(367): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.Activity.setContentView(Activity.java:1647)
04-03 03:22:25.249: E/AndroidRuntime(367): at com.example.gallery.GalleryFlowActivity.onCreate(GalleryFlowActivity.java:18)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-03 03:22:25.249: E/AndroidRuntime(367): ... 11 more
04-03 03:22:25.249: E/AndroidRuntime(367): Caused by: java.lang.ClassNotFoundException: com.image.galleryFlow.GalleryFlow in loader dalvik.system.PathClassLoader[/data/app/com.example.gallery-2.apk]
04-03 03:22:25.249: E/AndroidRuntime(367): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-03 03:22:25.249: E/AndroidRuntime(367): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-03 03:22:25.249: E/AndroidRuntime(367): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
04-03 03:22:25.249: E/AndroidRuntime(367): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
04-03 03:22:25.249: E/AndroidRuntime(367): ... 20 more
04-03 03:22:38.690: I/Process(367): Sending signal. PID: 367 SIG: 9
中级设计师
by: 程序猿style 发表于:2012-10-16 09:24:31 顶(0) | 踩(0) 回复
很炫的效果!

回复评论