Androidは美しいGalleryアルバムを開発
3 Dアルバム(Gallery 3 D+バックグラウンド効果)GalleryFlow
GalleryFlowActivity.java
GalleryFlow.java
ImageAdapter.java
layout_gallery.xml
Galleryループ再生画像
GalleryFlowActivity.java
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 extendsActivity {
privateGalleryFlow galleryFlow;
privateImageAdapter adapter;
privateGestureDetector detector;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_gallery);
detector =new GestureDetector(newMyGestureListener());
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);
}
privateclass MyGestureListenerextends SimpleOnGestureListener {
@Override
publicboolean onFling(MotionEvent e1, MotionEvent e2,float velocityX,
floatvelocityY) {
// TODO Auto-generated method stub
intcurrentPosition = 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);
}
}
returnsuper.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
publicboolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
returndetector.onTouchEvent(event);
}
}
GalleryFlow.java
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 extendsGallery {
privateCamera mCamera = newCamera();
privateint mMaxRotationAngle =60;//
privateint mMaxZoom = -430;//
privateint mCoverflowCenter;
publicGalleryFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}
publicGalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
publicGalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
publicint getMaxRotationAngle() {
returnmMaxRotationAngle;
}
publicvoid setMaxRotationAngle(intmaxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
publicint getMaxZoom() {
returnmMaxZoom;
}
publicvoid setMaxZoom(intmaxZoom) {
mMaxZoom = maxZoom;
}
privateint getCenterOfCoverflow() {
return(getWidth() - getPaddingLeft() - getPaddingRight()) / 2
+ getPaddingLeft();
}
privatestatic int getCenterOfView(View view) {
returnview.getLeft() + view.getWidth() / 2;
}
@Override
protectedboolean getChildStaticTransformation(View child, Transformation t) {
// TODO Auto-generated method stub
finalint childCenter = getCenterOfView(child);
finalint childWidth = child.getWidth();
introtationAngle = 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);
}
returntrue;
}
privatevoid transformImageBitmap(ImageView child, Transformation t,
introtationAngle) {
mCamera.save();
finalMatrix imageMatrix = t.getMatrix();
finalint imageHeight = child.getLayoutParams().height;
finalint imageWidth = child.getLayoutParams().width;
finalint rotation = Math.abs(rotationAngle);
mCamera.translate(0.0f,0.0f, 100.0f);
if(rotation < mMaxRotationAngle) {
floatzoomAmount = (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();
}
protectedvoid onSizeChanged(intw, int h, int oldw, int oldh) {
mCoverflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
}
ImageAdapter.java
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 extendsBaseAdapter {
intmGalleryItemBackground;
privateContext mContext;
privateint[] mImageIds;
privateImageView[] mImages;
publicImageAdapter(Context c, int[] ImageIds) {
mContext = c;
mImageIds = ImageIds;
mImages =new ImageView[mImageIds.length];
}
publicvoid createReflectedImages() {
// TODO Auto-generated method stub
finalint reflectionGap = 4;
intindex = 0;
for(int imageId : mImageIds) {
Bitmap originalImage = BitmapFactory.decodeResource(
mContext.getResources(), imageId);
intwidth = originalImage.getWidth();
intheight = 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(newPorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
ImageView imageView =new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);
imageView.setLayoutParams(newGalleryFlow.LayoutParams(160,240));
mImages[index++] = imageView;
}
}
publicint getCount() {
// TODO Auto-generated method stub
returnmImageIds.length;
}
publicObject getItem(intposition) {
// TODO Auto-generated method stub
returnposition;
}
publiclong getItemId(intposition) {
// TODO Auto-generated method stub
returnposition;
}
publicView getView(intposition, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
returnmImages[position];
}
publicfloat getScale(booleanfocused, int offset) {
returnMath.max(0,1.0f / (float) Math.pow(2, Math.abs(offset)));
}
}
layout_gallery.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns: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>
Galleryループ再生画像
package com.example.testandroid;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/** * gallery * * @author maqf * */
@SuppressWarnings("deprecation")
public class CycleImageActivity extendsActivity {
privateint[] picture = { R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5,
R.drawable.gallery6, };
privateGallery pictureGallery = null;
privateint index = 0;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tiem);
this.pictureGallery = (Gallery) findViewById(R.id.gallery);
ImageAdapter adapter =new ImageAdapter(this);
this.pictureGallery.setAdapter(adapter);
Timer timer =new Timer();
timer.schedule(task,2000, 2000);
}
/** * */
privateTimerTask task = newTimerTask() {
@Override
publicvoid run() {
Message message =new Message();
message.what =2;
index = pictureGallery.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
};
/** * Handler */
privateHandler handler = newHandler() {
@Override
publicvoid handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what) {
case2:
pictureGallery.setSelection(index);
break;
default:
break;
}
}
};
/** * * * @author TanRuixiang * */
classImageAdapter extendsBaseAdapter {
privateContext context;
publicImageAdapter(Context context) {
this.context = context;
}
publicint getCount() {
returnInteger.MAX_VALUE;
}
publicObject getItem(intposition) {
returnposition;
}
publiclong getItemId(intposition) {
returnposition;
}
publicView getView(intposition, View convertView, ViewGroup parent) {
ImageView imageView =new ImageView(context);
imageView.setImageResource(picture[position % picture.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(newGallery.LayoutParams(
Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
returnimageView;
}
}
}