0%

CountDownTimer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public abstract class CountDownTimer {

/**
* Millis since epoch when alarm should stop.
*/
private final long mMillisInFuture;

/**
* The interval in millis that the user receives callbacks
*/
private final long mCountdownInterval;

private long mStopTimeInFuture;

/**
* boolean representing if the timer was cancelled
*/
private boolean mCancelled = false;


public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}


public synchronized final void cancel() {
mCancelled = true;
mHandler.removeMessages(MSG);
}


public synchronized final CountDownTimer start() {
mCancelled = false;
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}


/**
* Callback fired on regular interval.
* @param millisUntilFinished The amount of time until finished.
*/
public abstract void onTick(long millisUntilFinished);

/**
* Callback fired when the time is up.
*/
public abstract void onFinish();


private static final int MSG = 1;


// handles counting down
private Handler mHandler = new Handler() {

@Override
public void handleMessage(Message msg) {

synchronized (CountDownTimer.this) {
if (mCancelled) {
return;
}

final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

if (millisLeft <= 0) {
onFinish();
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);

// take into account user's onTick taking time to execute
long lastTickDuration = SystemClock.elapsedRealtime() - lastTickStart;
long delay;

if (millisLeft < mCountdownInterval) {
// just delay until done
delay = millisLeft - lastTickDuration;

// special case: user's onTick took more than interval to
// complete, trigger onFinish without delay
if (delay < 0) delay = 0;
} else {
delay = mCountdownInterval - lastTickDuration;

// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
}

sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}
阅读全文 »

Property Animation

Introduced in Android 3.0 (API level 11), the property animation system lets you animate properties of any object, including ones that are not rendered to the screen. The system is extensible and lets you animate properties of custom types as well.

1
2
3
4
5
6
7
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start();

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();
1
2
3
ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();

View Animation

View Animation is the older system and can only be used for Views. It is relatively easy to setup and offers enough capabilities to meet many application’s needs.

阅读全文 »

1.SharedPreference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static SharedPreferences sp;
private static void init(Context context) {
if (sp == null) {
sp = PreferenceManager.getDefaultSharedPreferences(BaseApplication.getAppContext());
}
}
public static void setSharedIntData(Context context, String key, int value) {
if (sp == null) {
init(context);
}
sp.edit().putInt(key, value).commit();
}
public static int getSharedIntData(Context context, String key) {
if (sp == null) {
init(context);
}
return sp.getInt(key, 0);
}
  • 强引用
  • 软引用(SoftReference):垃圾回收器就不会回收它,如果内存空间不足了,就会回收这些对象的内存
  • 弱引用(WeakReference):一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存
  • 虚引用(PhantomReference) 虚引用主要用来跟踪对象被垃圾回收的活动。虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列( ReferenceQueue)联合使用。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之关联的引用队列中。程序可以通过判断引用队列中是否已经加入了虚引用,来了解被引用的对象是否将要被垃圾回收。
阅读全文 »

1. Notification

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Notification.Builder builder =  new Notification.Builder(context);
PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE, new Intent(), PendingIntent.FLAG_NO_CREATE);

builder.setContentIntent(pendingIntent);

builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("通知内容");
builder.setContentTitle("通知标题");

Notification notification = builder.build();


NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(1,notification);
阅读全文 »

占位符

%1$d

%s
int类 d
string s
double f

TextView

  • textView中的属性

    1
    2
    3
    4
    5
    android:textScaleX="0.9f"  字间距
    android:ellipsize="end" 末尾省略号
    android:ellipsize="marquee" 跑马灯
    android:lineSpacingExtra 设置行间距,如”3dp”
    android:lineSpacingMultiplier 设置行间距的倍数,如”1.2″

    字符转义

    1
    `&#160`;表示全角空格
    阅读全文 »