Skip to content

Instantly share code, notes, and snippets.

@shekaroppo
Created July 18, 2015 07:06
Show Gist options
  • Save shekaroppo/efef2013f47d4adf30d8 to your computer and use it in GitHub Desktop.
Save shekaroppo/efef2013f47d4adf30d8 to your computer and use it in GitHub Desktop.
package com.example.samsungflip;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SamsungFlipActivity extends Activity {
private RelativeLayout mImage0;
private RelativeLayout mImage1;
private RelativeLayout mImage2;
private RelativeLayout mImage3;
privateintmState;
privateintupAngle;
privateintinitialYPoint;
private RelativeLayout mTouchedImageView;
privatebooleanisActionMoveCompleted=false;
private ArrayList<RelativeLayout> mImageIDArray;
private BitmapDrawable[] bitmapArray;
private BitmapDrawable mBitmap_a1;
private BitmapDrawable mBitmap_a2;
private BitmapDrawable mBitmap_a3;
private BitmapDrawable mBitmap_a4;
private BitmapDrawable mBitmap_b1;
private BitmapDrawable mBitmap_b2;
private BitmapDrawable mBitmap_b3;
private BitmapDrawable mBitmap_b4;
private BitmapDrawable mBitmap_c1;
private BitmapDrawable mBitmap_c2;
private BitmapDrawable mBitmap_c3;
private BitmapDrawable mBitmap_c4;
privateintdownAngle;
private TextView mTitle1;
private TextView mTitle2;
private TextView mTitle3;
private TextView mTitle4;
private TextView mSubTitle1;
private TextView mSubTitle2;
private TextView mSubTitle3;
private TextView mSubTitle4;
private TextView mSource1;
private TextView mSource2;
private TextView mSource3;
private TextView mSource4;
doublenumerator=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_blank_food);
ActionBar actionBar = getActionBar();
actionBar.hide();
mImageIDArray=new ArrayList<RelativeLayout>();
bitmapArray=new BitmapDrawable[4];
mImage0 = (RelativeLayout) findViewById(R.id.image0);
mImage1 = (RelativeLayout) findViewById(R.id.image1);
mImage2 = (RelativeLayout) findViewById(R.id.image2);
mImage3 = (RelativeLayout) findViewById(R.id.image3);
mImageIDArray.add(mImage0);
mImageIDArray.add(mImage1);
mImageIDArray.add(mImage2);
mImageIDArray.add(mImage3);
mTitle1 = (TextView) findViewById(R.id.title1);
mTitle2 = (TextView) findViewById(R.id.title2);
mTitle3 = (TextView) findViewById(R.id.title3);
mTitle4 = (TextView) findViewById(R.id.title4);
mSubTitle1 = (TextView) findViewById(R.id.subtitle1);
mSubTitle2 = (TextView) findViewById(R.id.subtitle2);
mSubTitle3 = (TextView) findViewById(R.id.subtitle3);
mSubTitle4 = (TextView) findViewById(R.id.subtitle4);
mSource1 = (TextView) findViewById(R.id.source1);
mSource2 = (TextView) findViewById(R.id.source2);
mSource3 = (TextView) findViewById(R.id.source3);
mSource4 = (TextView) findViewById(R.id.source4);
mState = 0;
Resources res = getResources();
mBitmap_a1 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_a1));
mBitmap_a2 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_a2));
mBitmap_a3 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_a3));
mBitmap_a4 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_a4));
mBitmap_b1 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_b1));
mBitmap_b2 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_b2));
mBitmap_b3 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_b3));
mBitmap_b4 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_b4));
mBitmap_c1 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_c1));
mBitmap_c2 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_c2));
mBitmap_c3 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_c3));
mBitmap_c4 = new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.elastic_c4));
bitmapArray[0]=mBitmap_a1;
bitmapArray[0]=mBitmap_a2;
bitmapArray[0]=mBitmap_a3;
bitmapArray[1]=mBitmap_a4;
setImageState();
}
public void onDestroy() {
/**
mBitmap_a1.recycle();
mBitmap_a2.recycle();
mBitmap_a3.recycle();
mBitmap_a4.recycle();
mBitmap_b1.recycle();
mBitmap_b2.recycle();
mBitmap_b3.recycle();
mBitmap_b4.recycle();
mBitmap_c1.recycle();
mBitmap_c2.recycle();
mBitmap_c3.recycle();
mBitmap_c4.recycle();
**/
super.onDestroy();
}
private void setImageState() {
mImage0.setBackground(mBitmap_a1); // setImageBitmap(mBitmap_a1);
mTitle1.setText(R.string.title_a1);
mSubTitle1.setText(R.string.subtitle_a1);
mSource1.setText(R.string.source_a1);
mImage1.setBackground(mBitmap_a2); // setImageBitmap(mBitmap_a2);
mTitle2.setText(R.string.title_a2);
mSubTitle2.setText(R.string.subtitle_a2);
mSource2.setText(R.string.source_a2);
mImage2.setBackground(mBitmap_a3); // setImageBitmap(mBitmap_a3);
mTitle3.setText(R.string.title_a3);
mSubTitle3.setText(R.string.subtitle_a3);
mSource3.setText(R.string.source_a3);
mImage3.setBackground(mBitmap_a4); // setImageBitmap(mBitmap_a4);
mTitle4.setText(R.string.title_a4);
mSubTitle4.setText(R.string.subtitle_a4);
mSource4.setText(R.string.source_a4);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP){
if(isActionMoveCompleted){
isActionMoveCompleted=false;
}else{
if(initialYPoint>event.getY()){
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), upAngle, 0,numerator);
}
else{
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), downAngle, 0,numerator);
}
}
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initialYPoint=(int) event.getY();
upAngle=0;
downAngle=0;
mTouchedImageView = getTouchedImageVIew(event.getX(), event.getY());
}
if (event.getAction() == MotionEvent.ACTION_MOVE && !isActionMoveCompleted) {
if(!isPointInsideView(event.getX(), event.getY(), mTouchedImageView)&&!isActionMoveCompleted){
isActionMoveCompleted=true;
//updateImageBitmapState();
mState = (mState +1) % 3;
if(initialYPoint>event.getY()){
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), -90, 0,numerator);
}
else{
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), 90, 0,numerator);
}
}
else if(isPointInsideView(event.getX(), event.getY(), mTouchedImageView)) {
if(Math.abs(initialYPoint-event.getY())>=mTouchedImageView.getHeight()/2&&!isActionMoveCompleted){
isActionMoveCompleted=true;
//updateImageBitmapState();
mState = (mState +1) % 3;
if(initialYPoint>event.getY()){
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), -90, 0,numerator);
}
else{
autoCompleteAnimation(mImageIDArray.indexOf(mTouchedImageView), 90, 0,numerator);
}
}
else{
numerator = event.getY() - mTouchedImageView.getY()+mTouchedImageView.getHeight();
if(initialYPoint>event.getY()){
if(upAngle <= 90){
animateAllViewsUp(mImageIDArray.indexOf(mTouchedImageView),upAngle,(int) (upAngle+(numerator/50)), numerator);
upAngle=(int)(upAngle+numerator/50);
}
}
else{
if(downAngle >=-90){
animateAllViewsDown(mImageIDArray.indexOf(mTouchedImageView),downAngle,(int) (int) (downAngle-(numerator/50)), numerator);
downAngle=(int)(downAngle-numerator/50);
}
}
}
}
}
returntrue;
}
private void autoCompleteAnimation(int indexID, int oldAngle, int newAngle, double numerator){
Log.d("Num=",numerator+"");
Log.d("old=",oldAngle+"");
Log.d("new=",newAngle+"");
for(int i=0;i<4;i++){
RelativeLayout v=((mImageIDArray.get((indexID+i)%4)));
int buff_oldAngle = oldAngle - i * (int) (numerator/50) ;
int buff_newAngle = newAngle - i * (int) (numerator/50) ;
if(oldAngle == 90 || oldAngle == -90 || newAngle == 0){
buff_oldAngle = oldAngle;
buff_newAngle = newAngle;
}
//if(i<3){
flipViewAnimation(v, bitmapArray[mState],i*100, buff_oldAngle, buff_newAngle);
//}
//else if(isActionMoveCompleted){
//flipViewReverseAnimation(v, bitmapArray[mState],0, 0, 90);
////flipViewAnimation(v, bitmapArray[mState],i*100, -90, 0);
//}
}
updateImageBitmapState();
}
private void flipViewReverseAnimation(final RelativeLayout imageView, final BitmapDrawable endBitmap,
int delay,int startangle,int endangle) {
int centerX = imageView.getWidth() / 2;
int centerY = imageView.getHeight() / 2;
Resources res = getResources();
final FlipAnimation endFlipAnimation = new FlipAnimation(startangle, endangle,
centerX, centerY);
endFlipAnimation.setDuration(res.getInteger(R.dimen.animation_time));
endFlipAnimation.setFillAfter(true);
endFlipAnimation.setStartOffset(delay);
endFlipAnimation.setInterpolator(this,
android.R.anim.linear_interpolator);
endFlipAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
flipViewAnimation(imageView, bitmapArray[mState],0, -90, 0);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
imageView.startAnimation(endFlipAnimation);
}
private void animateAllViewsUp(int indexID, int oldAngle, int newAngle, double numerator){
if(oldAngle == 90)
return ;
for(int i=0;i<4;i++){
RelativeLayout v=((mImageIDArray.get((indexID+i)%4)));
int buff_oldAngle = oldAngle - i * (int) (numerator/50) ;
int buff_newAngle = newAngle - i * (int) (numerator/50) ;
flipViewAnimation(v, bitmapArray[mState],0, buff_oldAngle, buff_newAngle);
}
}
private void animateAllViewsDown(int indexID, int oldAngle, int newAngle, double numerator){
if(oldAngle == -90)
return ;
for(int i=0;i<4;i++){
RelativeLayout v=((mImageIDArray.get((indexID+i)%4)));
int buff_oldAngle = oldAngle + i * (int) (numerator/50) ;
int buff_newAngle = newAngle + i * (int) (numerator/50) ;
flipViewAnimation(v, bitmapArray[mState],0, buff_oldAngle, buff_newAngle);
}
}
private RelativeLayout getTouchedImageVIew(float x, float y) {
if (isPointInsideView(x, y, mImage0)) {
returnmImage0;
} else if (isPointInsideView(x, y, mImage1)) {
returnmImage1;
} else if (isPointInsideView(x, y, mImage2)) {
returnmImage2;
} else {
returnmImage3;
}
}
private boolean isPointInsideView(float x, float y, View view) {
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
// point is inside view bounds
if ((x > viewX && x < (viewX + view.getWidth()))
&& (y > viewY && y < (viewY + view.getHeight()))) {
returntrue;
} else {
returnfalse;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blank_food, menu);
returntrue;
}
private void flipViewAnimation(final RelativeLayout imageView, final BitmapDrawable endBitmap,
int delay,int startangle,int endangle) {
int centerX = imageView.getWidth() / 2;
int centerY = imageView.getHeight() / 2;
Resources res = getResources();
final FlipAnimation endFlipAnimation = new FlipAnimation(startangle, endangle,
centerX, centerY);
endFlipAnimation.setDuration(res.getInteger(R.dimen.animation_time));
endFlipAnimation.setFillAfter(true);
endFlipAnimation.setStartOffset(delay);
endFlipAnimation.setInterpolator(this,
android.R.anim.linear_interpolator);
imageView.startAnimation(endFlipAnimation);
}
private void updateImageBitmapState() {
switch (mState) {
case 0:
mImage0.setBackground(mBitmap_b1); // setImageBitmap(mBitmap_b1);
mTitle1.setText(R.string.title_b1);
mSubTitle1.setText(R.string.subtitle_b1);
mSource1.setText(R.string.source_b1);
mImage1.setBackground(mBitmap_b2); // setImageBitmap(mBitmap_b2);
mTitle2.setText(R.string.title_b2);
mSubTitle2.setText(R.string.subtitle_b2);
mSource2.setText(R.string.source_b2);
mImage2.setBackground(mBitmap_b3); // setImageBitmap(mBitmap_b3);
mTitle3.setText(R.string.title_b3);
mSubTitle3.setText(R.string.subtitle_b3);
mSource3.setText(R.string.source_b3);
mImage3.setBackground(mBitmap_b4); // setImageBitmap(mBitmap_b4);
mTitle4.setText(R.string.title_b4);
mSubTitle4.setText(R.string.subtitle_b4);
mSource4.setText(R.string.source_b4);
break;
case 1:
mImage0.setBackground(mBitmap_c1); // setImageBitmap(mBitmap_c1);
mTitle1.setText(R.string.title_c1);
mSubTitle1.setText(R.string.subtitle_c1);
mSource1.setText(R.string.source_c1);
mImage1.setBackground(mBitmap_c2); // setImageBitmap(mBitmap_c2);
mTitle2.setText(R.string.title_c2);
mSubTitle2.setText(R.string.subtitle_c2);
mSource2.setText(R.string.source_c2);
mImage2.setBackground(mBitmap_c3); // setImageBitmap(mBitmap_c3);
mTitle3.setText(R.string.title_c3);
mSubTitle3.setText(R.string.subtitle_c3);
mSource3.setText(R.string.source_c3);
mImage3.setBackground(mBitmap_c4); // setImageBitmap(mBitmap_c4);
mTitle4.setText(R.string.title_c4);
mSubTitle4.setText(R.string.subtitle_c4);
mSource4.setText(R.string.source_c4);
break;
case 2:
mImage0.setBackground(mBitmap_a1); // setImageBitmap(mBitmap_a1);
mTitle1.setText(R.string.title_a1);
mSubTitle1.setText(R.string.subtitle_a1);
mSource1.setText(R.string.source_a1);
mImage1.setBackground(mBitmap_a2); // setImageBitmap(mBitmap_a2);
mTitle2.setText(R.string.title_a2);
mSubTitle2.setText(R.string.subtitle_a2);
mSource2.setText(R.string.source_a2);
mImage2.setBackground(mBitmap_a3); // setImageBitmap(mBitmap_a3);
mTitle3.setText(R.string.title_a3);
mSubTitle3.setText(R.string.subtitle_a3);
mSource3.setText(R.string.source_a3);
mImage3.setBackground(mBitmap_a4); // setImageBitmap(mBitmap_a4);
mTitle4.setText(R.string.title_a4);
mSubTitle4.setText(R.string.subtitle_a4);
mSource4.setText(R.string.source_a4);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment