This is a mirror of official site: http://jasper-net.blogspot.com/

Android 3D Carousel

| Tuesday, February 22, 2011
Introduction

For a while I was looking for 3D carousel control for Android platform. The only one I found was UltimateFaves at [1]. But as it turned out it uses OpenGL. And it’s not open source. I thought if it is possible to avoid a use of OpenGL. Continuing my investigations I stamped on Coverflow Widget at [2]. And it uses standard Android 2D libraries. So the idea was the same – to use Gallery class for the carousel. The Coverflow Widget just rotates images and I wanted to rotate all group of them. Well, at least it implies the use of simple trig methods. More complicated stuff goes with the Gallery class. If you’d look through the article about Coverflow Widget at [3] you’d see a bunch of problems, such as unavailability of default scope variables in AbsSpinner and AdapterView classes. So I went the same way and rewrote some classes. And the Scroller class will be replaced by the Rotator class wich looks like Scroller but it rotates the group of images.

The Preparations

At first we should decide what parameters will define a behavior of our Carousel. For example a min quantity of items in the carousel. It will not look nice if it has only one or two items, won’t it? As for performance issue we have to define max quantity of items. Also we will need max theta angle for the carousel, what items will be in there, current selected item and if items will be reflected. So let’s define them in attrs.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="Carousel">
<attr name="android:gravity" />
<attr name="android:animationDuration" />
<attr name="UseReflection" format="boolean"/>
<attr name="Items" format="integer"/>
<attr name="SelectedItem" format="integer"/>
<attr name="maxTheta" format="float"/>
<attr name="minQuantity" format="integer"/>
<attr name="maxQuantity" format="integer"/>
</declare-styleable>
</resources>

The Carousel Item Class

To simplify some stuff with carousel I’ve created CarouselImageView

public class CarouselImageView extends ImageView implements Comparable<carouselimageview> 

{
private int index;
private float currentAngle;
private float x;
private float y;
private float z;
private boolean drawn;

public CarouselImageView(Context context) {
this(context, null, 0);
}

public CarouselImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

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

public int compareTo(CarouselImageView another) {
return (int)(another.z – this.z);
}

Read more: Codeproject

Posted via email from Jasper-net

0 comments: