iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Android gallery 3D效果
  • 821
分享到

Android gallery 3D效果

效果Androidgallery 2023-01-31 06:01:40 821人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

转载来自:Http://blog.csdn.net/leehong2005/article/details/8070538   在看了iOS上面的CoverFlow后,感觉效果真的不错,就想在Android上面实现一个,这个程序在网上参考了

转载来自:Http://blog.csdn.net/leehong2005/article/details/8070538  

在看了iOS上面的CoverFlow后,感觉效果真的不错,就想在Android上面实现一个,这个程序在网上参考了一此核心的代码,当然我添加了一些其他的东西,废话不多说,先看效果,不然就是无图无真相。



其实实现这个效果很简单,下面作一个简单的介绍


一,创建倒影效果

这个基本思路是:

1.创建一个源图一样的图,利用martrix将图片旋转180度。这个倒影图的高是源图的一半。


Matrix matrix = new Matrix();
// 1表示放大比例,不放大也不缩小。
// -1表示在y轴上相反,即旋转180度。
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(
    srcBitmap,
    0,
    srcBitmap.getHeight() / 2,  // top为源图的一半
    srcBitmap.getWidth(),       // 宽度与源图一样
    srcBitmap.getHeight() / 2,  // 高度与源图的一半
    matrix,
    false);


2,创建一个最终效果的图,即源图 + 间隙 + 倒影。

final int REFLECTION_GAP = 5;
Bitmap bitmapWithReflection = Bitmap.createBitmap(
       reflectionWidth,
       srcHeight + reflectionHeight + REFLECTION_GAP,
       Config.ARGB_8888);

3,依次将源图、倒影图绘制在最终的bitmap上面。

// Prepare the canvas to draw stuff.
Canvas canvas = new Canvas(bitmapWithReflection);
                                                                        
// Draw the original bitmap.
canvas.drawBitmap(srcBitmap, 0, 0, null);
                                                                        
// Draw the reflection bitmap.
canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);

4,创建LinearGradient,从而给定一个由上到下的渐变色。

