情况:
在android4.0.4的系统设置-关于手机-Android版本,连续快速点击几下,就会出现一个机器人,长按住机器人不放,机器人会一直变大,然后出现一堆机器从屏幕上飞过
觉得有点意思,于是想看看是怎么实现的,电脑上有android4.0的代码。
系统设置-关于手机的代码:
packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java
其中,点击选项的处理如下:
long
[] mHits =
new
long
[
3
];
- @Override
- public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
- if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
- System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
- mHits[mHits.length-1] = SystemClock.uptimeMillis();
- if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.setClassName("android",
- com.android.internal.app.PlatLogoActivity.class.getName());
- try {
- startActivity(intent);
- } catch (Exception e) {
- Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
- }
- }
- }
- return super.onPreferenceTreeClick(preferenceScreen, preference);
- }
这里可以看到对固件版本选项有特殊处理:
if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
//如果点击的是固件版本,则记录下最近三次点击的时间;
//如果三次点击的总时间少于500ms,则启动com.android.internal.app.PlatLogoActivity
}
再看PlatLogoActivity的实现:
- public class PlatLogoActivity extends Activity {
- Toast mToast;
- ImageView mContent;
- Vibrator mZzz = new Vibrator();
- int mCount;
- final Handler mHandler = new Handler();
-
- Runnable mSuperLongPress = new Runnable() {
- public void run() {
- mCount++;
- mZzz.vibrate(50 * mCount);
- final float scale = 1f + 0.25f * mCount * mCount;
- mContent.setScaleX(scale);
- mContent.setScaleY(scale);
-
- if (mCount <= 3) {
- mHandler.postDelayed(mSuperLongPress, ViewConfiguration.getLongPressTimeout());
- } else {
- try {
- startActivity(new Intent(Intent.ACTION_MAIN)
- .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_CLEAR_TASK
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
- .setClassName("com.android.systemui","com.android.systemui.Nyandroid"));
- } catch (ActivityNotFoundException ex) {
- android.util.Log.e("PlatLogoActivity", "Couldn't find platlogo screensaver.");
- }
- finish();
- }
- }
- };
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- mToast = Toast.makeText(this, "Android 4.0: Ice Cream Sandwich", Toast.LENGTH_SHORT);
-
- mContent = new ImageView(this);
- mContent.setImageResource(com.android.internal.R.drawable.platlogo);
- mContent.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
-
- mContent.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- final int action = event.getAction();
- if (action == MotionEvent.ACTION_DOWN) {
- mContent.setPressed(true);
- mHandler.removeCallbacks(mSuperLongPress);
- mCount = 0;
- mHandler.postDelayed(mSuperLongPress, 2*ViewConfiguration.getLongPressTimeout());
- } else if (action == MotionEvent.ACTION_UP) {
- if (mContent.isPressed()) {
- mContent.setPressed(false);
- mHandler.removeCallbacks(mSuperLongPress);
- mToast.show();
- }
- }
- return true;
- }
- });
-
- setContentView(mContent);
- }
- }
这里对图片的触摸事件进行了处理,
mHandler.postDelayed(mSuperLongPress, 2*ViewConfiguration.getLongPressTimeout());
在图片按下开始,2倍长按超时时间后,执行mSuperLongPress,而在mSuperLongPress中会设置图片缩放比例,逐渐放大。图片连续放大超过三次后,启动com.android.systemui.Nyandroid,显示一堆机器人。
frameworks/base/packages/SystemUI/src/com/android/systemui/Nyandroid.java
- public class Nyandroid extends Activity {
- final static boolean DEBUG = false;
-
- public static class Board extends FrameLayout
- {
- public static final boolean FIXED_STARS = true;
- public static final int NUM_CATS = 20;
-
- static Random sRNG = new Random();
-
- static float lerp(float a, float b, float f) {
- return (b-a)*f + a;
- }
-
- static float randfrange(float a, float b) {
- return lerp(a, b, sRNG.nextFloat());
- }
-
- static int randsign() {
- return sRNG.nextBoolean() ? 1 : -1;
- }
-
- static <E> E pick(E[] array) {
- if (array.length == 0) return null;
- return array[sRNG.nextInt(array.length)];
- }
-
- public class FlyingCat extends ImageView {
- public static final float VMAX = 1000.0f;
- public static final float VMIN = 100.0f;
-
- public float v, vr;
-
- public float dist;
- public float z;
-
- public ComponentName component;
-
- public FlyingCat(Context context, AttributeSet as) {
- super(context, as);
- setImageResource(R.drawable.nyandroid_anim);
-
- if (DEBUG) setBackgroundColor(0x80FF0000);
- }
-
- public String toString() {
- return String.format("<cat (%.1f, %.1f) (%d x %d)>",
- getX(), getY(), getWidth(), getHeight());
- }
-
- public void reset() {
- final float scale = lerp(0.1f,2f,z);
- setScaleX(scale); setScaleY(scale);
-
- setX(-scale*getWidth()+1);
- setY(randfrange(0, Board.this.getHeight()-scale*getHeight()));
- v = lerp(VMIN, VMAX, z);
-
- dist = 0;
-
-
- }
-
- public void update(float dt) {
- dist += v * dt;
- setX(getX() + v * dt);
- }
- }
-
- TimeAnimator mAnim;
-
- public Board(Context context, AttributeSet as) {
- super(context, as);
-
- setLayerType(View.LAYER_TYPE_HARDWARE, null);
- setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
- setBackgroundColor(0xFF003366);
- }
-
- private void reset() {
-
- removeAllViews();
-
- final ViewGroup.LayoutParams wrap = new ViewGroup.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT,
- ViewGroup.LayoutParams.WRAP_CONTENT);
-
- if (FIXED_STARS) {
- for(int i=0; i<20; i++) {
- ImageView fixedStar = new ImageView(getContext(), null);
- if (DEBUG) fixedStar.setBackgroundColor(0x8000FF80);
- fixedStar.setImageResource(R.drawable.star_anim);
- addView(fixedStar, wrap);
- final float scale = randfrange(0.1f, 1f);
- fixedStar.setScaleX(scale); fixedStar.setScaleY(scale);
- fixedStar.setX(randfrange(0, getWidth()));
- fixedStar.setY(randfrange(0, getHeight()));
- final AnimationDrawable anim = (AnimationDrawable) fixedStar.getDrawable();
- postDelayed(new Runnable() {
- public void run() {
- anim.start();
- }}, (int) randfrange(0, 1000));
- }
- }
-
- for(int i=0; i<NUM_CATS; i++) {
- FlyingCat nv = new FlyingCat(getContext(), null);
- addView(nv, wrap);
- nv.z = ((float)i/NUM_CATS);
- nv.z *= nv.z;
- nv.reset();
- nv.setX(randfrange(0,Board.this.getWidth()));
- final AnimationDrawable anim = (AnimationDrawable) nv.getDrawable();
- postDelayed(new Runnable() {
- public void run() {
- anim.start();
- }}, (int) randfrange(0, 1000));
- }
-
- if (mAnim != null) {
- mAnim.cancel();
- }
- mAnim = new TimeAnimator();
- mAnim.setTimeListener(new TimeAnimator.TimeListener() {
- public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
-
-
-
- for (int i=0; i<getChildCount(); i++) {
- View v = getChildAt(i);
- if (!(v instanceof FlyingCat)) continue;
- FlyingCat nv = (FlyingCat) v;
- nv.update(deltaTime / 1000f);
- final float catWidth = nv.getWidth() * nv.getScaleX();
- final float catHeight = nv.getHeight() * nv.getScaleY();
- if ( nv.getX() + catWidth < -2
- || nv.getX() > getWidth() + 2
- || nv.getY() + catHeight < -2
- || nv.getY() > getHeight() + 2)
- {
- nv.reset();
- }
- }
- }
- });
- }
-
- @Override
- protected void onSizeChanged (int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w,h,oldw,oldh);
-
- post(new Runnable() { public void run() {
- reset();
- mAnim.start();
- } });
- }
-
-
- @Override
- protected void onDetachedFromWindow() {
- super.onDetachedFromWindow();
- mAnim.cancel();
- }
-
- @Override
- public boolean isOpaque() {
- return true;
- }
- }
-
- private Board mBoard;
-
- @Override
- public void onStart() {
- super.onStart();
-
- getWindow().addFlags(
- WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
- | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
- );
- }
-
- @Override
- public void onResume() {
- super.onResume();
- mBoard = new Board(this, null);
- setContentView(mBoard);
-
- mBoard.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
- @Override
- public void onSystemUiVisibilityChange(int vis) {
- if (0 == (vis & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)) {
- Nyandroid.this.finish();
- }
- }
- });
- }
-
- @Override
- public void onUserInteraction() {
-
- finish();
- }
- }
这里面FlyingCat继承了ImageView,每次产生一个FlyingCat,生成的图片大小都是随机的。
Board继承了FrameLayout,所有的星星和机器人图片都添加到Board中。
Board使用TimeAnimator定时刷新每个机器人图片的位置,实现了移动。
星星和机器本身的变化是同过boolean postDelayed(Runnable r, long delayMillis)实现自身形状变化的。
- postDelayed(new Runnable() {
- public void run() {
- anim.start();
- }}, (int) randfrange(0, 1000));