android gallery 3d
4399 단어 gallery
코드를 직접 보고 갤러리를 사용자 정의합니다. 코드는 대부분 학습한 인터넷입니다.
첨부 파일은 데모입니다. 토론을 환영하는 건의가 있습니다.
public class Gallery3D extends Gallery {
private Camera camera = new Camera();//
private int maxRotationAngle = 60;//
private int maxZoom = 60;// z ,
private int coveflowCenter;//
private int height;
public Gallery3D(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
public Gallery3D(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public Gallery3D(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);
height = getHeight();
}
public int getMaxRotationAngle() {
return maxRotationAngle;
}
public void setMaxRotationAngle(int maxRotationAngle) {
this.maxRotationAngle = maxRotationAngle;
}
public int getMaxZoom() {
return maxZoom;
}
public void setMaxZoom(int maxZoom) {
this.maxZoom = 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_BOTH);
//
if (childCenter == coveflowCenter) {
transformImageBitmap((ImageView) child, t, 0);
} else {
// gallery
rotationAngle = (int) (((float) (coveflowCenter - childCenter) / childWidth) * maxRotationAngle);
System.out.println("rotationAngle:" + rotationAngle);
// (-mMaxRotationAngle mMaxRotationAngle;)
if (Math.abs(rotationAngle) > maxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -maxRotationAngle
: maxRotationAngle;
}
transformImageBitmap((ImageView) child, t, rotationAngle);
}
return true;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
coveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
private void transformImageBitmap(ImageView child, Transformation t,
int rotationAngle) {
//
camera.save();
//
// final int rotation = Math.abs(rotationAngle);
// Z camera , 。
// Y , ;X 。
camera.translate(0f, 0f, maxZoom);
// //
// if (rotation < mMaxRotationAngle) {
// float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
// mCamera.translate(0.0f, 0.0f, zoomAmount);
// }
// Y , 。
// X , 。
camera.rotateY(rotationAngle);
final Matrix imageMatrix = t.getMatrix();
camera.getMatrix(imageMatrix);
camera.restore();
//
// , gallery
// , ; , -1,
final int imageWidth = child.getLayoutParams().width;
imageMatrix.preTranslate(-(imageWidth / 2), -height / 2);
imageMatrix.postTranslate((imageWidth / 2), height / 2);
camera.save();
}
}
4