国外大牛们的博客

Android Weekly


Android Developers Blog

Dave Smith


Romain Guy


Chiu-Ki Chan


Daniel Lew


Mark Allison


Wolfram Rittmeyer


Mark Murphy


Cyril Mottier


Martin van Zuilekom


Chris Banes


Antonio Leiva


More

https://github.com/android-cn/android-dev-com

https://github.com/android-cn/android-dev-cn

https://github.com/futurice/android-best-practices

android 开发全纪录

ActionBar

Activity

Animation

APK

App

DB

Gradle

Hardware

Intent

ListView

Shortcut

TextView

Util

View

Window

textview带有右上角文字

rightuptext pic

1.RightUpperCornerText 代码

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
public class RightUpperCornerText extends TextView {
private Paint mPaint;
private String mCornerText;
private Context mContext;
public RightUpperCornerText(Context context) {
super(context);
init(context);
}
public RightUpperCornerText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RightUpperCornerText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mContext = context;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
float cornerSize = getTextSize() * 0.6f;
if (cornerSize > 50) {
cornerSize = 50;
}
mPaint.setTextSize(cornerSize);
mCornerText = "";
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 不要忘了设置 Padding
String text = (String) getText();
Rect bounds = new Rect();
getPaint().getTextBounds(text, 0, text.length(), bounds);
canvas.drawText(mCornerText, getPaddingLeft() + bounds.right + bounds.left,
(canvas.getHeight() + bounds.top) / 2 + mPaint.measureText(mCornerText),
mPaint);
}
public void setCornertext(String text) {
mCornerText = text;
}
public void setCornertextSize(float size) {
mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
size, mContext.getResources().getDisplayMetrics()));
invalidate();
}
}

2. 用法

1
2
3
4
5
6
7
8
<com.ilegendsoft.toprankapps.cleanmaster.UI.RightUpperCornerText
android:id="@+id/dashboard_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="26sp" />

mPercentageView.setCornertext(cornerText);

注意 android:padding="20dp"设置padding 很重要,否则没地方画了。

3. 其实完全没必要自定义,采用TextView Style可以实现各种样式的字体排版,就是SpannableString

先看下图片

复合文本图片

具体参考如下链接:

listview item进入和删除动画

1.先看一下最终效果

listview

2.从右边划入的动画

  • 定义xml动画
1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/animTime"
android:fromXDelta="100%p"
android:toXDelta="0%p" >
</translate>
  • Adapter里只需要写如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
....
if (!junk.isAnimatedBefore()) {
junk.setAnimatedBefore(true);
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_left_in));
}else{
convertView.clearAnimation();
}
return convertView;
}

3.从左边划出的动画

  • 定义xml动画
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true"
android:duration="@integer/animTime">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p">
</translate>
</set>
  • Adapter里不需要做什么,需要在外面操作ListView.

4.在外面操作ListView

  • 开始动画
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
private void startBoost() {
mIsDeleting = true;
boolean hasAnimation = false;
long offset = 0;
mAnimCount = 0;
int groupPosition = -1;
int childPosition = -1;
Iterator<ArrayList<Junk>> listIterator = mChildren.iterator();
while (listIterator.hasNext()) {
groupPosition++;
final Iterator<Junk> junkIterator = listIterator.next().iterator();
while (junkIterator.hasNext()) {
childPosition++;
Junk junk = junkIterator.next();
if (junk.isChecked()) {
...
junkIterator.remove();
final View itemView = ListViewUtil.getChildItemView(this,
mListView, groupPosition, childPosition);
if (itemView != null) {
hasAnimation = true;
((Animation) itemView.getTag(R.id.anim)).setAnimationListener(mAnimationListener);
((Animation) itemView.getTag(R.id.anim)).setStartOffset(offset);
offset += 100;
itemView.startAnimation(((Animation) itemView.getTag(R.id.anim)));
}
}
}
}
if (!hasAnimation) {
deleteCompleted();
}
}
  • mAnimationListener定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Animation.AnimationListener mAnimationListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mAnimCount++;
}
@Override
public void onAnimationEnd(Animation animation) {
mAnimCount--;
if (mAnimCount == 0) {
deleteCompleted();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
  • 删除完成后需要调用ListViewUtil.clearListViewAnim(this, mListView);,否则原来动画的地方会显示空白。
  • 动画时要禁用OnTouch事件,因为我们做动画时并没有调用mAdapter.notifyDataSetChanged(),所以如果滚动,由于数据没有同步,肯定会挂掉。

禁用OnTouch事件的方法如下

1
2
3
4
5
6
7
8
9
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// intercept touch event when deleting ,avoid listview get view.
if (mIsDeleting) {
return true;
}
return super.dispatchTouchEvent(ev);
}

4.ListView的工具方法

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
public static View getChildItemView(Context context, FloatingGroupExpandableListView listView, int groupPosition, int childPosition) {
long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition);
int flatPosition;
try {
flatPosition = listView.getFlatListPosition(packedPosition);
} catch (Exception e) {
e.printStackTrace();
return null;
}
int firstPosition = listView.getFirstVisiblePosition();
int wantedChild = flatPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
return null;
}
View childItemView = listView.getChildAt(wantedChild);
childItemView.setTag(R.id.anim, AnimationUtils.loadAnimation(context, R.anim.slide_left_out));
return childItemView;
}
public static View getItemView(Context context, ListView listView, int position) {
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
int wantedChild = position - firstPosition;
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
return null;
}
View wantedView = listView.getChildAt(wantedChild);
wantedView.setTag(R.id.anim, AnimationUtils.loadAnimation(context, R.anim.slide_left_out));
return wantedView;
}
public static void clearListViewAnim(Context context, ListView listView) {
int first = listView.getFirstVisiblePosition();
int count = listView.getChildCount() + 2;
for (int i = first; i < first + count; i++) {
View itemView = getItemView(context, listView, i);
if (itemView != null) {
itemView.clearAnimation();
}
}
}

如何确保Listview刷新结束,然后再执行其他操作。

场景:当调用ListviewnotifyDataSetChanged()刷新列表时,执行滚动或点击按钮删除源数据,然后再次刷新列表。

方法:采用Viewpost()方法,把对数据源的操作加入消息队列,等待刷新结束,如下:

1
2
3
4
5
6
7
adapter.notifyDataSetChanged();
view.post( new Runnable() {
@Override
public void run() {
//call smooth scroll
}
});