-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity
More file actions
88 lines (76 loc) · 3.62 KB
/
MainActivity
File metadata and controls
88 lines (76 loc) · 3.62 KB
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
public class MainActivity extends AppCompatActivity {
private Context mContext;
private TextView tv_gradient;
private Button btn_apply;
private RadioGroup mradio_roup;
private int mWidth;
private int mHeight;
private Random mRandom = new Random();
private Shader shader;
private GradientManager mGradientManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
mContext = getApplicationContext();
// Get the widgets reference from XML layout
tv_gradient = (TextView) findViewById(R.id.tv);
btn_apply = (Button) findViewById(R.id.btn);
mradio_roup = (RadioGroup) findViewById(R.id.rg);
// Set a click listener for Button widget
btn_apply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get the TextView width and height in pixels
mWidth = tv_gradient.getWidth();
mHeight = tv_gradient.getHeight();
Point size = new Point(mWidth,mHeight);
// Initializing a new instance of GradientManager class
mGradientManager = new GradientManager(mContext,size);
// Get a random indicator to define next gradient type
int indicator = mRandom.nextInt(3);
if(indicator == 0){
shader = mGradientManager.getRandomLinearGradient();
tv_gradient.setText("Linear Gradient");
}else if(indicator == 1){
shader = mGradientManager.getRandomRadialGradient();
tv_gradient.setText("Radial Gradient");
}else {
shader = mGradientManager.getRandomSweepGradient();
tv_gradient.setText("Sweep Gradient");
}
tv_gradient.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
tv_gradient.getPaint().setShader(shader);
}
});
// Set a checked change listener for RadioGroup
mradio_roup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rb_normal) {
// Remove the mask filter
tv_gradient.getPaint().setMaskFilter(null);
} else if (i == R.id.rb_emboss) {
EmbossMaskFilter embossFilter = new EmbossMaskFilter(
new float[]{1f, 5f, 1f}, // direction of the light source
0.8f, // ambient light between 0 to 1
8, // specular highlights
7f // blur before applying lighting
);
// Apply the emboss mask filter
tv_gradient.getPaint().setMaskFilter(embossFilter);
} else if (i == R.id.rb_deboss) {
EmbossMaskFilter debossFilter = new EmbossMaskFilter(
new float[]{0f, -1f, 0.5f}, // direction of the light source
0.8f, // ambient light between 0 to 1
13, // specular highlights
7.0f // blur before applying lighting
);
// Apply the deboss mask filter
tv_gradient.getPaint().setMaskFilter(debossFilter);
}
}
});
}
}