【067】◀▶ Android (III) - 其他和动画

●·● 目录:

package: android.others*

A1 ………… Color 类
A2 ………… Log 类
A3 ………… KeyEvent 类
A4 ………… MotionEvent 类
A5 ………… Uri 类
A6 ………… CheckBox 类
A7 ………… ProgressBar 类
A8 ………… ListView 类

package: android.view.animation

D1 ………… Animation 类
D2 ………… AnimationSet 类
D3 ………… AlphaAnimation 类
D4 ………… RotateAnimation 类
D5 ………… ScaleAnimation 类
D6 ………… TranslateAnimation 类
D7 ………… ProgressBar 类
D8 ………… ListView 类

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Color 类

1. 定义了默认的颜色及一些方法.

java.lang.Object
   ↳     java.lang.Boolean

2. Color Methods:

  • HSVToColor(float[] hsv):由 HSV 转换为 ARGB.
  • HSVToColor(int alpha, float[] hsv):
  • RGBToHSV(int red, int green, int blue, float[] hsv):由 RGB 转换为 HSV.
  • colorToHSV(int color, float[] hsv):

  • alpha(int color):返回一个颜色的 A 值.
  • red(int color):返回一个颜色的 R 值.
  • green(int color):返回一个颜色的 G 值.
  • blue(int color):返回一个颜色的 B 值.

  • argb(int alpha, int red, int green, int blue):通过 A R G B 构造颜色.
  • rgb(int red, int green, int blue):通过 R G B 构造颜色.
  • parseColor(String colorString):通过颜色名称来解析, 大小写无碍.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A2个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Log 类

1. LogCat 中显示的日志, 可以自定义输出.

java.lang.Object
   ↳     android.util.Log

2. Log Methods:

  • i(String tag, String msg):INFO.
  • e(String tag, String msg):ERROR.
  • d(String tag, String msg):DEBUG.
  • v(String tag, String msg):VERBOSE(详细的).
  • w(String tag, Throwable tr):WARN.

※ tag 一般设置为一个常量, 一般为 Activity 的名称.

private static final String TAG = "MyActivity";

将想要传递的内容写在 msg 的参数中, 然后在 LogCat 中通过提取 tag, 并过滤 "MyActivity" 来查看.

 

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A3个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● KeyEvent 类

1. Object used to report key and button events.

java.lang.Object
   ↳     android.view.InputEvent
         ↳     android.view.KeyEvent

2. KeyEvent Constants:

  • ACTION_DOWN
  • KEYCODE_BACK:返回键.
  • KEYCODE_HOME:Home 键.
  • KEYCODE_MENU:菜单键.

3. KeyEvent Methods:

  • getAction():

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A4个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● MotionEvent 类

1. Object used to report movement (mouse, pen, finger, trackball) events.

java.lang.Object
   ↳     android.view.InputEvent
         ↳     android.view.MotionEvent

2. MotionEvent Methods:

  • getX():返回值:final float. X 坐标值.
  • getY():返回值:final float. Y 坐标值.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A5个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Uri 类

1. Immutable URI reference.

java.lang.Object
   ↳     android.net.Uri

2. MotionEvent Methods:

  • parse(String uriString):返回值:static Uri. 通过给定的字符串解析创建一个 Uri.
  • toString():转会字符串.

※ 参考:android之Uri的常用几个例子

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A6个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

 

●·● WindowManager 接口

 

1. The interface that apps use to talk to the window manager.

android.view.WindowManager

 

WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();

 

2. WindowManager Methods:

  • getDefaultDisplay():返回值:abstract Display. 使用此方法获取默认 Display 对象.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A7个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Display 类

1. Provides information about the display size and density.

java.lang.Object
   ↳    android.view.Display

2. Display Methods:

  • getHeight():获取对象的高度. (deprecated)
  • getWidth():获取对象的宽度. (deprecated)
  • getSize (Point outSize):先新建一个 Point 对象 outSize, 然后执行此方法, 就会将对象的高度和宽度存到 Point 对象内.
    Point point = new Point();
    Display d = wm.getDefaultDisplay();
    d.getSize(point);
    Log.d("Orientation", "getSize - Width" + point.x);  //宽度
    Log.d("Orientation", "getSize - Height" + point.y);  //高度
  • getRotation():获取对象的方向. 0 为 Portrait, 1 为 Landscape.
  • getDisplayId():返回对象的索引.

 


 


 


---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Animation 类

1. 动画抽象类.

2. Animation Methods:

  • setDuration(long):设置动画的持续时间.  → android:duration
  • setStartOffset(long):设置开始动画的滞留时间.  android:startOffset
  • setFillBefore(boolean):若为 true, 动画停留在起始位置.  android:fillBefore
  • setFillAfter(boolean):若为 true, 动画停留在结束的位置.  android:fillAfter
  • setStartTime(long):动画开始的时间.  
  • setInterpolator(Interpolator):动画运行模式.  android:interpolator
  • setRepeatCount(int):设置动画重复的次数.(infinite)   android:repeatCount
  • setRepeatMode(int):设置动画重复的模式.(restart, reverse)   android:repeatMode

3. Animation Constants:

  • INFINITE:动画无限重复.
  • RESTART:重新开始动画.  
  • REVERSE:相反移动动画.
  • ABSOLUTE:绝对坐标值.
  • RELATIVE_TO_SELF:相对于自身比例的坐标值.
  • RELATIVE_TO_PARENT:相对于父控件比例的坐标值.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D2个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● AnimationSet 类

1. 动画集合.

2. AnimationSet Methods:

  • addAnimation(Animation a):增加一个子动画.

