Java tutorial
package com.ridhofkr.hanacaraka; import java.util.ArrayList; import com.ridhofkr.hanacaraka.controller.LetterController; import com.ridhofkr.hanacaraka.model.Letters; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.view.animation.Transformation; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class LetterCardActivity extends Activity { private LetterController ctrl; private ArrayList<Letters> letters; private int category; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* remove title bar and notification bar */ requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); /* end remove title bar and notification bar */ setContentView(R.layout.lettercard); ctrl = new LetterController(); category = ListingLettersActivity.CURRENT_CATEGORY; TextView text = (TextView) findViewById(R.id.headertext); text.setText("informasi huruf"); text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.information_circle, 0); ctrl.makeListOfLetter(); letters = getLetters(ListingLettersActivity.CURRENT_CATEGORY); int p = 0; for (int i = 0; i < letters.size(); i++) { if (letters.get(i).name.equals(ListingLettersActivity.CURRENT_TEXT)) p = i; } ICPagerAdapter adapter = new ICPagerAdapter(letters); ViewPager pager = (ViewPager) findViewById(R.id.icviewpager); pager.setAdapter(adapter); pager.setCurrentItem(p); } private ArrayList<Letters> getLetters(int c) { ArrayList<Letters> cat = new ArrayList<Letters>(); for (int i = 0; i < ctrl.letters.size(); i++) { if (ctrl.letters.get(i).category == c) { cat.add(ctrl.letters.get(i)); } } return cat; } public void backButtonListener(View v) { super.onBackPressed(); } private void applyRotation(float start, float end, LinearLayout front, LinearLayout back, boolean flag) { final float centerX = front.getWidth() / 2.0f; final float centerY = front.getHeight() / 2.0f; final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); rotation.setDuration(300); rotation.setFillAfter(true); rotation.setInterpolator(new AccelerateInterpolator()); rotation.setAnimationListener(new DisplayNextView(flag, front, back)); LinearLayout inside = (LinearLayout) back.findViewById(R.id.backcardinside); if (flag) { String text = ((String) ((TextView) front.findViewById(R.id.cardinfotitle)).getText()); front.startAnimation(rotation); removeSoundListener(front); inside.addView(ctrl.playAnimation(LetterCardActivity.this, text, category)); } else { back.startAnimation(rotation); addSoundListener(front); inside.removeViewAt(2); } } private void addSoundListener(LinearLayout front) { final String text = ((String) ((TextView) front.findViewById(R.id.cardinfotitle)).getText()).toLowerCase() .replace('/', '_') + "_" + category; Button sound = (Button) front.findViewById(R.id.soundaksara); sound.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.e("audio", "play " + text); ctrl.playMusic(LetterCardActivity.this, text); } }); } private void removeSoundListener(LinearLayout front) { Button sound = (Button) front.findViewById(R.id.soundaksara); sound.setOnClickListener(null); } private class ICPagerAdapter extends PagerAdapter { private ArrayList<Letters> cards; public ICPagerAdapter(ArrayList<Letters> cards) { this.cards = cards; } @Override public int getCount() { return cards.size(); } private class FlipListener implements View.OnClickListener { private LinearLayout front; private LinearLayout back; private boolean flag; public FlipListener(LinearLayout front, LinearLayout back) { this.front = front; this.back = back; this.flag = true; } public void onClick(View v) { if (flag) { applyRotation(0, 90, front, back, flag); flag = false; } else { applyRotation(0, -90, front, back, flag); flag = true; } } } public Object instantiateItem(View c, int p) { LayoutInflater inflater = (LayoutInflater) c.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.lettercard_item, null); LinearLayout lview = (LinearLayout) view; LinearLayout front = (LinearLayout) inflater.inflate(R.layout.lettercard_front, null); LinearLayout back = (LinearLayout) inflater.inflate(R.layout.lettercard_back, null); lview.addView(front); lview.addView(back); TextView backanim = (TextView) back.findViewById(R.id.backcardanimation); backanim.setText(cards.get(p).name); back.setVisibility(View.GONE); // back.addView(getLetterAnimation(cards.get(p).animationName)); front.setOnClickListener(new FlipListener(front, back)); ctrl.letterImageRendering(view, cards.get(p)); ((ViewPager) c).addView(view, 0); final String name = cards.get(p).soundExample; Button sound = (Button) findViewById(R.id.soundaksara); sound.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ctrl.playMusic(LetterCardActivity.this, name); Log.e("audio", "play " + name); } }); return view; } @Override public void destroyItem(View v, int c, Object o) { ((ViewPager) v).removeView((View) o); } @Override public boolean isViewFromObject(View v, Object o) { return v == ((View) o); } @Override public Parcelable saveState() { return null; } } /* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain a * copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ /** * An animation that rotates the view on the Y axis between two specified * angles. This animation also adds a translation on the Z axis (depth) to * improve the effect. */ public class Rotate3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera; /** * Creates a new 3D rotation on the Y axis. The rotation is defined by * its start angle and its end angle. Both angles are in degrees. The * rotation is performed around a center point on the 2D space, definied * by a pair of X and Y coordinates, called centerX and centerY. When * the animation starts, a translation on the Z axis (depth) is * performed. The length of the translation can be specified, as well as * whether the translation should be reversed in time. * * @param fromDegrees * the start angle of the 3D rotation * @param toDegrees * the end angle of the 3D rotation * @param centerX * the X center of the 3D rotation * @param centerY * the Y center of the 3D rotation * @param reverse * true if the translation should be reversed, false * otherwise */ public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; mReverse = reverse; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save(); if (mReverse) { camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); } else { camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); } camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } /** * This class listens for the end of the first half of the animation. It * then posts a new action that effectively swaps the views when the * container is rotated 90 degrees and thus invisible. */ private final class DisplayNextView implements Animation.AnimationListener { private boolean mPosition; LinearLayout front; LinearLayout back; private DisplayNextView(boolean position, LinearLayout front, LinearLayout back) { mPosition = position; this.front = front; this.back = back; } public void onAnimationStart(Animation animation) { } public void onAnimationEnd(Animation animation) { front.post(new SwapViews(mPosition, front, back)); } public void onAnimationRepeat(Animation animation) { } } /** * This class is responsible for swapping the views and start the second * half of the animation. */ private final class SwapViews implements Runnable { private boolean mPosition; LinearLayout front; LinearLayout back; public SwapViews(boolean position, LinearLayout front, LinearLayout back) { mPosition = position; this.front = front; this.back = back; } public void run() { final float centerX = front.getWidth() / 2.0f; final float centerY = front.getHeight() / 2.0f; Rotate3dAnimation rotation; if (mPosition) { front.setVisibility(View.GONE); // front.setEnabled(false); // back.setEnabled(true); back.setVisibility(View.VISIBLE); back.requestFocus(); rotation = new Rotate3dAnimation(-90, 0, centerX, centerY, 310.0f, false); } else { back.setVisibility(View.GONE); // back.setEnabled(false); // front.setEnabled(true); front.setVisibility(View.VISIBLE); front.requestFocus(); rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false); } rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new DecelerateInterpolator()); if (mPosition) { back.startAnimation(rotation); } else { front.startAnimation(rotation); } } } }