Android自定义控件-SlidingValidationView

效果图

图2

代码

属性

1
2
3
4
5
6
7
8
9
<declare-styleable name="SlidingValidationView">
<attr name="sv_drag_view_color" format="color|reference" />
<attr name="sv_line_color" format="color|reference" />
<attr name="sv_final_point_color" format="color|reference" />
<attr name="sv_sliding_progress" format="integer" />
<attr name="sv_draw_radius" format="dimension|reference" />
<attr name="sv_final_point_radius" format="dimension|reference" />
<attr name="sv_line_point_radius" format="dimension|reference" />
</declare-styleable>

Android自定义控件-RatioColorBar

效果图

图2

代码

属性

1
2
3
4
5
6
7
<declare-styleable name="RatioColorBar">
<attr name="rcb_show_border" format="boolean" />
<attr name="rcb_show_padding" format="boolean" />
<attr name="rcb_border_width" format="dimension|reference" />
<attr name="rcb_border_padding" format="dimension|reference" />
<attr name="rcb_border_color" format="color|reference" />
</declare-styleable>

Android疑难杂症-控件任意拖动+点击监听

实现代码(ImageView为例)

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.keqiang.highcloud.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;


/**
* Created by KID on 2017/11/14.
* 随意拖动的view
*/
@SuppressLint("AppCompatCustomView")
public class DragView extends ImageView {

private int width;
private int height;
private int screenWidth;
private int screenHeight;
private Context context;

private boolean dragEnable = true;
//是否拖动
private boolean isDrag = false;

public boolean isDrag() {
return isDrag;
}

public boolean isDragEnable() {
return dragEnable;
}

public void setDragEnable(boolean dragEnable) {
this.dragEnable = dragEnable;
}

public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
dragEnable = true;
this.context = context;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
}

private float downX;
private float downY;

@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (this.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDrag = false;
ViewGroup mViewGroup = (ViewGroup) getParent();
if (null != mViewGroup) {
screenWidth = mViewGroup.getWidth();
screenHeight = mViewGroup.getHeight();
}
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
Log.e("kid", "ACTION_MOVE");
if (dragEnable) {
final float xDistance = event.getX() - downX;
final float yDistance = event.getY() - downY;
int l, r, t, b;
//当水平或者垂直滑动距离大于10,才算拖动事件
if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) {
Log.e("kid", "Drag");
isDrag = true;
l = (int) (getLeft() + xDistance);
r = l + width;
t = (int) (getTop() + yDistance);
b = t + height;
//不划出边界判断,此处应按照项目实际情况,因为本项目需求移动的位置是手机全屏,
// 所以才能这么写,如果是固定区域,要得到父控件的宽高位置后再做处理
if (l < 0) {
l = 0;
r = l + width;
} else if (r > screenWidth) {
r = screenWidth;
l = r - width;
}
if (t < 0) {
t = 0;
b = t + height;
} else if (b > screenHeight) {
b = screenHeight;
t = b - height;
}

this.layout(l, t, r, b);
}
}
break;
case MotionEvent.ACTION_UP:
setPressed(false);
break;
case MotionEvent.ACTION_CANCEL:
setPressed(false);
break;
}
return true;
}
return false;
}
}

RxJava-无限重复定时任务

开启任务

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
private void getAllDatas(final long delay) {

//解除上次订阅
if (subscribe != null) {
subscribe.unsubscribe();
subscribe = null;
}

subscribe = Observable.timer(delay, TimeUnit.MILLISECONDS)
.flatMap(new Func1<Long, Observable<GetSomeThingResult>>() {
@Override
public Observable<GetSomeThingResult> call(Long aLong) {
//接口调用
return Api.getDefaultApi().getSomeThing();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<GetSomeThingResult>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
ToastUtils.showCustomBgToast(getString(R.string.no_net_text) + e.toString());
getAllDatas(Constants.AUTO_REFRESH_DURATION);
}

@Override
public void onNext(GetSomeThingResult result) {
//... do some thing with data

getAllDatas(Constants.AUTO_REFRESH_DURATION);
}
});

}

RxJava-模拟进度

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
//0-100 每隔20ms +1
Observable.just(100)
.flatMap(new Func1<Integer, Observable<Long>>() {
@Override
public Observable<Long> call(Integer integer) {
return Observable.timer(20, TimeUnit.MILLISECONDS);
}
})
.compose(RxHelper.<Long>io_main())
.subscribe(new Subscriber<Long>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Long aLong) {
pb.setProgress(aLong.intValue());
}
});

应用场景

  • 计时器
  • 进度模拟
  • 定时任务

Android常用代码-bugly集成

集成bugly

1
2
//bugly
compile 'com.tencent.bugly:crashreport:latest.release'

添加权限

1
2
3
<!-- bugly start-->
<uses-permission android:name="android.permission.READ_LOGS" />
<!--bugly end-->

Application的onCreate方法中初始化

不带X5内核

1
2
3
4
5
try {
CrashReport.initCrashReport(this, "你的appID", false);
} catch (Exception e) {
e.printStackTrace();
}

带X5内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*bugly+x5内核*/
try {
CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(this);
strategy.setCrashHandleCallback(new CrashReport.CrashHandleCallback() {
public Map onCrashHandleStart(int crashType, String errorType, String errorMessage, String errorStack) {
LinkedHashMap map = new LinkedHashMap();
String x5CrashInfo = com.tencent.smtt.sdk.WebView.getCrashExtraMessage(GFApplication.this);
map.put("x5crashInfo", x5CrashInfo);
return map;
}

@Override
public byte[] onCrashHandleStart2GetExtraDatas(int crashType, String errorType, String errorMessage, String errorStack) {
try {
return "Extra data.".getBytes("UTF-8");
} catch (Exception e) {
return null;
}
}
});
CrashReport.initCrashReport(this, "你的appID", false, strategy);
} catch (Exception e) {
e.printStackTrace();
}

Android签名打包-多渠道打包

复制libs

  • common
  • analytics

集成友盟+ App统计功能

在Application的onCreate方法中调用

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 初始化common库
* 参数1:上下文,不能为空
* 参数2:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
* 参数3:Push推送业务的secret
*/
try {
UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "你的appID");
} catch (Exception e) {
e.printStackTrace();
}

添加相应权限

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 必须的权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- 推荐的权限 -->
<!-- 添加如下权限,以便使用更多的第三方SDK和更精准的统计数据 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

配置渠道

1
2
3
4
5
6
7
8
9
<manifest>
<application ……>
……
<!--umeng start-->
<meta-data android:value="你的AppID" android:name="UMENG_APPKEY"/>
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
<!--umeng end-->
</application>
</manifest>

代码混淆

1
keep class com.umeng.commonsdk.** {*;}

多渠道配置

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
android {
productFlavors {
wandoujia {}
yingyongbao {}
c360 {}
kq {}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}

applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def fileName
def flavor = productFlavors.name;
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {
fileName = "名字_${defaultConfig.versionName}_${flavor}.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "名字_${defaultConfig.versionName}_debug_${flavor}.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}