android 3d 效果,Android Gallery 3D效果(有图有真相)

--------------------.Java

package gongzibai.co.cc;

import java.util.ArrayList;

import Android.app.Activity;

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.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.KeyEvent;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

import android.widget.ImageView.ScaleType;

import android.widget.Toast;

public class Gallery1Activity extends

Activity {

public static final int CHANGE_BANNER = 1;

private int cur_index = 0;

/** Called when the activity is first created. */

@Override

public void onCreate(

Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final DetialGallery gallery = (DetialGallery) findViewById(R.id.widget1);

ImageAdapter imageAdapter = new ImageAdapter(

Gallery1Activity.this);

gallery.setAdapter(imageAdapter);

final Handler handler = new Handler() {

public void handleMessage(

Message msg) {

switch (msg.what) {

case CHANGE_BANNER:

gallery.onKeyDown(

KeyEvent.KEYCODE_DPAD_RIGHT,

null);

break;

default:

break;

}

};

};

// new Thread(new Runnable() {

// int flag = 1;

//

// public void run() {

//

// while (true) {

//

// handler.sendEmptyMessage(CHANGE_BANNER);

//

// try {

//

// Thread.sleep(1000);

//

// } catch (InterruptedException e) {

//

// e.printStackTrace();

//

// }

//

// }

//

// }

//

// }

//

// ).start();

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override

public void onItemSelected(

AdapterView> arg0,

View arg1,

int arg2, long arg3) {

// TODO Auto-generated method stub

Toast.makeText(

Gallery1Activity.this,

"这是第" + arg2

+ "张图片",

2).show();

}

@Override

public void onNothingSelected(

AdapterView> arg0) {

// TODO Auto-generated method stub

}

});

}

class ImageAdapter extends

BaseAdapter {

// private ArrayList dishImages = new ArrayList();

// private ImageView[] mImages;

Context context;

int[] imgResId = new int[] {

R.drawable.icon,

R.drawable.icon2,

R.drawable.icon3,

R.drawable.icon,

R.drawable.icon2,

R.drawable.icon3,

};

public ImageAdapter(

Gallery1Activity gallery1Activity) {

// TODO Auto-generated constructor stub

super();

this.context = gallery1Activity;

}

//  public boolean createReflectedImages() {

//   final int reflectionGap = 4;

//   int index = 0;

//   System.out

//     .println("dishImages size "

//       + dishImages

//         .size());

//   for (int i = 0; i < dishImages

//     .size(); ++i) {

//    System.out

//      .println("dishImage --- "

//        + dishImages

//          .get(i));

//    Bitmap originalImage = BitmapFactory

//      .decodeByteArray(

//        dishImages

//          .get(i),

//        0,

//        dishImages

//          .get(i).length);

//    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 deafaultPaint = new Paint();

//    canvas.drawRect(

//      0,

//      height,

//      width,

//      height

//        + reflectionGap,

//      deafaultPaint);

//    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(

//      context);

//    imageView

//      .setImageBitmap(bitmapWithReflection);

//    // imageView.setLayoutParams(new GalleryFlow.LayoutParams(180,

//    // 240));

//    imageView

//      .setLayoutParams(new DetialGallery.LayoutParams(

//        170,

//        200));

//    imageView.setScaleType(ScaleType.FIT_XY);

//    mImages[index++] = imageView;

//   }

//   return true;

//  }

@Override

public int getCount() {

// TODO Auto-generated method stub

// return Integer.MAX_VALUE;

return Integer.MAX_VALUE;

}

@Override

public Object getItem(

int position) {

// TODO Auto-generated method stub

return position;

//

// return position;

// return position;

}

@Override

public long getItemId(

int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public View getView(int arg0,

View arg1,

ViewGroup arg2) {

// TODO Auto-generated method stub

ImageView imageView = new ImageView(

context);

imageView

.setImageResource(imgResId[arg0

% imgResId.length]);

imageView

.setScaleType(ImageView.ScaleType.FIT_XY);

imageView

.setLayoutParams(new DetialGallery.LayoutParams(

175, 200));

return imageView;

}

}

}

-----------------------.java DetialGallery

package gongzibai.co.cc;

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;

import android.widget.ImageView;

public class DetialGallery extends

Gallery {

private Camera mCamera = new Camera();

private int mMaxRotationAngle = 45;

private int mMaxZoom = -120;

private int mCoveflowCenter;

public DetialGallery(

Context context,

AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

}

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;

}

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;

}

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();

}

//

// private boolean isScrollingLeft(

// MotionEvent e1,

// MotionEvent e2) {

// return e2.getX() > e1.getX();

// }

@Override

public boolean onFling(

MotionEvent e1,

MotionEvent e2,

float velocityX,

float velocityY) {

// int keyCode;

// if (isScrollingLeft(e1, e2)) {

// keyCode = KeyEvent.KEYCODE_DPAD_LEFT;

// } else {

// keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;

// }

// onKeyDown(keyCode, null);

return false;

}

}

-------------------.xml

android:id="@+id/widget32"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/widget1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:spacing="10dp"

android:unselectedAlpha="0.5" />

dd9692da8a1723ccc37926c781b3eb11.png0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值