Thursday, May 22, 2014

Code Examples of TranslateAnimation

An animation that controls the position of an object. See th android.view.animation description for details and sample code.
  • android.view.animation.TranslateAnimation

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.replica.replicaisland;
  17. import java.lang.reflect.InvocationTargetException;
  18. import android.app.Activity;
  19. import android.content.Intent;
  20. import android.graphics.drawable.AnimationDrawable;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.util.DisplayMetrics;
  25. import android.view.MotionEvent;
  26. import android.view.View;
  27. import android.view.animation.Animation;
  28. import android.view.animation.AnimationUtils;
  29. import android.view.animation.TranslateAnimation;
  30. import android.widget.ImageView;
  31. public class AnimationPlayerActivity extends Activity {
  32. public static final int KYLE_DEATH = 0;
  33. public static final int WANDA_ENDING = 1;
  34. public static final int KABOCHA_ENDING = 2;
  35. public static final int ROKUDOU_ENDING = 3;
  36. private AnimationDrawable mAnimation;
  37. private int mAnimationType;
  38. private long mAnimationEndTime;
  39. private KillActivityHandler mKillActivityHandler = new KillActivityHandler();
  40. class KillActivityHandler extends Handler {
  41. @Override
  42. public void handleMessage(Message msg) {
  43. AnimationPlayerActivity.this.finish();
  44. if (UIConstants.mOverridePendingTransition != null) {
  45. try {
  46. UIConstants.mOverridePendingTransition.invoke(AnimationPlayerActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
  47. } catch (InvocationTargetException ite) {
  48. DebugLog.d("Activity Transition", "Invocation Target Exception");
  49. } catch (IllegalAccessException ie) {
  50. DebugLog.d("Activity Transition", "Illegal Access Exception");
  51. }
  52. }
  53. }
  54. public void sleep(long delayMillis) {
  55. this.removeMessages(0);
  56. sendMessageDelayed(obtainMessage(0), delayMillis);
  57. }
  58. };
  59. @Override
  60. public void onCreate(Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. final Intent callingIntent = getIntent();
  63. mAnimationType = callingIntent.getIntExtra("animation", KYLE_DEATH);
  64. if (mAnimationType == KYLE_DEATH) {
  65. setContentView(R.layout.animation_player);
  66. ImageView canvasImage = (ImageView) findViewById(R.id.animation_canvas);
  67. canvasImage.setImageResource(R.anim.kyle_fall);
  68. mAnimation = (AnimationDrawable) canvasImage.getDrawable();
  69. } else {
  70.  
  71. if (mAnimationType == WANDA_ENDING || mAnimationType == KABOCHA_ENDING) {
  72. float startX = 0.0f;
  73. DisplayMetrics metrics = new DisplayMetrics();
  74. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  75. if (mAnimationType == WANDA_ENDING) {
  76. setContentView(R.layout.good_ending_animation);
  77. startX = 200 * metrics.density;
  78. } else {
  79. setContentView(R.layout.kabocha_ending_animation);
  80. startX = -200 * metrics.density;
  81. }
  82. // HACK
  83. // the TranslateAnimation system doesn't support device independent pixels.
  84. // So for the Wanda ending and Kabocha endings, in which the game over text
  85. // scrolls in horizontally, compute the size based on the actual density of
  86. // the display and just generate the anim in code. The Rokudou animation
  87. // can be safely loaded from a file.
  88. Animation gameOverAnim = new TranslateAnimation(startX, 0, 0, 0);
  89. gameOverAnim.setDuration(6000);
  90. gameOverAnim.setFillAfter(true);
  91. gameOverAnim.setFillEnabled(true);
  92. gameOverAnim.setStartOffset(8000);
  93. View background = findViewById(R.id.animation_background);
  94. View foreground = findViewById(R.id.animation_foreground);
  95. View gameOver = findViewById(R.id.game_over);
  96. Animation foregroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer2_slide);
  97. Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer1_slide);
  98. background.startAnimation(backgroundAnim);
  99. foreground.startAnimation(foregroundAnim);
  100. gameOver.startAnimation(gameOverAnim);
  101. mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();
  102. } else if (mAnimationType == ROKUDOU_ENDING) {
  103. setContentView(R.layout.rokudou_ending_animation);
  104. View background = findViewById(R.id.animation_background);
  105. View sphere = findViewById(R.id.animation_sphere);
  106. View cliffs = findViewById(R.id.animation_cliffs);
  107. View rokudou = findViewById(R.id.animation_rokudou);
  108. View gameOver = findViewById(R.id.game_over);
  109. Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_bg);
  110. Animation sphereAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_sphere);
  111. Animation cliffsAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_cliffs);
  112. Animation rokudouAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_rokudou);
  113. Animation gameOverAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_game_over);
  114. background.startAnimation(backgroundAnim);
  115. sphere.startAnimation(sphereAnim);
  116. cliffs.startAnimation(cliffsAnim);
  117. rokudou.startAnimation(rokudouAnim);
  118. gameOver.startAnimation(gameOverAnim);
  119. mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();
  120. } else {
  121. assert false;
  122. }
  123. }
  124. // Pass the calling intent back so that we can figure out which animation just played.
  125. setResult(RESULT_OK, callingIntent);
  126. }
  127. @Override
  128. public boolean onTouchEvent(MotionEvent event) {
  129. long time = System.currentTimeMillis();
  130. if (time > mAnimationEndTime) {
  131. finish();
  132. } else {
  133. try {
  134. Thread.sleep(32);
  135. } catch (InterruptedException e) {
  136. // Safe to ignore.
  137. }
  138. }
  139. return true;
  140. }
  141. @Override
  142. public void onWindowFocusChanged(boolean hasFocus) {
  143. if (hasFocus && mAnimation != null) {
  144. mAnimation.start();
  145. mKillActivityHandler.sleep(mAnimation.getDuration(0) * mAnimation.getNumberOfFrames());
  146. }
  147. }
  148. }
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.android.apis.view;
  17. // Need the following import to get access to the app resources, since this
  18. // class is in a sub-package.
  19. import com.example.android.apis.R;
  20. import android.app.Activity;
  21. import android.os.Bundle;
  22. import android.view.View;
  23. import android.view.animation.AnimationUtils;
  24. import android.view.animation.Animation;
  25. import android.view.animation.TranslateAnimation;
  26. import android.widget.AdapterView;
  27. import android.widget.ArrayAdapter;
  28. import android.widget.Spinner;
  29. public class Animation3 extends Activity implements AdapterView.OnItemSelectedListener {
  30. private static final String[] INTERPOLATORS = {
  31. "Accelerate", "Decelerate", "Accelerate/Decelerate",
  32. "Anticipate", "Overshoot", "Anticipate/Overshoot",
  33. "Bounce"
  34. };
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.animation_3);
  39. Spinner s = (Spinner) findViewById(R.id.spinner);
  40. ArrayAdapter adapter = new ArrayAdapter(this,
  41. android.R.layout.simple_spinner_item, INTERPOLATORS);
  42. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  43. s.setAdapter(adapter);
  44. s.setOnItemSelectedListener(this);
  45. }
  46. public void onItemSelected(AdapterView parent, View v, int position, long id) {
  47. final View target = findViewById(R.id.target);
  48. final View targetParent = (View) target.getParent();
  49. Animation a = new TranslateAnimation(0.0f,
  50. targetParent.getWidth() - target.getWidth() - targetParent.getPaddingLeft() -
  51. targetParent.getPaddingRight(), 0.0f, 0.0f);
  52. a.setDuration(1000);
  53. a.setStartOffset(300);
  54. a.setRepeatMode(Animation.RESTART);
  55. a.setRepeatCount(Animation.INFINITE);
  56. switch (position) {
  57. case 0:
  58. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  59. android.R.anim.accelerate_interpolator));
  60. break;
  61. case 1:
  62. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  63. android.R.anim.decelerate_interpolator));
  64. break;
  65. case 2:
  66. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  67. android.R.anim.accelerate_decelerate_interpolator));
  68. break;
  69. case 3:
  70. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  71. android.R.anim.anticipate_interpolator));
  72. break;
  73. case 4:
  74. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  75. android.R.anim.overshoot_interpolator));
  76. break;
  77. case 5:
  78. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  79. android.R.anim.anticipate_overshoot_interpolator));
  80. break;
  81. case 6:
  82. a.setInterpolator(AnimationUtils.loadInterpolator(this,
  83. android.R.anim.bounce_interpolator));
  84. break;
  85. }
  86. target.startAnimation(a);
  87. }
  88. public void onNothingSelected(AdapterView parent) {
  89. }
  90. }
  1. /*
  2.  * Copyright (C) 2011 The LiteListen Project
  3.  *
  4.  * Licensed under the Mozilla Public Licence, version 1.1 (the "License");
  5.  * You may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.mozilla.org/MPL/MPL-1.1.html
  9.  *
  10.  * ???????????????? ??????? 2011
  11.  * ???? Mozilla Public Licence 1.1 ??????????????B????
  12.  * ????????????????????????????
  13.  * ???????????????????????????
  14.  *
  15.  *      http://www.mozilla.org/MPL/MPL-1.1.html
  16.  */
  17.  
  18. package com.galapk.litelisten;
  19.  
  20. import android.content.SharedPreferences.Editor;
  21. import android.graphics.Color;
  22. import android.util.DisplayMetrics;
  23. import android.view.Gravity;
  24. import android.view.MotionEvent;
  25. import android.view.WindowManager;
  26. import android.view.animation.Animation;
  27. import android.view.animation.TranslateAnimation;
  28. import android.widget.AbsoluteLayout;
  29. import android.widget.ImageView;
  30. import android.widget.LinearLayout;
  31. import android.widget.TextView;
  32.  
  33. @SuppressWarnings("deprecation")
  34. public class FloatLRC extends LinearLayout
  35. {
  36.     // ???????????View??????
  37.     private float DownPosX = 0;
  38.     private float DownPosY = 0;
  39.  
  40.     // ?????????????????????
  41.     private float x = 0;
  42.     private float y = 0;
  43.  
  44.     private WindowManager wm;
  45.     private WindowManager.LayoutParams layWM;
  46.     private scrMain main;
  47.     private DisplayMetrics dm;
  48.  
  49.     // ???????
  50.     ImageView imgIcon;
  51.     AbsoluteLayout layMain;
  52.     TextView txtLRC1;
  53.     TextView txtLRC2;
  54.  
  55.     public FloatLRC(scrMain main)
  56.     {
  57.         super(main);
  58.         wm = (WindowManager) main.getApplicationContext().getSystemService("window");
  59.         layWM = main.getLayWM();
  60.         this.main = main;
  61.         dm = new DisplayMetrics();
  62.  
  63.         // ??????????
  64.         imgIcon = new ImageView(main);
  65.         LinearLayout.LayoutParams layIcon = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  66.         layIcon.gravity = Gravity.CENTER_VERTICAL;
  67.         imgIcon.setLayoutParams(layIcon);
  68.  
  69.         // ??????
  70.         layMain = new AbsoluteLayout(main);
  71.         LinearLayout.LayoutParams layMainPars = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  72.         layMain.setLayoutParams(layMainPars);
  73.  
  74.         // ???????
  75.         txtLRC1 = new TextView(main);
  76.         AbsoluteLayout.LayoutParams layText1 = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 5, 0);
  77.         txtLRC1.setLayoutParams(layText1);
  78.         txtLRC1.setTextSize(22);
  79.         txtLRC1.setTextColor(Color.WHITE);
  80.         txtLRC1.setShadowLayer(1, 1, 1, Color.BLACK);
  81.         txtLRC1.setSingleLine(true);
  82.  
  83.         // ???????
  84.         txtLRC2 = new TextView(main);
  85.         AbsoluteLayout.LayoutParams layText2 = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 5, 35);
  86.         txtLRC2.setLayoutParams(layText2);
  87.         txtLRC2.setTextSize(22);
  88.         txtLRC2.setTextColor(Color.WHITE);
  89.         txtLRC2.setShadowLayer(1, 1, 1, Color.BLACK);
  90.         txtLRC2.setSingleLine(true);
  91.  
  92.         // ??View?????????
  93.         layMain.addView(txtLRC1);
  94.         layMain.addView(txtLRC2);
  95.         addView(imgIcon);
  96.         addView(layMain);
  97.     }
  98.  
  99.     /* ?????????? */
  100.     public void SetLRC(int IconResource, String Sentence1, int Color1, String Sentence2, int Color2, Long TimeGap, int ChangedLineNumber)
  101.     {
  102.         main.getWindowManager().getDefaultDisplay().getMetrics(dm);
  103.  
  104.         if (ChangedLineNumber == 1)
  105.         {
  106.             Animation anim = null;
  107.             AbsoluteLayout.LayoutParams layText2 = (AbsoluteLayout.LayoutParams) txtLRC2.getLayoutParams();
  108.             float FontWidth = Common.GetTextWidth(Sentence2, txtLRC2.getTextSize());
  109.  
  110.             layWM.width = dm.widthPixels;
  111.             layText2.x = (int) (dm.widthPixels - 75 - FontWidth);
  112.             if (FontWidth > dm.widthPixels - 80)
  113.                 anim = new TranslateAnimation(FontWidth - (dm.widthPixels - 80), 0, 0, 0);
  114.  
  115.             AbsoluteLayout.LayoutParams layText1 = (AbsoluteLayout.LayoutParams) txtLRC1.getLayoutParams();
  116.             layText1.x = 5;
  117.             layText1.width = (int) Common.GetTextWidth(Sentence1, txtLRC1.getTextSize());
  118.             txtLRC1.setLayoutParams(layText1);
  119.             layText2.width = (int) FontWidth;
  120.             txtLRC2.setLayoutParams(layText2);
  121.  
  122.             if (anim != null && TimeGap != null)
  123.             {
  124.                 anim.setStartOffset((long) (TimeGap * 0.15));
  125.                 anim.setDuration((long) (TimeGap * 0.75));
  126.                 txtLRC2.startAnimation(anim);
  127.             }
  128.         }
  129.         else
  130.         {
  131.             Animation anim = null;
  132.             AbsoluteLayout.LayoutParams layText1 = (AbsoluteLayout.LayoutParams) txtLRC1.getLayoutParams();
  133.             AbsoluteLayout.LayoutParams layText2 = (AbsoluteLayout.LayoutParams) txtLRC2.getLayoutParams();
  134.             float FontWidth1 = Common.GetTextWidth(Sentence1, txtLRC1.getTextSize());
  135.             float FontWidth2 = Common.GetTextWidth(Sentence2, txtLRC2.getTextSize());
  136.  
  137.             layWM.width = dm.widthPixels;
  138.             if (FontWidth2 > dm.widthPixels - 80)
  139.                 layText2.x = 5;
  140.             else
  141.                 layText2.x = (int) (dm.widthPixels - 75 - FontWidth2);
  142.  
  143.             if (FontWidth1 > dm.widthPixels - 80)
  144.             {
  145.                 layText1.x = (int) (dm.widthPixels - 75 - FontWidth1);
  146.                 anim = new TranslateAnimation(FontWidth1 - (dm.widthPixels - 80), 0, 0, 0);
  147.             }
  148.             else
  149.                 layText1.x = 5;
  150.  
  151.             txtLRC1.setLayoutParams(layText1);
  152.             layText2.width = (int) Common.GetTextWidth(Sentence2, txtLRC1.getTextSize());
  153.             txtLRC2.setLayoutParams(layText2);
  154.  
  155.             if (anim != null && TimeGap != null)
  156.             {
  157.                 anim.setStartOffset((long) (TimeGap * 0.15));
  158.                 anim.setDuration((long) (TimeGap * 0.75));
  159.                 txtLRC1.startAnimation(anim);
  160.             }
  161.         }
  162.  
  163.         wm.updateViewLayout(main.getFl(), layWM);
  164.         imgIcon.setImageResource(IconResource);
  165.         txtLRC1.setText(Sentence1);
  166.         txtLRC1.setTextColor(Color1);
  167.         txtLRC2.setText(Sentence2);
  168.         txtLRC2.setTextColor(Color2);
  169.     }
  170.  
  171.     @Override
  172.     public boolean onTouchEvent(MotionEvent event)
  173.     {
  174.         x = event.getRawX();
  175.         y = event.getRawY() - 25;
  176.  
  177.         switch (event.getAction())
  178.         {
  179.             case MotionEvent.ACTION_DOWN:
  180.                 DownPosX = event.getX();
  181.                 DownPosY = event.getY();
  182.  
  183.                 break;
  184.             case MotionEvent.ACTION_MOVE:
  185.                 MoveView();
  186.                 break;
  187.  
  188.             case MotionEvent.ACTION_UP:
  189.                 MoveView();
  190.                 DownPosX = 0;
  191.                 DownPosY = 0;
  192.  
  193.                 // ??????????????????
  194.                 Editor edt = main.getSp().edit();
  195.                 edt.putInt("FloatLRCPos", layWM.y);
  196.                 main.getSt().setFloatLRCPos(layWM.y);
  197.                 edt.commit();
  198.  
  199.                 break;
  200.         }
  201.         return true;
  202.     }
  203.  
  204.     /* ???????????? */
  205.     private void MoveView()
  206.     {
  207.         layWM.x = (int) (x - DownPosX);
  208.         layWM.y = (int) (y - DownPosY);
  209.         wm.updateViewLayout(this, layWM);
  210.     }
  211.  
  212.     public float getDownPosX()
  213.     {
  214.         return DownPosX;
  215.     }
  216.  
  217.     public void setDownPosX(float downPosX)
  218.     {
  219.         DownPosX = downPosX;
  220.     }
  221.  
  222.     public float getDownPosY()
  223.     {
  224.         return DownPosY;
  225.     }
  226.  
  227.     public void setDownPosY(float downPosY)
  228.     {
  229.         DownPosY = downPosY;
  230.     }
  231.  
  232.     public float getX()
  233.     {
  234.         return x;
  235.     }
  236.  
  237.     public void setX(float x)
  238.     {
  239.         this.x = x;
  240.     }
  241.  
  242.     public float getY()
  243.     {
  244.         return y;
  245.     }
  246.  
  247.     public void setY(float y)
  248.     {
  249.         this.y = y;
  250.     }
  251.  
  252.     public WindowManager getWm()
  253.     {
  254.         return wm;
  255.     }
  256.  
  257.     public void setWm(WindowManager wm)
  258.     {
  259.         this.wm = wm;
  260.     }
  261.  
  262.     public WindowManager.LayoutParams getLayWM()
  263.     {
  264.         return layWM;
  265.     }
  266.  
  267.     public void setLayWM(WindowManager.LayoutParams layWM)
  268.     {
  269.         this.layWM = layWM;
  270.     }
  271.  
  272.     public scrMain getMain()
  273.     {
  274.         return main;
  275.     }
  276.  
  277.     public void setMain(scrMain main)
  278.     {
  279.         this.main = main;
  280.     }
  281.  
  282.     public ImageView getImgIcon()
  283.     {
  284.         return imgIcon;
  285.     }
  286.  
  287.     public void setImgIcon(ImageView imgIcon)
  288.     {
  289.         this.imgIcon = imgIcon;
  290.     }
  291.  
  292.     public AbsoluteLayout getLayMain()
  293.     {
  294.         return layMain;
  295.     }
  296.  
  297.     public void setLayMain(AbsoluteLayout layMain)
  298.     {
  299.         this.layMain = layMain;
  300.     }
  301.  
  302.     public TextView getTxtLRC1()
  303.     {
  304.         return txtLRC1;
  305.     }
  306.  
  307.     public void setTxtLRC1(TextView txtLRC1)
  308.     {
  309.         this.txtLRC1 = txtLRC1;
  310.     }
  311.  
  312.     public TextView getTxtLRC2()
  313.     {
  314.         return txtLRC2;
  315.     }
  316.  
  317.     public void setTxtLRC2(TextView txtLRC2)
  318.     {
  319.         this.txtLRC2 = txtLRC2;
  320.     }
  321. }
  1. /*
  2. * Copyright (C) 2011 The LiteListen Project
  3. *
  4. * Licensed under the Mozilla Public Licence, version 1.1 (the "License");
  5. * You may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.mozilla.org/MPL/MPL-1.1.html
  9. *
  10. * ???????????????? ??????? 2011
  11. * ???? Mozilla Public Licence 1.1 ??????????????B????
  12. * ????????????????????????????
  13. * ???????????????????????????
  14. *
  15. * http://www.mozilla.org/MPL/MPL-1.1.html
  16. */
  17.  
  18. package com.galapk.litelisten;
  19.  
  20. import java.util.List;
  21. import java.util.Map;
  22.  
  23. import android.graphics.Color;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.view.View.OnClickListener;
  28. import android.view.animation.Animation;
  29. import android.view.animation.LinearInterpolator;
  30. import android.view.animation.TranslateAnimation;
  31. import android.widget.BaseAdapter;
  32. import android.widget.ImageButton;
  33. import android.widget.ImageView;
  34. import android.widget.LinearLayout;
  35. import android.widget.RelativeLayout;
  36. import android.widget.TextView;
  37.  
  38. public class MusicAdapter extends BaseAdapter
  39. {
  40. private scrMain main;
  41. private List> lstSong; // ????
  42. public MusicAdapter(scrMain main, List> lstSong)
  43. {
  44. this.main = main;
  45. this.lstSong = lstSong;
  46. }
  47. public int getCount()
  48. {
  49. return lstSong.size();
  50. }
  51. public Object getItem(int arg0)
  52. {
  53. return arg0;
  54. }
  55. public long getItemId(int position)
  56. {
  57. return position;
  58. }
  59. public View getView(int position, View convertView, ViewGroup parent)
  60. {
  61. if (position < 0 || lstSong.size() <= 0 || position >= lstSong.size())
  62. return null;
  63. if (convertView == null)
  64. convertView = LayoutInflater.from(main).inflate(R.layout.list_music, null);
  65. RelativeLayout layMusicList = (RelativeLayout) convertView.findViewById(R.id.layMusicList);
  66. ImageView imgAlbum = (ImageView) convertView.findViewById(R.id.imgAlbum);
  67. TextView txtSongTitle = (TextView) convertView.findViewById(R.id.txtSongTitle);
  68. TextView txtSongInfo = (TextView) convertView.findViewById(R.id.txtSongInfo);
  69. ImageButton btnListPlay = (ImageButton) convertView.findViewById(R.id.btnListPlay);
  70. txtSongTitle.setText((String) lstSong.get(position).get("Title"));
  71. txtSongInfo.setText((String) lstSong.get(position).get("SongInfo"));
  72. if (main.getMs().getPlayerStatus() == MusicService.STATUS_PLAY && main.getMs().getCurrIndex() == position)
  73. {
  74. imgAlbum.setBackgroundResource(R.drawable.album_playing);
  75. txtSongTitle.setTextColor(Color.parseColor("#FF9900"));
  76. txtSongTitle.setTextSize(Float.parseFloat(main.getSt().getListFontSize()));
  77. if (main.getSt().getListFontShadow())
  78. txtSongTitle.setShadowLayer(0.5f, 0.5f, 1, Color.parseColor("#000000"));
  79. }
  80. else if (main.getMs().getPlayerStatus() == MusicService.STATUS_PAUSE && main.getMs().getCurrIndex() == position)
  81. {
  82. imgAlbum.setBackgroundResource(R.drawable.album_paused);
  83. txtSongTitle.setTextColor(Color.parseColor("#FF9900"));
  84. txtSongTitle.setTextSize(Float.parseFloat(main.getSt().getListFontSize()));
  85. if (main.getSt().getListFontShadow())
  86. txtSongTitle.setShadowLayer(0.5f, 0.5f, 1, Color.parseColor("#000000"));
  87. }
  88. else
  89. {
  90. imgAlbum.setBackgroundResource(R.drawable.album_normal);
  91. txtSongTitle.setTextColor(Color.parseColor(main.getSt().getListFontColor()));
  92. txtSongTitle.setTextSize(Float.parseFloat(main.getSt().getListFontSize()));
  93. if (main.getSt().getListFontShadow())
  94. txtSongTitle.setShadowLayer(0.5f, 0.5f, 1, Color.parseColor(main.getSt().getListFontShadowColor()));
  95. }
  96. if (main.getSelectedItemIndex() != position)
  97. {
  98. layMusicList.setBackgroundResource(R.drawable.bg_list_music_height);
  99. btnListPlay.setVisibility(View.GONE);
  100. txtSongTitle.clearAnimation();
  101. txtSongInfo.clearAnimation();
  102. }
  103. else
  104. {
  105. imgAlbum.setBackgroundResource(R.drawable.album_selected);
  106. btnListPlay.setVisibility(View.VISIBLE);
  107. btnListPlay.setFocusable(false);
  108. btnListPlay.setFocusableInTouchMode(false);
  109. // ??????????????????
  110. float CurrWidth = Common.GetTextWidth(txtSongTitle.getText().toString(), txtSongTitle.getTextSize());
  111. if (CurrWidth > main.getDm().widthPixels - 165)
  112. {
  113. LinearLayout.LayoutParams laySongTitle = (LinearLayout.LayoutParams) txtSongTitle.getLayoutParams();
  114. laySongTitle.width = (int) CurrWidth;
  115. txtSongTitle.setLayoutParams(laySongTitle);
  116. Animation anim = new TranslateAnimation(0, -(CurrWidth - main.getDm().widthPixels + 165), 0, 0);
  117. anim.setDuration((long) (CurrWidth * 15));
  118. anim.setStartOffset(2500);
  119. anim.setRepeatCount(100);
  120. anim.setInterpolator(new LinearInterpolator());
  121. anim.setRepeatMode(Animation.REVERSE);
  122. txtSongTitle.startAnimation(anim);
  123. }
  124. // ????????????????????
  125. CurrWidth = Common.GetTextWidth(txtSongInfo.getText().toString(), txtSongInfo.getTextSize());
  126. if (CurrWidth > main.getDm().widthPixels - 165)
  127. {
  128. LinearLayout.LayoutParams laySongInfo = (LinearLayout.LayoutParams) txtSongInfo.getLayoutParams();
  129. laySongInfo.width = (int) CurrWidth;
  130. txtSongInfo.setLayoutParams(laySongInfo);
  131. Animation anim = new TranslateAnimation(0, -(CurrWidth - main.getDm().widthPixels + 165), 0, 0);
  132. anim.setDuration((long) (CurrWidth * 15));
  133. anim.setStartOffset(2500);
  134. anim.setRepeatCount(100);
  135. anim.setInterpolator(new LinearInterpolator());
  136. anim.setRepeatMode(Animation.REVERSE);
  137. txtSongInfo.startAnimation(anim);
  138. }
  139. // ??????????
  140. btnListPlay.setOnClickListener(new OnClickListener()
  141. {
  142. public void onClick(View v)
  143. {
  144. main.getMs().Play(main.getSelectedItemIndex());
  145. }
  146. });
  147. if (main.getScreenOrantation() == 1 || main.getScreenOrantation() == 3)
  148. layMusicList.setBackgroundResource(R.drawable.bg_land_list_highlight);
  149. else
  150. layMusicList.setBackgroundResource(R.drawable.bg_port_list_highlight);
  151. }
  152. return convertView;
  153. }
  154. public scrMain getMain()
  155. {
  156. return main;
  157. }
  158. public void setMain(scrMain main)
  159. {
  160. this.main = main;
  161. }
  162. public List> getLstSong()
  163. {
  164. return lstSong;
  165. }
  166. public void setLstSong(List> lstSong)
  167. {
  168. this.lstSong = lstSong;
  169. }
  170. }
  1. package com.adserver.adview.samples.advanced;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.view.animation.AlphaAnimation;
  8. import android.view.animation.Animation;
  9. import android.view.animation.AnimationSet;
  10. import android.view.animation.LayoutAnimationController;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import com.adserver.adview.AdServerView;
  15. import com.adserver.adview.AdServerViewCore.OnAdDownload;
  16. import com.adserver.adview.samples.ApiDemos;
  17. import com.adserver.adview.samples.R;
  18. public class AnimAd extends Activity {
  19. /** Called when the activity is first created. */
  20. private Context context;
  21. private LinearLayout linearLayout;
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.animation_ad);
  25. context = this;
  26. linearLayout = (LinearLayout) findViewById(R.id.frameAdContent);
  27. final AdServerView adserverView1 = new AdServerView(this,8061,20249);
  28. adserverView1.setId(1);
  29. adserverView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ApiDemos.BANNER_HEIGHT));
  30. Animation animation = new TranslateAnimation(
  31. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  32. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, -1.0f
  33. );
  34. animation.setDuration(Integer.MAX_VALUE);
  35. animation.setFillAfter(true);
  36. adserverView1.startAnimation(animation);
  37. adserverView1.setOnAdDownload(new OnAdDownload() {
  38. @Override
  39. public void error(String arg0) {
  40. }
  41. @Override
  42. public void end() {
  43. Animation animation = new TranslateAnimation(
  44. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  45. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
  46. );
  47. animation.setDuration(2000);
  48. adserverView1.startAnimation(animation);
  49. }
  50. @Override
  51. public void begin() {
  52. }
  53. });
  54. linearLayout.addView(adserverView1);
  55. }
  56. }
  1. package com.MobileAnarchy.Android.Widgets.DockPanel;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.util.AttributeSet;
  5. import android.util.Log;
  6. import android.view.Gravity;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.animation.AccelerateInterpolator;
  10. import android.view.animation.Animation;
  11. import android.view.animation.DecelerateInterpolator;
  12. import android.view.animation.TranslateAnimation;
  13. import android.view.animation.Animation.AnimationListener;
  14. import android.widget.FrameLayout;
  15. import android.widget.ImageButton;
  16. import android.widget.LinearLayout;
  17. public class DockPanel extends LinearLayout {
  18. // =========================================
  19. // Private members
  20. // =========================================
  21. private static final String TAG = "DockPanel";
  22. private DockPosition position;
  23. private int contentLayoutId;
  24. private int handleButtonDrawableId;
  25. private Boolean isOpen;
  26. private Boolean animationRunning;
  27. private FrameLayout contentPlaceHolder;
  28. private ImageButton toggleButton;
  29. private int animationDuration;
  30. // =========================================
  31. // Constructors
  32. // =========================================
  33. public DockPanel(Context context, int contentLayoutId,
  34. int handleButtonDrawableId, Boolean isOpen) {
  35. super(context);
  36. this.contentLayoutId = contentLayoutId;
  37. this.handleButtonDrawableId = handleButtonDrawableId;
  38. this.isOpen = isOpen;
  39. Init(null);
  40. }
  41. public DockPanel(Context context, AttributeSet attrs) {
  42. super(context, attrs);
  43. // to prevent from crashing the designer
  44. try {
  45. Init(attrs);
  46. } catch (Exception ex) {
  47. }
  48. }
  49. // =========================================
  50. // Initialization
  51. // =========================================
  52. private void Init(AttributeSet attrs) {
  53. setDefaultValues(attrs);
  54. createHandleToggleButton();
  55. // create the handle container
  56. FrameLayout handleContainer = new FrameLayout(getContext());
  57. handleContainer.addView(toggleButton);
  58. // create and populate the panel's container, and inflate it
  59. contentPlaceHolder = new FrameLayout(getContext());
  60. String infService = Context.LAYOUT_INFLATER_SERVICE;
  61. LayoutInflater li = (LayoutInflater) getContext().getSystemService(
  62. infService);
  63. li.inflate(contentLayoutId, contentPlaceHolder, true);
  64. // setting the layout of the panel parameters according to the docking
  65. // position
  66. if (position == DockPosition.LEFT || position == DockPosition.RIGHT) {
  67. handleContainer.setLayoutParams(new LayoutParams(
  68. android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
  69. android.view.ViewGroup.LayoutParams.FILL_PARENT, 1));
  70. contentPlaceHolder.setLayoutParams(new LayoutParams(
  71. android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
  72. android.view.ViewGroup.LayoutParams.FILL_PARENT, 1));
  73. } else {
  74. handleContainer.setLayoutParams(new LayoutParams(
  75. android.view.ViewGroup.LayoutParams.FILL_PARENT,
  76. android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
  77. contentPlaceHolder.setLayoutParams(new LayoutParams(
  78. android.view.ViewGroup.LayoutParams.FILL_PARENT,
  79. android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
  80. }
  81. // adding the view to the parent layout according to docking position
  82. if (position == DockPosition.RIGHT || position == DockPosition.BOTTOM) {
  83. this.addView(handleContainer);
  84. this.addView(contentPlaceHolder);
  85. } else {
  86. this.addView(contentPlaceHolder);
  87. this.addView(handleContainer);
  88. }
  89. if (!isOpen) {
  90. contentPlaceHolder.setVisibility(GONE);
  91. }
  92. }
  93. private void setDefaultValues(AttributeSet attrs) {
  94. // set default values
  95. isOpen = true;
  96. animationRunning = false;
  97. animationDuration = 500;
  98. setPosition(DockPosition.RIGHT);
  99. // Try to load values set by xml markup
  100. if (attrs != null) {
  101. String namespace = "http://com.MobileAnarchy.Android.Widgets";
  102. animationDuration = attrs.getAttributeIntValue(namespace,
  103. "animationDuration", 500);
  104. contentLayoutId = attrs.getAttributeResourceValue(namespace,
  105. "contentLayoutId", 0);
  106. handleButtonDrawableId = attrs.getAttributeResourceValue(
  107. namespace, "handleButtonDrawableResourceId", 0);
  108. isOpen = attrs.getAttributeBooleanValue(namespace, "isOpen", true);
  109. // Enums are a bit trickier (needs to be parsed)
  110. try {
  111. position = DockPosition.valueOf(attrs.getAttributeValue(
  112. namespace, "dockPosition").toUpperCase());
  113. setPosition(position);
  114. } catch (Exception ex) {
  115. // Docking to the left is the default behavior
  116. setPosition(DockPosition.LEFT);
  117. }
  118. }
  119. }
  120. private void createHandleToggleButton() {
  121. toggleButton = new ImageButton(getContext());
  122. toggleButton.setPadding(0, 0, 0, 0);
  123. toggleButton.setLayoutParams(new FrameLayout.LayoutParams(
  124. android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
  125. android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
  126. Gravity.CENTER));
  127. toggleButton.setBackgroundColor(Color.TRANSPARENT);
  128. toggleButton.setImageResource(handleButtonDrawableId);
  129. toggleButton.setOnClickListener(new OnClickListener() {
  130. @Override
  131. public void onClick(View v) {
  132. toggle();
  133. }
  134. });
  135. }
  136. private void setPosition(DockPosition position) {
  137. this.position = position;
  138. switch (position) {
  139. case TOP:
  140. setOrientation(LinearLayout.VERTICAL);
  141. setGravity(Gravity.TOP);
  142. break;
  143. case RIGHT:
  144. setOrientation(LinearLayout.HORIZONTAL);
  145. setGravity(Gravity.RIGHT);
  146. break;
  147. case BOTTOM:
  148. setOrientation(LinearLayout.VERTICAL);
  149. setGravity(Gravity.BOTTOM);
  150. break;
  151. case LEFT:
  152. setOrientation(LinearLayout.HORIZONTAL);
  153. setGravity(Gravity.LEFT);
  154. break;
  155. }
  156. }
  157. // =========================================
  158. // Public methods
  159. // =========================================
  160. public int getAnimationDuration() {
  161. return animationDuration;
  162. }
  163. public void setAnimationDuration(int milliseconds) {
  164. animationDuration = milliseconds;
  165. }
  166. public Boolean getIsRunning() {
  167. return animationRunning;
  168. }
  169. public void open() {
  170. if (!animationRunning) {
  171. Log.d(TAG, "Opening...");
  172. Animation animation = createShowAnimation();
  173. this.setAnimation(animation);
  174. animation.start();
  175. isOpen = true;
  176. }
  177. }
  178. public void close() {
  179. if (!animationRunning) {
  180. Log.d(TAG, "Closing...");
  181. Animation animation = createHideAnimation();
  182. this.setAnimation(animation);
  183. animation.start();
  184. isOpen = false;
  185. }
  186. }
  187. public void toggle() {
  188. if (isOpen) {
  189. close();
  190. } else {
  191. open();
  192. }
  193. }
  194. // =========================================
  195. // Private methods
  196. // =========================================
  197. private Animation createHideAnimation() {
  198. Animation animation = null;
  199. switch (position) {
  200. case TOP:
  201. animation = new TranslateAnimation(0, 0, 0, -contentPlaceHolder
  202. .getHeight());
  203. break;
  204. case RIGHT:
  205. animation = new TranslateAnimation(0, contentPlaceHolder
  206. .getWidth(), 0, 0);
  207. break;
  208. case BOTTOM:
  209. animation = new TranslateAnimation(0, 0, 0, contentPlaceHolder
  210. .getHeight());
  211. break;
  212. case LEFT:
  213. animation = new TranslateAnimation(0, -contentPlaceHolder
  214. .getWidth(), 0, 0);
  215. break;
  216. }
  217. animation.setDuration(animationDuration);
  218. animation.setInterpolator(new AccelerateInterpolator());
  219. animation.setAnimationListener(new AnimationListener() {
  220. @Override
  221. public void onAnimationStart(Animation animation) {
  222. animationRunning = true;
  223. }
  224. @Override
  225. public void onAnimationRepeat(Animation animation) {
  226. }
  227. @Override
  228. public void onAnimationEnd(Animation animation) {
  229. contentPlaceHolder.setVisibility(View.GONE);
  230. animationRunning = false;
  231. }
  232. });
  233. return animation;
  234. }
  235. private Animation createShowAnimation() {
  236. Animation animation = null;
  237. switch (position) {
  238. case TOP:
  239. animation = new TranslateAnimation(0, 0, -contentPlaceHolder
  240. .getHeight(), 0);
  241. break;
  242. case RIGHT:
  243. animation = new TranslateAnimation(contentPlaceHolder.getWidth(),
  244. 0, 0, 0);
  245. break;
  246. case BOTTOM:
  247. animation = new TranslateAnimation(0, 0, contentPlaceHolder
  248. .getHeight(), 0);
  249. break;
  250. case LEFT:
  251. animation = new TranslateAnimation(-contentPlaceHolder.getWidth(),
  252. 0, 0, 0);
  253. break;
  254. }
  255. Log.d(TAG, "Animation duration: " + animationDuration);
  256. animation.setDuration(animationDuration);
  257. animation.setInterpolator(new DecelerateInterpolator());
  258. animation.setAnimationListener(new AnimationListener() {
  259. @Override
  260. public void onAnimationStart(Animation animation) {
  261. animationRunning = true;
  262. contentPlaceHolder.setVisibility(View.VISIBLE);
  263. Log.d(TAG, "\"Show\" Animation started");
  264. }
  265. @Override
  266. public void onAnimationRepeat(Animation animation) {
  267. }
  268. @Override
  269. public void onAnimationEnd(Animation animation) {
  270. animationRunning = false;
  271. Log.d(TAG, "\"Show\" Animation ended");
  272. }
  273. });
  274. return animation;
  275. }
  276. }
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * the source is come from andorid - 2.1 calculator2
  18. */
  19. package com.bottleworks.dailymoney.calculator2;
  20. import android.content.Context;
  21. import android.text.Editable;
  22. import android.text.Spanned;
  23. import android.text.method.NumberKeyListener;
  24. import android.util.AttributeSet;
  25. import android.view.animation.TranslateAnimation;
  26. import android.text.InputType;
  27. import android.widget.EditText;
  28. import android.widget.TextView;
  29. import android.widget.ViewSwitcher;
  30. import android.graphics.Rect;
  31. import android.graphics.Paint;
  32. /**
  33. * Provides vertical scrolling for the input/result EditText.
  34. */
  35. class CalculatorDisplay extends ViewSwitcher {
  36. // only these chars are accepted from keyboard
  37. private static final char[] ACCEPTED_CHARS =
  38. "0123456789.+-*/\u2212\u00d7\u00f7()!%^".toCharArray();
  39. private static final int ANIM_DURATION = 500;
  40. enum Scroll { UP, DOWN, NONE }
  41. TranslateAnimation inAnimUp;
  42. TranslateAnimation outAnimUp;
  43. TranslateAnimation inAnimDown;
  44. TranslateAnimation outAnimDown;
  45. private Logic mLogic;
  46. private boolean mComputedLineLength = false;
  47. public CalculatorDisplay(Context context, AttributeSet attrs) {
  48. super(context, attrs);
  49. }
  50. @Override
  51. protected void onFinishInflate() {
  52. super.onFinishInflate();
  53. Calculator calc = (Calculator) getContext();
  54. calc.adjustFontSize((TextView)getChildAt(0));
  55. calc.adjustFontSize((TextView)getChildAt(1));
  56. }
  57. @Override
  58. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  59. super.onLayout(changed, left, top, right, bottom);
  60. if (!mComputedLineLength) {
  61. mLogic.setLineLength(getNumberFittingDigits((TextView) getCurrentView()));
  62. mComputedLineLength = true;
  63. }
  64. }
  65. // compute the maximum number of digits that fit in the
  66. // calculator display without scrolling.
  67. private int getNumberFittingDigits(TextView display) {
  68. int available = display.getWidth()
  69. - display.getTotalPaddingLeft() - display.getTotalPaddingRight();
  70. Paint paint = display.getPaint();
  71. float digitWidth = paint.measureText("2222222222") / 10f;
  72. return (int) (available / digitWidth);
  73. }
  74. protected void setLogic(Logic logic) {
  75. mLogic = logic;
  76. NumberKeyListener calculatorKeyListener =
  77. new NumberKeyListener() {
  78. public int getInputType() {
  79. // Don't display soft keyboard.
  80. return InputType.TYPE_NULL;
  81. }
  82. protected char[] getAcceptedChars() {
  83. return ACCEPTED_CHARS;
  84. }
  85. public CharSequence filter(CharSequence source, int start, int end,
  86. Spanned dest, int dstart, int dend) {
  87. /* the EditText should still accept letters (eg. 'sin')
  88. coming from the on-screen touch buttons, so don't filter anything.
  89. */
  90. return null;
  91. }
  92. };
  93. Editable.Factory factory = new CalculatorEditable.Factory(logic);
  94. for (int i = 0; i < 2; ++i) {
  95. EditText text = (EditText) getChildAt(i);
  96. text.setBackgroundDrawable(null);
  97. text.setEditableFactory(factory);
  98. text.setKeyListener(calculatorKeyListener);
  99. }
  100. }
  101. @Override
  102. public void setOnKeyListener(OnKeyListener l) {
  103. getChildAt(0).setOnKeyListener(l);
  104. getChildAt(1).setOnKeyListener(l);
  105. }
  106. @Override
  107. protected void onSizeChanged(int w, int h, int oldW, int oldH) {
  108. inAnimUp = new TranslateAnimation(0, 0, h, 0);
  109. inAnimUp.setDuration(ANIM_DURATION);
  110. outAnimUp = new TranslateAnimation(0, 0, 0, -h);
  111. outAnimUp.setDuration(ANIM_DURATION);
  112. inAnimDown = new TranslateAnimation(0, 0, -h, 0);
  113. inAnimDown.setDuration(ANIM_DURATION);
  114. outAnimDown = new TranslateAnimation(0, 0, 0, h);
  115. outAnimDown.setDuration(ANIM_DURATION);
  116. }
  117. void insert(String delta) {
  118. EditText editor = (EditText) getCurrentView();
  119. int cursor = editor.getSelectionStart();
  120. editor.getText().insert(cursor, delta);
  121. }
  122. EditText getEditText() {
  123. return (EditText) getCurrentView();
  124. }
  125. Editable getText() {
  126. EditText text = (EditText) getCurrentView();
  127. return text.getText();
  128. }
  129. void setText(CharSequence text, Scroll dir) {
  130. if (getText().length() == 0) {
  131. dir = Scroll.NONE;
  132. }
  133. if (dir == Scroll.UP) {
  134. setInAnimation(inAnimUp);
  135. setOutAnimation(outAnimUp);
  136. } else if (dir == Scroll.DOWN) {
  137. setInAnimation(inAnimDown);
  138. setOutAnimation(outAnimDown);
  139. } else { // Scroll.NONE
  140. setInAnimation(null);
  141. setOutAnimation(null);
  142. }
  143. EditText editText = (EditText) getNextView();
  144. editText.setText(text);
  145. //Calculator.log("selection to " + text.length() + "; " + text);
  146. editText.setSelection(text.length());
  147. showNext();
  148. }
  149. void setSelection(int i) {
  150. EditText text = (EditText) getCurrentView();
  151. text.setSelection(i);
  152. }
  153. int getSelectionStart() {
  154. EditText text = (EditText) getCurrentView();
  155. return text.getSelectionStart();
  156. }
  157. @Override
  158. protected void onFocusChanged(boolean gain, int direction, Rect prev) {
  159. //Calculator.log("focus " + gain + "; " + direction + "; " + prev);
  160. if (!gain) {
  161. requestFocus();
  162. }
  163. }
  164. }
  1. package com.adserver.adview.samples.advanced;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.view.animation.AlphaAnimation;
  8. import android.view.animation.Animation;
  9. import android.view.animation.AnimationSet;
  10. import android.view.animation.AnimationUtils;
  11. import android.view.animation.LayoutAnimationController;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.Button;
  14. import android.widget.LinearLayout;
  15. import com.adserver.adview.AdServerView;
  16. import com.adserver.adview.AdServerViewCore.OnAdDownload;
  17. import com.adserver.adview.samples.ApiDemos;
  18. import com.adserver.adview.samples.R;
  19. public class AnimAdXML extends Activity {
  20. /** Called when the activity is first created. */
  21. private Context context;
  22. private LinearLayout linearLayout;
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.animation_ad_xml);
  26. context = this;
  27. final AdServerView adserverView1 = (AdServerView)findViewById(R.id.adServerView);
  28. final Animation animation =
  29. AnimationUtils.loadAnimation(getApplicationContext(),
  30. R.animator.ad_in_out);
  31. Animation animation2 = new TranslateAnimation(
  32. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  33. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, -1.0f
  34. );
  35. animation2.setDuration(Integer.MAX_VALUE);
  36. animation2.setFillAfter(true);
  37. adserverView1.startAnimation(animation2);
  38. adserverView1.setOnAdDownload(new OnAdDownload() {
  39. @Override
  40. public void error(String arg0) {
  41. }
  42. @Override
  43. public void end() {
  44. adserverView1.startAnimation(animation);
  45. }
  46. @Override
  47. public void begin() {
  48. }
  49. });
  50. /*linearLayout = (LinearLayout) findViewById(R.id.frameAdContent);
  51. final AdServerView adserverView1 = new AdServerView(this,8061,20249);
  52. adserverView1.setId(1);
  53. adserverView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ApiDemos.BANNER_HEIGHT));
  54. Animation animation = new TranslateAnimation(
  55. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  56. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, -1.0f
  57. );
  58. animation.setDuration(Integer.MAX_VALUE);
  59. animation.setFillAfter(true);
  60. adserverView1.startAnimation(animation);
  61. adserverView1.setOnAdDownload(new OnAdDownload() {
  62. @Override
  63. public void error(String arg0) {
  64. }
  65. @Override
  66. public void end() {
  67. Animation animation = new TranslateAnimation(
  68. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  69. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
  70. );
  71. animation.setDuration(2000);
  72. adserverView1.startAnimation(animation);
  73. }
  74. @Override
  75. public void begin() {
  76. }
  77. });
  78. linearLayout.addView(adserverView1);
  79. /* final AdServerView adserverView2 = new AdServerView(this,8061,20249);
  80. adserverView2.setId(1);
  81. adserverView2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ApiDemos.BANNER_HEIGHT));
  82. Animation animation2 = new AlphaAnimation(0f, 0f);
  83. animation2.setDuration(Integer.MAX_VALUE);
  84. animation2.setFillAfter(true);
  85. adserverView2.startAnimation(animation2);
  86. adserverView2.setOnAdDownload(new OnAdDownload() {
  87. @Override
  88. public void error(String arg0) {
  89. }
  90. @Override
  91. public void end() {
  92. Animation animation = new AlphaAnimation(0.0f, 0.5f);
  93. animation.setDuration(2000);
  94. adserverView2.startAnimation(animation);
  95. }
  96. @Override
  97. public void begin() {
  98. }
  99. });
  100. linearLayout.addView(adserverView2);*/
  101. }
  102. }
  1. package ma.android.fts;
  2. import java.util.Random;
  3. import android.content.Context;
  4. import android.graphics.Point;
  5. import android.view.animation.AlphaAnimation;
  6. import android.view.animation.Animation;
  7. import android.view.animation.AnimationSet;
  8. import android.view.animation.RotateAnimation;
  9. import android.view.animation.ScaleAnimation;
  10. import android.view.animation.TranslateAnimation;
  11. import android.widget.ImageView;
  12. public class AnimationFactory {
  13. final static long ANIMATION_DURATION = 4000;
  14. private static final int ANIMATION_IMAGES = 25;
  15. private static int[] animationImages = new int[ANIMATION_IMAGES];
  16. static {
  17. for (int a=0; a
  1. import sw6.visualschedule.extendedViews.LinkedActivityView;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import android.view.animation.Animation;
  8. import android.view.animation.Animation.AnimationListener;
  9. import android.view.animation.TranslateAnimation;
  10. import android.widget.ImageView;
  11. import java.util.Date;
  12. /**
  13. *
  14. * @author kfuglsang
  15. *
  16. */
  17. public class ActivityImageViewTouchListener implements OnTouchListener {
  18. private final AnimationListener aniListener = new AnimationListener() {
  19. @Override
  20. public void onAnimationEnd(final Animation animation) {
  21. isAnimating = false;
  22. mDraggableImage.setVisibility(View.INVISIBLE);
  23. mActivityView.setVisibility(View.VISIBLE);
  24. if (mActivityView.getScheduleActivity().getInstanceDate().before(new Date())) {
  25. mActivityView.startAnimation();
  26. }
  27. }
  28. @Override
  29. public void onAnimationRepeat(final Animation animation) {
  30. }
  31. @Override
  32. public void onAnimationStart(final Animation animation) {
  33. isAnimating = true;
  34. }
  35. };
  36. private Boolean isAnimating = false;
  37. private transient boolean isOnTopOfTargetView = false;
  38. private transient StartActivityListener mActivityListener;
  39. private final transient LinkedActivityView mActivityView;
  40. private final transient ImageView mDraggableImage;
  41. private transient Rect mHitRect;
  42. /**
  43. * Initializes the ActivityImageViewTouchListener.
  44. *
  45. * WARNING: High coupling with ActivityList. Should not be used by any other classes than that
  46. *
  47. * @param context the context in which the listener should run.
  48. * @param activityView the LinkedActivityView to listen for events on.
  49. * @param outerContainerView the draggable image.
  50. */
  51. public ActivityImageViewTouchListener(Context context, LinkedActivityView activityView, ImageView drag) {
  52. mActivityView = activityView;
  53. mDraggableImage = drag;
  54. }
  55. /**
  56. * Handles on touch events on a view.
  57. *
  58. * @param view the view being touched
  59. * @param event the event that has occurred.
  60. *
  61. * @return true if the event is handled by the method.
  62. */
  63. @Override
  64. public boolean onTouch(final View view, final MotionEvent event) {
  65. Boolean droppedOnActiveZone;
  66. if (isAnimating)
  67. return false;
  68. switch (event.getActionMasked())
  69. {
  70. case MotionEvent.ACTION_MOVE:
  71. //Move the image to the position underneith user's finger, by modifying padding.
  72. //TODO: To avoid unnesceary image manipulation, extend ImageView to hold these values.
  73. mDraggableImage.setPadding((int) event.getRawX()
  74. - (mDraggableImage.getDrawable().getMinimumWidth() / 2),
  75. (int) event.getRawY()
  76. - (int) (mDraggableImage.getDrawable().getMinimumHeight() / 2),
  77. 0, 0);
  78. //Is the dragging image on top of the target view?
  79. droppedOnActiveZone = mHitRect.contains((int) event.getRawX(),(int) event.getRawY());
  80. //Avoid constantly setting the drawable by comparing the found boolean to a boolean value
  81. //that is changed every time droppedOnActiveZone changes.
  82. if (droppedOnActiveZone != isOnTopOfTargetView) {
  83. isOnTopOfTargetView = droppedOnActiveZone;
  84. if (droppedOnActiveZone)
  85. mActivityListener.draggableImageOverTarget();
  86. else
  87. mActivityListener.draggableImageAwayFromTarget();
  88. }
  89. break;
  90. case MotionEvent.ACTION_UP:
  91. mActivityListener.up();
  92. //Is the dragging view dropped on the drop zone (target view)
  93. droppedOnActiveZone = mHitRect.contains((int) event.getRawX(), (int) event.getRawY());
  94. if (droppedOnActiveZone) {
  95. //User has dragged activity to dropzone. Check if the step view or countdown view should be shown.
  96. //TODO : mCurrentActivity global?
  97. mActivityListener.draggableImageDroppedOnTarget();
  98. } else {
  99. //Hide target view (dropzone)
  100. mActivityView.getLinkedImageView().setVisibility(View.INVISIBLE);
  101. //Create animation that moves activity back to initial position.
  102. final int paddingLeft = mDraggableImage.getPaddingLeft();
  103. final int paddingTop = mDraggableImage.getPaddingTop();
  104. final Rect rectangle = new Rect();
  105. view.getHitRect(rectangle);
  106. final int[] pos = new int[2];
  107. mActivityView.getLocationOnScreen(pos);
  108. final int toX = pos[0];
  109. final int toY = pos[1];
  110. mDraggableImage.setPadding(0, 0, 0, 0);
  111. final TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE, paddingLeft, Animation.ABSOLUTE, toX , Animation.ABSOLUTE, paddingTop, Animation.ABSOLUTE, toY);
  112. anim.setZAdjustment(Animation.ZORDER_TOP);
  113. anim.setDuration(500);
  114. anim.setAnimationListener(aniListener);
  115. mDraggableImage.setPadding(0, 0, 0, 0);
  116. mDraggableImage.startAnimation(anim);
  117. }
  118. break;
  119. case MotionEvent.ACTION_DOWN:
  120. final int[] location = new int[2];
  121. mActivityView.getLinkedImageView().getLocationOnScreen(location);
  122. mHitRect = new Rect(location[0], location[1], location[0] + mActivityView.getLinkedImageView().getWidth(), location[1] + mActivityView.getHeight());
  123. mDraggableImage.setPadding((int) event.getRawX()
  124. - (mDraggableImage.getDrawable().getMinimumWidth() / 2),
  125. (int) event.getRawY()
  126. - (int) (mDraggableImage.getDrawable().getMinimumHeight() / 2),
  127. 0, 0);
  128. mActivityView.getLinkedImageView().setVisibility(View.VISIBLE);
  129. mActivityListener.down();
  130. break;
  131. default:
  132. break;
  133. }
  134. return true;
  135. }
  136. public void setActivityListener(StartActivityListener activityListener) {
  137. mActivityListener = activityListener;
  138. }
  139. }
  1. package jp.android_group.payforward.ui;
  2. import android.view.animation.TranslateAnimation;
  3. import android.view.MotionEvent;
  4. import android.view.View;
  5. import android.view.GestureDetector;
  6. import android.widget.FrameLayout;
  7. import android.content.Context;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. class PanelSwitcher extends FrameLayout {
  11. private static final String TAG = "PanelSwitcher";
  12. private static final int MAJOR_MOVE = 80;
  13. private static final int ANIM_DURATION = 300;
  14. private GestureDetector mGestureDetector;
  15. private int mCurrentView;
  16. private View mChildren[];
  17. private int mWidth;
  18. private TranslateAnimation inLeft;
  19. private TranslateAnimation outLeft;
  20. private TranslateAnimation inRight;
  21. private TranslateAnimation outRight;
  22. private ActiveViewChangeListener listener = null;
  23. public static interface ActiveViewChangeListener {
  24. public void onActiveViewChanged(int newId);
  25. }
  26. public void setActiveViewChangeListener(ActiveViewChangeListener l) {
  27. listener = l;
  28. }
  29. public PanelSwitcher(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. mCurrentView = 0;
  32. mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
  33. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  34. float velocityY) {
  35. int dx = (int) (e2.getX() - e1.getX());
  36. // don't accept the fling if it's too short
  37. // as it may conflict with a button push
  38. if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
  39. if (velocityX > 0) {
  40. moveRight();
  41. } else {
  42. moveLeft();
  43. }
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. });
  50. }
  51. public void setCurrentIndex(int current) {
  52. mCurrentView = current;
  53. updateCurrentView();
  54. }
  55. private void updateCurrentView() {
  56. if (mChildren == null) {
  57. initChildlen();
  58. }
  59. for (int i = mChildren.length-1; i >= 0 ; --i) {
  60. mChildren[i].setVisibility(i==mCurrentView ? View.VISIBLE : View.GONE);
  61. }
  62. }
  63. @Override
  64. public void onSizeChanged(int w, int h, int oldW, int oldH) {
  65. mWidth = w;
  66. inLeft = new TranslateAnimation(mWidth, 0, 0, 0);
  67. outLeft = new TranslateAnimation(0, -mWidth, 0, 0);
  68. inRight = new TranslateAnimation(-mWidth, 0, 0, 0);
  69. outRight = new TranslateAnimation(0, mWidth, 0, 0);
  70. inLeft.setDuration(ANIM_DURATION);
  71. outLeft.setDuration(ANIM_DURATION);
  72. inRight.setDuration(ANIM_DURATION);
  73. outRight.setDuration(ANIM_DURATION);
  74. }
  75. protected void initChildlen() {
  76. int count = getChildCount();
  77. mChildren = new View[count];
  78. for (int i = 0; i < count; ++i) {
  79. mChildren[i] = getChildAt(i);
  80. }
  81. }
  82. protected void onFinishInflate() {
  83. initChildlen();
  84. updateCurrentView();
  85. }
  86. @Override
  87. public boolean onTouchEvent(MotionEvent ev) {
  88. Log.d(TAG, "onTouch:" + ev);
  89. final float xf = ev.getX();
  90. final float yf = ev.getY();
  91. TouchPassThroughListView list = (TouchPassThroughListView)getCurrentView();
  92. if (list.isEventHandled()) {
  93. final float scrolledXFloat = xf + getScrollX();
  94. final float scrolledYFloat = yf + getScrollY();
  95. final float xc = scrolledXFloat - (float) list.getLeft();
  96. final float yc = scrolledYFloat - (float) list.getTop();
  97. ev.setLocation(xc, yc);
  98. list.dispatchTouchEvent(ev);
  99. }
  100. ev.setLocation(xf, yf);
  101. boolean ret = (list != null ? list.isEventHandled() : false);
  102. ret |= mGestureDetector.onTouchEvent(ev);
  103. Log.d(TAG, "onTouch ret:" + ret);
  104. return ret;
  105. }
  106. @Override
  107. public boolean onInterceptTouchEvent(MotionEvent event) {
  108. return mGestureDetector.onTouchEvent(event);
  109. }
  110. void moveLeft() {
  111. // <-- data-blogger-escaped---="" data-blogger-escaped--="" data-blogger-escaped-1="" data-blogger-escaped-iew.gone="" data-blogger-escaped-iew.visible="" data-blogger-escaped-if="" data-blogger-escaped-inleft="" data-blogger-escaped-listener.onactiveviewchanged="" data-blogger-escaped-mchildren.length="" data-blogger-escaped-mchildren="" data-blogger-escaped-mcurrentview="" data-blogger-escaped-moveright="" data-blogger-escaped-outleft="" data-blogger-escaped-setvisibility="" data-blogger-escaped-startanimation="" data-blogger-escaped-void="">
  112. if (mCurrentView > 0) {
  113. mChildren[mCurrentView-1].setVisibility(View.VISIBLE);
  114. mChildren[mCurrentView-1].startAnimation(inRight);
  115. mChildren[mCurrentView].startAnimation(outRight);
  116. mChildren[mCurrentView].setVisibility(View.GONE);
  117. mCurrentView--;
  118. listener.onActiveViewChanged(mCurrentView);
  119. }
  120. }
  121. public int getCurrentIndex() {
  122. return mCurrentView;
  123. }
  124. public View getCurrentView() {
  125. return mChildren[mCurrentView];
  126. }
  127. }
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.android.apis.view;
  17. import android.app.ListActivity;
  18. import android.os.Bundle;
  19. import android.view.animation.AlphaAnimation;
  20. import android.view.animation.Animation;
  21. import android.view.animation.AnimationSet;
  22. import android.view.animation.LayoutAnimationController;
  23. import android.view.animation.TranslateAnimation;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.ListView;
  26. public class LayoutAnimation2 extends ListActivity {
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setListAdapter(new ArrayAdapter(this,
  31. android.R.layout.simple_list_item_1, mStrings));
  32. AnimationSet set = new AnimationSet(true);
  33. Animation animation = new AlphaAnimation(0.0f, 1.0f);
  34. animation.setDuration(50);
  35. set.addAnimation(animation);
  36. animation = new TranslateAnimation(
  37. Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
  38. Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
  39. );
  40. animation.setDuration(100);
  41. set.addAnimation(animation);
  42. LayoutAnimationController controller =
  43. new LayoutAnimationController(set, 0.5f);
  44. ListView listView = getListView();
  45. listView.setLayoutAnimation(controller);
  46. }
  47. private String[] mStrings = {
  48. "Bordeaux",
  49. "Lyon",
  50. "Marseille",
  51. "Nancy",
  52. "Paris",
  53. "Toulouse",
  54. "Strasbourg"
  55. };
  56. }

No comments:

Post a Comment