Gallery
This class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.
package com.github.gallery.view;
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 CustomGallery extends Gallery {
private int galleryCenterPoint = 0; // gallery的中心点
private Camera camera;
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
// 启用getChildStaticTransformation被调用
setStaticTransformationsEnabled(true);
camera = new Camera();
}
/**
* 当gallery控件的宽和高改变时回调此方法, 第一次计算出gallery的宽和高时, 也会出发此方法
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
galleryCenterPoint = getGalleryCenterPoint();
}
/**
* 返回gallery的item的子图形变换效果
* Transformation 指定当前item的变换效果
*/
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
int viewCenterPoint = getviewCenterPoint(child); // item的中心点
int rotateAngle = 0; // 默认旋转角度为0
// 如果当前的View的中心点不等于gallery的中心点, 就是两边的图片, 需要计算旋转角度
if(viewCenterPoint != galleryCenterPoint) {
// gallery中心点 - 图片中心点 = 差值
int diff = galleryCenterPoint - viewCenterPoint;
// 差值 / 图片的宽度 = 比值
float scale = (float)diff / (float)child.getWidth();
// 比值 * 最大旋转角度 = 最终的旋转角度
rotateAngle = (int) (scale * 50);
if(Math.abs(rotateAngle) > 50) { // 当前角度超过了50, 需要赋值到50 或者 -50
rotateAngle = rotateAngle > 0 ?