自定义控件:3D画廊Gallery

本文介绍如何在API级别16后替代废弃的Gallery控件,使用HorizontalScrollView和ViewPager等组件创建3D画廊效果。同时,探讨了ImageUtils和MainActivity在实现过程中的关键作用。

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

gallery 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 ? 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值