Newer
Older
package im.delight.android.identicons;
/**
* Copyright 2014 www.delight.im <info@delight.im>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
abstract public class IdenticonView extends View {
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
public IdenticonView(Context context) {
super(context);
init();
}
public IdenticonView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public IdenticonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@SuppressLint("NewApi")
protected void init() {
setWillNotDraw(false);
if (Build.VERSION.SDK_INT >= 11) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
public void show(byte[] input) {
getDelegate().show(input);
invalidate();
}
public void show(String input) {
show(input.getBytes());
}
public void show(int input) {
show(String.valueOf(input));
}
public void show(long input) {
show(String.valueOf(input));
}
public void show(float input) {
show(String.valueOf(input));
}
public void show(double input) {
show(String.valueOf(input));
}
public void show(byte input) {
show(new byte[] { input });
}
public void show(char input) {
show(String.valueOf(input));
}
public void show(boolean input) {
show(String.valueOf(input));
}
public void show(Object input) {
if (input == null) {
getDelegate().show(null);
} else {
show(String.valueOf(input));
}
}
abstract protected IdenticonBase getDelegate();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
getDelegate().updateSize(w, h);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(size, size);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
getDelegate().draw(canvas);
}