【067】◀▶ Android (III) - 其他和动画_第1张图片

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D3个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● AlphaAnimation 类

1. 透明渐变的动画.

  构造函数:AlphaAnimation(float fromAlpha, float toAlpha):1f 为不透明, 0f 为完全透明.

2. AlphaAnimation Methods:

  • setDuration(long durationMillis):设置动画的持续时间.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D4个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● RotateAnimation 类

1. 旋转的动画.

2. RotateAnimation Constructors:

  • RotateAnimation(float fromDegrees, float toDegrees):角度指:当前半径所在直线与起始半径所在直线的角度.
  • RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY):
  • RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue):第三个和第五个参数表示坐标类型, 第四个和第六个参数表示坐标值, 分别为 x 和 y.

3. RotateAnimation Methods:

  • setDuration(long durationMillis):设置动画的持续时间.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D5个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● ScaleAnimation 类

1. 缩放的动画.

2. ScaleAnimation Constructs:

  • ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue):原始 X 的长度到动画后 X 的长度, 原始 Y 的长度到动画后 Y 的长度. 参考坐标类型.

3. ScaleAnimation Methods:

  • setDuration(long durationMillis):设置动画的持续时间.

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第D6个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● TranslateAnimation 类

1. 移动的动画.

2. TranslateAnimation Constructs:

  • TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue):

3. TranslateAnimation Methods:

  • setDuration(long durationMillis):设置动画的持续时间.

※ 参考:Android Animation学习笔记

举例:直接建立动画

View Code
 1 public class MainActivity extends Activity {
 2     private ImageView imageView = null;
 3     private Button rotateButton = null;
 4     
 5     public void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.main);
 8         imageView = (ImageView) findViewById(R.id.imageViewId);
 9 
10         rotateButton = (Button) findViewById(R.id.rotateButtonId);
11         rotateButton.setOnClickListener(new RotateButtonListener());
12     }
13 
14     private class RotateButtonListener implements OnClickListener {
15         public void onClick(View view) {
16             AnimationSet animationSet = new AnimationSet(true);
17             RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
18                     Animation.RELATIVE_TO_PARENT, 0.5f,
19                     Animation.RELATIVE_TO_SELF, 0.5f);
20             rotateAnimation.setDuration(5000);
21             animationSet.addAnimation(rotateAnimation);
22             imageView.startAnimation(animationSet);
23         }
24     }
25 }

 

 

 

举例:通过 *.xml 文件批量建立动画

  • pivot 设置为 50:Absolute.
  • pivot 设置为 50%:relative to self.
  • pivot 设置为 50%p:relative to parent.

res/anim/alpha.xml

  • android:interpolator:定义动画模式, 一直加速, 匀速...等等.
  • android:shareinterpolator:是否共享此模式, true 为共享.
     View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3     android:interpolator="@android:anim/accelerate_interpolator">
4     <alpha 
5         android:fromAlpha="1.0"
6         android:toAlpha="0.0"
7         android:startOffset="500"
8         android:duration="500" />
9 </set>

 

res/anim/rotate.xml

     View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3     android:interpolator="@android:anim/accelerate_interpolator">
4     <rotate android:fromDegrees="0"
5         android:toDegrees="360"
6         android:pivotX="50%"
7         android:pivotY="50%"
8         android:duration="5000" />
9 </set>

 

res/anim/scale.xml

     View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <scale android:fromXScale="1.0" 
 5         android:toXScale="0.0"
 6         android:fromYScale="1.0" 
 7         android:toYScale="0.0" 
 8         android:pivotX="50%"
 9         android:pivotY="50%" 
10         android:duration="2000" />
11 </set>

 

res/anim/translate.xml

     View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <translate 
 5         android:fromXDelta="50%" 
 6         android:toXDelta="100%" 
 7         android:fromYDelta="0%" 
 8         android:toYDelta="100%" 
 9         android:duration="2000" />
10 </set>

 

主函数调用如下.

     View Code
 1 public class MainActivity extends Activity {
 2     /** Called when the activity is first created. */
 3     private ImageView imageView = null;
 4     private Button rotateButton = null;
 5     private Button scaleButton = null;
 6     private Button alphaButton = null;
 7     private Button translateButton = null;
 8 
 9     public void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.main);
12         imageView = (ImageView) findViewById(R.id.imageViewId);
13 
14         rotateButton = (Button) findViewById(R.id.rotateButtonId);
15         rotateButton.setOnClickListener(new RotateButtonListener());
16 
17         scaleButton = (Button) findViewById(R.id.scaleButtonId);
18         scaleButton.setOnClickListener(new ScaleButtonListener());
19 
20         alphaButton = (Button) findViewById(R.id.alphaButtonId);
21         alphaButton.setOnClickListener(new AlphaButtonListener());
22 
23         translateButton = (Button) findViewById(R.id.translateButtonId);
24         translateButton.setOnClickListener(new TranslateButtonListener());
25     }
26 
27     private class RotateButtonListener implements OnClickListener {
28         public void onClick(View view) {
29             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
30             imageView.startAnimation(animation);
31         }
32     }
33 
34     private class ScaleButtonListener implements OnClickListener {
35         public void onClick(View view) {
36             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.sacle);
37             imageView.startAnimation(animation);
38         }
39     }
40 
41     private class AlphaButtonListener implements OnClickListener {
42         public void onClick(View view) {
43             //使用AnimationUtils装载动画设置文件
44             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
45             imageView.startAnimation(animation);
46         }
47     }
48 
49     private class TranslateButtonListener implements OnClickListener {
50         public void onClick(View view) {
51             Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
52             imageView.startAnimation(animation);
53         }
54     }
55 }

 

 


你可能感兴趣的:(android)