android自定义Gallery

本文详细介绍了如何创建一个自定义滚动相册组件,通过使用Android的Gallery控件并进行自定义改造,实现图片的流畅滚动和立体效果展示。重点在于通过Matrix和Camera类调整每个图片的显示位置和缩放比例,同时加入滑动反馈和动画效果,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.putao.game.majiang.util;

import com.putao.game.majiang.data.AppData;
import com.putao.game.majiang.data.User;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.Gallery;

/**
* 类功能描述:</br>
*
* @author 兰唯
* @version 1.0 </p> 修改时间:</br> 修改备注:</br>
*/

public class CustomGallery extends Gallery {

public Camera mCamera = new Camera();

Matrix mMatrix = new Matrix();

public CustomGallery(Context context) {
super(context);
setStaticTransformationsEnabled(true);
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
final float offset = calculateOffsetOfCenter(child);
transformViewRoom(child, t, offset);
return true;
}

protected int getCenterOfCoverflow() {
return ((getWidth() - getPaddingLeft() - getPaddingRight()) >> 1) + getPaddingLeft();
}

// 获取 child 中心点 X 的位置
protected int getCenterOfView(View view) {
return view.getLeft() + (view.getWidth() >> 1);
}

// 计算 child 偏离 父控件中心的 offset 值, -1 <= offset <= 1
protected float calculateOffsetOfCenter(View view) {
final int pCenter = getCenterOfCoverflow();
final int cCenter = getCenterOfView(view);

float offset = (cCenter - pCenter) / (pCenter * 1.0f);
offset = Math.min(offset, 0.3f);
offset = Math.max(offset, -0.3f);

return offset;
}

void transformViewRoom(View child, Transformation t, float race) {

User userInfo = AppData.getInstance().getUser();

Camera mCamera = new Camera();
mCamera.save();
final Matrix matrix = t.getMatrix();
final int halfHeight = child.getMeasuredHeight() >> 1;
final int halfWidth = child.getMeasuredWidth() >> 1;
// 平移 X、Y、Z 轴已达到立体效果
mCamera.translate(-race * 50, 0.0f, Math.abs(race) * 200);
// 也可设置旋转效果
mCamera.getMatrix(matrix);
// 获取当前屏幕的宽高来缩小当前的item
if (userInfo.getWidth() == 800 && userInfo.getHeight() == 480) {
matrix.postScale(0.9f, 0.9f);
}
// 以 child 的中心点变换
matrix.preTranslate(-halfWidth, -halfHeight);
matrix.postTranslate(halfWidth, halfHeight);
mCamera.restore();
// 设置 alpha 变换
t.setAlpha(1 - Math.abs(race));
}

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
if (android.os.Build.VERSION.SDK_INT > 15) {
if (child.getAnimation() == null) {
TransformationAnimation ta = new TransformationAnimation(child);
child.setAnimation(ta);
}
}

return super.drawChild(canvas, child, drawingTime);
}

final class TransformationAnimation extends Animation {
View v;

TransformationAnimation(View _v) {
v = _v;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
getChildStaticTransformation(v, t);
}
}

void getTransformationMatrix(View child, float offset) {
final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);
final int halfHeight = child.getMeasuredHeight() >> 1;

mCamera.save();
mCamera.translate(-offset * 50, 0.0f, Math.abs(offset) * 200);

mCamera.getMatrix(mMatrix);
mCamera.restore();
mMatrix.preTranslate(-halfWidth, -halfHeight);
mMatrix.postTranslate(halfWidth, halfHeight);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
/**
* return false; 只可以滑动一张图片
* */
return super.onFling(e1, e2, velocityX, velocityY);
}
}
[size=large][size=x-small][/size][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值