Paint paint = new Paint();
paint.setAntiAlias(true);
LinearGradient shader = new LinearGradient(
        0,
        srcHeight,
        0,
        bitmapWithReflection.getHeight() + REFLECTION_GAP,
        0x70FFFFFF,
        0x00FFFFFF,
        TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
// Draw the linear shader.
canvas.drawRect(
        0,
        srcHeight,
        srcWidth,
        bitmapWithReflection.getHeight() + REFLECTION_GAP,
        paint);

二,扩展Gallery

扩展系统的gallery,我们需要重写两个方法,getChildStaticTransfORMation()和getChildDrawinGorder(),同时,要使这两个方法能被调用,必须执行如下两行代码,文档上面是有说明的。

// Enable set transformation.
this.setStaticTransformationsEnabled(true);
// Enable set the children drawing order.
this.setChildrenDrawingOrderEnabled(true);

  • getChildDrawingOrder的实现

@Override
protected int getChildDrawingOrder(int childCount, int i)
{
    // Current selected index.
    int selectedIndex = getSelectedItemPosition() - getFirstVisiblePosition();
    if (selectedIndex < 0)
    {
        return i;
    }
                                        
    if (i < selectedIndex)
    {
        return i;
    }
    else if (i >= selectedIndex)
    {
        return childCount - 1 - i + selectedIndex;
    }
    else
    {
        return i;
    }
}


这里为什么要计算drawing order,因为从上图中看到,我们的效果是:中间左边的顺序是 0, 1, 2,右边的child覆盖左边的child,而在中间右边的顺序正好相反,左边的覆盖右边的,所以我们要重写这个方法,而gallery自身的实现,不是这种效果。

  • getChildStaticTransformation的实现

@Override
protected boolean getChildStaticTransformation(View child, Transformation t)
{
    super.getChildStaticTransformation(child, t);
                               
    final int childCenter = getCenterOfView(child);
    final int childWidth  = child.getWidth();
                               
    int rotationAngle = 0;
    t.clear();
    t.setTransformationType(Transformation.TYPE_MATRIX);
                               
    // If the child is in the center, we do not rotate it.
    if (childCenter == mCoveflowCenter)
    {
        transformImageBitmap(child, t, 0);
    }
    else
    {
        // Calculate the rotation angle.
        rotationAngle = (int)(((float)(mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
                                   
        // Make the angle is not bigger than maximum.
        if (Math.abs(rotationAngle) > mMaxRotationAngle)
        {
            rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
        }
                                   
        transformImageBitmap(child, t, rotationAngle);
    }
                               
    return true;
}

这个方法就是根据child来计算它的transformation(变换),我们需要去修改它里面的matrix,从而达到旋转的效果。根据位置和角度来计算的matrix的方法写在另外一个方法transformImageBitmap中实现。


  • transformImageBitmap()的实现

private void transformImageBitmap(View child, Transformation t, int rotationAngle)
{
    mCamera.save();
                      
    final Matrix p_w_picpathMatrix = t.getMatrix();
    final int p_w_picpathHeight = child.getHeight();
    final int p_w_picpathWidth  = child.getWidth();
    final int rotation    = Math.abs(rotationAngle);
                      
    // Zoom on Z axis.
    mCamera.translate(0, 0, mMaxZoom);
                      
    if (rotation < mMaxRotationAngle)
    {
        float zoomAmount = (float)(mMaxZoom + rotation * 1.5f);
        mCamera.translate(0, 0, zoomAmount);
    }
                      
    // Rotate the camera on Y axis.
    mCamera.rotateY(rotationAngle);
    // Get the matrix from the camera, in fact, the matrix is S (scale) transformation.
    mCamera.getMatrix(p_w_picpathMatrix);
                      
    // The matrix final is T2 * S * T1, first translate the center point to (0, 0),
    // then scale, and then translate the center point to its original point.
    // T * S * T
                      
    // S * T1
    p_w_picpathMatrix.postTranslate((p_w_picpathWidth / 2), (p_w_picpathHeight / 2));
    // (T2 * S) * T1
    p_w_picpathMatrix.preTranslate(-(p_w_picpathWidth / 2), -(p_w_picpathHeight / 2));
                      
    mCamera.restore();
}

这里,简单说明一个,


       第一,先在Z轴上平称,其实就是得到一个缩放矩阵变换,我这里简写为 S。

       第二,是利用camera这个类来生成matrix,其实mCamera.rotateY就是围绕Y轴旋转。这里生成了一个旋转矩阵,记为 R 。经过这两步,此时调用mCamera.getMatrix(p_w_picpathMatrix); 从Camera中得到matrix,此时这个矩阵中包含了S * R。

       第三,最关键是下面两句      

// S * T1
 p_w_picpathMatrix.postTranslate((p_w_picpathWidth / 2), (p_w_picpathHeight / 2));
 // (T2 * S) * T1
 p_w_picpathMatrix.preTranslate(-(p_w_picpathWidth / 2), -(p_w_picpathHeight / 2));

于这里涉及到旋转与缩放,缩放操作其实应该是针对Child中点进行了,这里就是作一个平衡操作,我们必须是先平移,再缩放,再平移回原来位置,所以,我们最终的矩阵变换应该是这样的:

       M = T * (S * R) * T1   (这里在T1表示与T相反)。

三,完整代码

GalleryFlow.java

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;
public class GalleryFlow extends Gallery
{
    
    private Camera mCamera = new Camera();
          
    
    private int mMaxRotationAngle = 60;
          
    
    private int mMaxZoom = -120;
          
    
    private int mCoveflowCenter = 0;
          
    public GalleryFlow(Context context)
    {
        this(context, null);
    }
          
    public GalleryFlow(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
          
    public GalleryFlow(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
              
        // Enable set transformation.
        this.setStaticTransformationsEnabled(true);
        // Enable set the children drawing order.
        this.setChildrenDrawingOrderEnabled(true);
    }
          
    public int getMaxRotationAngle()
    {
        return mMaxRotationAngle;
    }
          
    public void setMaxRotationAngle(int maxRotationAngle)
    {
        mMaxRotationAngle = maxRotationAngle;
    }
          
    public int getMaxZoom()
    {
        return mMaxZoom;
    }
          
    public void setMaxZoom(int maxZoom)
    {
        mMaxZoom = maxZoom;
    }
          
    @Override
    protected int getChildDrawingOrder(int childCount, int i)
    {
        // Current selected index.
        int selectedIndex = getSelectedItemPosition() - getFirstVisiblePosition();
        if (selectedIndex < 0)
        {
            return i;
        }
              
        if (i < selectedIndex)
        {
            return i;
        }
        else if (i >= selectedIndex)
        {
            return childCount - 1 - i + selectedIndex;
        }
        else
        {
            return i;
        }
    }
          
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        mCoveflowCenter = getCenterOfCoverflow();
        super.onSizeChanged(w, h, oldw, oldh);
    }
          
    private int getCenterOfView(View view)
    {
        return view.getLeft() + view.getWidth() / 2;
    }
          
    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t)
    {
        super.getChildStaticTransformation(child, t);
              
        final int childCenter = getCenterOfView(child);
        final int childWidth  = child.getWidth();
              
        int rotationAngle = 0;
        t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);
              
        // If the child is in the center, we do not rotate it.
        if (childCenter == mCoveflowCenter)
        {
            transformImageBitmap(child, t, 0);
        }
        else
        {
            // Calculate the rotation angle.
            rotationAngle = (int)(((float)(mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
                  
            // Make the angle is not bigger than maximum.
            if (Math.abs(rotationAngle) > mMaxRotationAngle)
            {
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
            }
                  
            transformImageBitmap(child, t, rotationAngle);
        }
              
        return true;
    }
          
    private int getCenterOfCoverflow()
    {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }
          
    private void transformImageBitmap(View child, Transformation t, int rotationAngle)
    {
        mCamera.save();
              
        final Matrix p_w_picpathMatrix = t.getMatrix();
        final int p_w_picpathHeight = child.getHeight();
        final int p_w_picpathWidth  = child.getWidth();
        final int rotation    = Math.abs(rotationAngle);
              
        // Zoom on Z axis.
        mCamera.translate(0, 0, mMaxZoom);
              
        if (rotation < mMaxRotationAngle)
        {
            float zoomAmount = (float)(mMaxZoom + rotation * 1.5f);
            mCamera.translate(0, 0, zoomAmount);
        }
              
        // Rotate the camera on Y axis.
        mCamera.rotateY(rotationAngle);
        // Get the matrix from the camera, in fact, the matrix is S (scale) transformation.
        mCamera.getMatrix(p_w_picpathMatrix);
              
        // The matrix final is T2 * S * T1, first translate the center point to (0, 0),
        // then scale, and then translate the center point to its original point.
        // T * S * T
              
        // S * T1
        p_w_picpathMatrix.postTranslate((p_w_picpathWidth / 2), (p_w_picpathHeight / 2));
        // (T2 * S) * T1
        p_w_picpathMatrix.preTranslate(-(p_w_picpathWidth / 2), -(p_w_picpathHeight / 2));
              
        mCamera.restore();
    }
}

BitmapUtil.java

package com.lee.gallery3D.utils;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
public class BitmapUtil
{
    public static Bitmap createReflectedBitmap(Bitmap srcBitmap)
    {
        if (null == srcBitmap)
        {
            return null;
        }
          
        // The gap between the reflection bitmap and original bitmap.
        final int REFLECTION_GAP = 4;
          
        int srcWidth  = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        int reflectionWidth  = srcBitmap.getWidth();
        int reflectionHeight = srcBitmap.getHeight() / 2;
          
        if (0 == srcWidth || srcHeight == 0)
        {
            return null;
        }
          
        // The matrix
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
          
        try
        {
            // The reflection bitmap, width is same with original's, height is half of original's.
            Bitmap reflectionBitmap = Bitmap.createBitmap(
                    srcBitmap,
                    0,
                    srcHeight / 2,
                    srcWidth,
                    srcHeight / 2,
                    matrix,
                    false);
              
            if (null == reflectionBitmap)
            {
                return null;
            }
              
            // Create the bitmap which contains original and reflection bitmap.
            Bitmap bitmapWithReflection = Bitmap.createBitmap(
                    reflectionWidth,
                    srcHeight + reflectionHeight + REFLECTION_GAP,
                    Config.ARGB_8888);
              
            if (null == bitmapWithReflection)
            {
                return null;
            }
              
            // Prepare the canvas to draw stuff.
            Canvas canvas = new Canvas(bitmapWithReflection);
              
            // Draw the original bitmap.
            canvas.drawBitmap(srcBitmap, 0, 0, null);
              
            // Draw the reflection bitmap.
            canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
              
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            LinearGradient shader = new LinearGradient(
                    0,
                    srcHeight,
                    0,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP,
                    0x70FFFFFF,
                    0x00FFFFFF,
                    TileMode.MIRROR);
            paint.setShader(shader);
            paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
              
            // Draw the linear shader.
            canvas.drawRect(
                    0,
                    srcHeight,
                    srcWidth,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP,
                    paint);
              
            return bitmapWithReflection;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
          
        return null;
    }
}





--结束END--

本文标题: Android gallery 3D效果

本文链接: https://www.lsjlt.com/news/189448.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android gallery 3D效果
    转载来自:http://blog.csdn.net/leehong2005/article/details/8070538   在看了iOS上面的CoverFlow后,感觉效果真的不错,就想在android上面实现一个,这个程序在网上参考了...
    99+
    2023-01-31
    效果 Android gallery
  • Android怎么使用ViewPager实现画廊Gallery效果
    本篇内容介绍了“Android怎么使用ViewPager实现画廊Gallery效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在手机QQ上...
    99+
    2023-06-19
  • Android实现3D滚动效果
    先上效果图 下载链接http://down.51cto.com/data/1076318...
    99+
    2023-01-31
    效果 Android
  • Android怎么实现3D界面效果
    要实现3D界面效果,可以通过以下几种方法:1. 使用OpenGL ES:Android支持OpenGL ES库,可以使用OpenGL...
    99+
    2023-08-24
    Android
  • 如何在Android应用中实现一个Gallery画廊效果
    这期内容当中小编将会给大家带来有关如何在Android应用中实现一个Gallery画廊效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。画廊 使用Gallery表示,按水平方向显示内容,并且可以用手指直接...
    99+
    2023-05-31
    android gallery roi
  • Android Flutter如何实现3D动画效果
    这篇文章主要讲解了“Android Flutter如何实现3D动画效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android Flutter如何实现3D动画效果”吧...
    99+
    2023-06-29
  • 详解Android 裸眼3D效果View控件
    描述:这是一个裸眼3D效果的控件View。 Tips:本项目代码部分逻辑参考于其他文章(自如的3D裸眼实现),众人拾柴火焰高,希望大家能多多补充。 项目代码:https://gite...
    99+
    2024-04-02
  • 打造最酷3D效果的Android界面
    要打造最酷的3D效果的Android界面,可以遵循以下步骤:1. 使用OpenGL ES或者Android的3D框架(如Unity3...
    99+
    2023-08-24
    Android
  • Android TV 3D卡片无限循环效果
    TV 3D卡片无限循环效果,供大家参考,具体内容如下 ##前言 1、需求:实现3个卡片实现无限循环效果:1-2-3-1-2-3-1…,而且要实现3D效果:中间突出,两侧呈角度显示 2...
    99+
    2024-04-02
  • Android集成Unity,实现3D看房效果
    引子 前几天有人找小编问能不能把3D模型放入到Unity中,再把Unity放入到Android APP中,在APP中实现观看房屋家具的功能,这次小编便来分享一下吧,如果还需要了解Android 集成Unity知识的,可以翻我主页其他文章 ...
    99+
    2023-08-31
    android unity 3d
  • Android中怎么通过自定义RecyclerView控件实现Gallery效果
    这期内容当中小编将会给大家带来有关Android中怎么通过自定义RecyclerView控件实现Gallery效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1、RecyclerView的基本用法首先主...
    99+
    2023-05-30
    android recyclerview gallery
  • ubuntu 的3D效果
    使用ubuntu的apt下载。 这个是compiz,可以开启3D效果 sudo apt-get install compiz compiz-fusion-plugins-main compizconfig-backend-gconf com...
    99+
    2023-01-31
    效果 ubuntu
  • Android OpenGL如何实现APP裸眼3D效果
    Android OpenGL如何实现APP裸眼3D效果,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。原理简介 & OpenGL 的优势裸眼 3D 效果的本质是...
    99+
    2023-06-28
  • Android自定义view之3D正方体效果实例
    目录前言一、小提二、将传感器改成事件分发机制三、使用四、源码总结前言 在之前写了一篇关于3D效果的文章,借助传感器展示,有小伙伴问可不可以改成手势滑动操作(事件分发),所以出一篇文...
    99+
    2024-04-02
  • 3D不同的效果图
    ...
    99+
    2023-01-31
    效果图
  • 怎么实现Android TV 3D卡片无限循环效果
    这篇文章主要讲解了“怎么实现Android TV 3D卡片无限循环效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么实现Android TV 3D卡片无限循环效果”吧!##思路自定义Vi...
    99+
    2023-06-25
  • js实现3D轮播图效果
    本文实例为大家分享了js实现3D轮播图效果的具体代码,供大家参考,具体内容如下 主要有平移和旋转构成3d效果的轮播图,小白一只,不足之处还请大家多多指教,代码如下 css代码: ...
    99+
    2024-04-02
  • Unity Shader实现3D翻页效果
    本文实例为大家分享了Unity Shader实现3D翻页效果的具体代码,供大家参考,具体内容如下 参考文章:UnityShader使用Plane实现翻书效果 效果图: 原理:Sh...
    99+
    2024-04-02
  • vue+highcharts实现3D饼图效果
    本文实例为大家分享了vue+highcharts实现3D饼图效果的具体代码,供大家参考,具体内容如下 这是最终效果 <template> <div class="...
    99+
    2024-04-02
  • vue实现3D环形图效果
    本文实例为大家分享了vue实现3D环形图效果的具体代码,供大家参考,具体内容如下 1.引入highcharts 2.main.js引入highcharts import highch...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作