Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Commit

Permalink
add FlipViewController.setAnimationBitmapFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
openaphid committed Dec 28, 2012
1 parent be3545d commit b395730
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.aphidmobile.flip.demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import com.aphidmobile.flip.FlipViewController;
import com.aphidmobile.flip.demo.adapter.TravelAdapter;
Expand All @@ -35,6 +36,9 @@ public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.activity_title);

flipView = new FlipViewController(this);

//Use RGB_565 to reduce peak memory usage on large screen device
flipView.setAnimationBitmapFormat(Bitmap.Config.RGB_565);

flipView.setAdapter(new TravelAdapter(this));

Expand Down
12 changes: 6 additions & 6 deletions FlipView/FlipLibrary/src/com/aphidmobile/flip/FlipCards.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import javax.microedition.khronos.opengles.GL10;

public class FlipCards {
private static final float ACCELERATION = 1f;
private static final float ACCELERATION = 0.65f;
private static final float MOVEMENT_RATE = 1.5f;
private static final int MAX_TIP_ANGLE = 60;
private static final int MAX_TOUCH_MOVE_ANGLE = 15;
Expand Down Expand Up @@ -103,20 +103,20 @@ public void reloadTexture(int frontIndex, View frontView, int backIndex, View ba
synchronized (this) {
if (frontView != null) {
if (backCards.getView() == frontView) {
frontCards.setView(-1, null);
frontCards.setView(-1, null, controller.getAnimationBitmapFormat());
swapCards();
}
}

if (backView != null) {
if (frontCards.getView() == backView) {
backCards.setView(-1, null);
backCards.setView(-1, null, controller.getAnimationBitmapFormat());
swapCards();
}
}

boolean frontChanged = frontCards.setView(frontIndex, frontView);
boolean backChanged = backCards.setView(backIndex, backView);
boolean frontChanged = frontCards.setView(frontIndex, frontView, controller.getAnimationBitmapFormat());
boolean backChanged = backCards.setView(backIndex, backView, controller.getAnimationBitmapFormat());

if (AphidLog.ENABLE_DEBUG)
AphidLog.d("reloading texture: %s and %s; old views: %s, %s, front changed %s, back changed %s", frontView, backView, frontCards.getView(), backCards.getView(), frontChanged, backChanged);
Expand Down Expand Up @@ -278,7 +278,7 @@ public synchronized boolean handleTouchEvent(MotionEvent event, boolean isOnTouc
controller.flippedToView(activeIndex, false);
} else {
swapCards();
frontCards.setView(-1, null);
frontCards.setView(-1, null, controller.getAnimationBitmapFormat());
if (-angle >= MAX_TIP_ANGLE)
angle = -MAX_TIP_ANGLE;
angle += 180;
Expand Down
16 changes: 2 additions & 14 deletions FlipView/FlipLibrary/src/com/aphidmobile/flip/FlipRenderer.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package com.aphidmobile.flip;

import static javax.microedition.khronos.opengles.GL10.GL_AMBIENT;
import static javax.microedition.khronos.opengles.GL10.GL_COLOR_BUFFER_BIT;
import static javax.microedition.khronos.opengles.GL10.GL_DEPTH_BUFFER_BIT;
import static javax.microedition.khronos.opengles.GL10.GL_DEPTH_TEST;
import static javax.microedition.khronos.opengles.GL10.GL_LEQUAL;
import static javax.microedition.khronos.opengles.GL10.GL_LIGHT0;
import static javax.microedition.khronos.opengles.GL10.GL_LIGHTING;
import static javax.microedition.khronos.opengles.GL10.GL_MODELVIEW;
import static javax.microedition.khronos.opengles.GL10.GL_NICEST;
import static javax.microedition.khronos.opengles.GL10.GL_PERSPECTIVE_CORRECTION_HINT;
import static javax.microedition.khronos.opengles.GL10.GL_POSITION;
import static javax.microedition.khronos.opengles.GL10.GL_PROJECTION;
import static javax.microedition.khronos.opengles.GL10.GL_SMOOTH;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

Expand All @@ -26,6 +12,8 @@

import java.util.LinkedList;

import static javax.microedition.khronos.opengles.GL10.*;

/*
Copyright 2012 Aphid Mobile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.os.Handler;
Expand Down Expand Up @@ -102,6 +103,8 @@ public void onChanged() {
private float maxVelocity;

private ViewFlipListener onViewFlipListener;

private Bitmap.Config animationBitmapFormat = Bitmap.Config.ARGB_8888;

public FlipViewController(Context context) {
this(context, true);
Expand All @@ -128,6 +131,18 @@ public FlipViewController(Context context, AttributeSet attrs) {
init(context, isOrientationVertical(context, attrs));
}

public Bitmap.Config getAnimationBitmapFormat() {
return animationBitmapFormat;
}

/**
* Set the bitmap config for the animation, default is ARGB_8888, which provides the best quality with large peak memory consumption.
* @param animationBitmapFormat ALPHA_8 is not supported and will throw exception when binding textures
*/
public void setAnimationBitmapFormat(Bitmap.Config animationBitmapFormat) {
this.animationBitmapFormat = animationBitmapFormat;
}

/**
* Check the attributes for a declared orientation. (Defaults to true)
*/
Expand All @@ -138,14 +153,15 @@ private boolean isOrientationVertical(Context context, AttributeSet attrs) {
R.styleable.FlipViewController,
0, 0);
try {
vertical = a.getInteger(R.styleable.FlipViewController_orientation,
0) == 0 ? true : false;
vertical = a.getInteger(R.styleable.FlipViewController_orientation, 0) == 0;
} finally {
a.recycle();
}
return vertical;
}



private void init(Context context, boolean orientationVertical) {
ViewConfiguration configuration = ViewConfiguration.get(getContext());
touchSlop = configuration.getScaledTouchSlop();
Expand Down
4 changes: 2 additions & 2 deletions FlipView/FlipLibrary/src/com/aphidmobile/flip/GrabIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class GrabIt {
private GrabIt() {
}

public static Bitmap takeScreenshot(View view) {
public static Bitmap takeScreenshot(View view, Bitmap.Config config) {
int width = view.getWidth();
int height = view.getHeight();

if (view != null && width > 0 && height > 0) {
Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);

//canvas.drawColor(Color.RED, PorterDuff.Mode.DARKEN); //XXX: debug option

if (AphidLog.ENABLE_DEBUG)
Expand Down
33 changes: 25 additions & 8 deletions FlipView/FlipLibrary/src/com/aphidmobile/flip/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public class Texture {
private FlipRenderer renderer;

private int[] id = {0};

private int width, height;
Expand Down Expand Up @@ -58,18 +58,35 @@ public static Texture createTexture(Bitmap bitmap, FlipRenderer renderer, GL10 g

gl.glGenTextures(1, t.id, 0);
gl.glBindTexture(GL_TEXTURE_2D, t.id[0]);

gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/*
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
*/
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

switch (bitmap.getConfig()) {
case ARGB_8888:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case ARGB_4444:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case RGB_565:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case ALPHA_8:
default:
throw new RuntimeException("Unrecognized bitmap format for OpenGL texture: " + bitmap.getConfig());
}

FlipRenderer.checkError(gl);

return t;
}

public void postDestroy() {
renderer.postDestroyTexture(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public View getView() {
return viewRef != null ? viewRef.get() : null;
}

public synchronized boolean setView(int index, View view) {
public synchronized boolean setView(int index, View view, Bitmap.Config format) {
UI.assertInMainThread();

if (this.index == index
Expand All @@ -72,7 +72,7 @@ && getView() == view
if (view != null) {
viewRef = new WeakReference<View>(view);
recycleScreenshot();
screenshot = GrabIt.takeScreenshot(view);
screenshot = GrabIt.takeScreenshot(view, format);
} else {
recycleScreenshot();
}
Expand Down

0 comments on commit b395730

Please sign in to comment.