securesmartcam / app / src / main / java / org / witness / obscuracam / ui / AnimatedBlockView.java @ 41590feb
History | View | Annotate | Download (2.83 KB)
1 |
package org.witness.obscuracam.ui; |
---|---|
2 |
|
3 |
import java.util.Random; |
4 |
|
5 |
import android.content.Context; |
6 |
import android.graphics.Canvas; |
7 |
import android.graphics.Color; |
8 |
import android.graphics.Paint; |
9 |
import android.os.Handler; |
10 |
import android.os.Message; |
11 |
import android.util.AttributeSet; |
12 |
import android.view.MotionEvent; |
13 |
import android.view.View; |
14 |
import android.view.View.MeasureSpec; |
15 |
|
16 |
public class AnimatedBlockView extends View implements Runnable { |
17 |
|
18 |
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
19 |
|
20 |
private float initX, initY, radius; |
21 |
private boolean drawing = false; |
22 |
|
23 |
Random rand = new Random(); |
24 |
|
25 |
private Handler mHandler = new Handler() |
26 |
{ |
27 |
public void handleMessage(Message msg) { |
28 |
|
29 |
invalidate(); |
30 |
} |
31 |
}; |
32 |
|
33 |
|
34 |
public AnimatedBlockView(Context context) { |
35 |
super(context);
|
36 |
// TODO Auto-generated constructor stub
|
37 |
init(); |
38 |
|
39 |
} |
40 |
|
41 |
public AnimatedBlockView(Context context, AttributeSet attrs) { |
42 |
super(context, attrs);
|
43 |
init(); |
44 |
} |
45 |
|
46 |
public AnimatedBlockView(Context context, AttributeSet attrs, int defStyle) { |
47 |
super(context, attrs, defStyle);
|
48 |
|
49 |
init(); |
50 |
} |
51 |
|
52 |
private void init(){ |
53 |
paint.setStyle(Paint.Style.FILL);
|
54 |
paint.setColor(Color.WHITE);
|
55 |
paint.setAntiAlias(true);
|
56 |
|
57 |
new Thread (this).start(); |
58 |
|
59 |
} |
60 |
|
61 |
@Override
|
62 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
63 |
// TODO Auto-generated method stub
|
64 |
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), |
65 |
MeasureSpec.getSize(heightMeasureSpec)); |
66 |
} |
67 |
|
68 |
int a1 = 30; |
69 |
int a2 = 255; |
70 |
|
71 |
@Override
|
72 |
protected void onDraw(Canvas canvas) { |
73 |
|
74 |
|
75 |
for (int i = 0; i < 20; i++) |
76 |
{ |
77 |
float r = rand.nextFloat()*255f; |
78 |
float g = rand.nextFloat()*255f; |
79 |
float b = rand.nextFloat()*255f; |
80 |
|
81 |
paint.setARGB(a1,(int)r,(int)g,(int)b); |
82 |
|
83 |
float x = rand.nextFloat() * getWidth();
|
84 |
float y = rand.nextFloat() * getHeight();
|
85 |
|
86 |
float w = rand.nextFloat() * getWidth();
|
87 |
float h = rand.nextFloat() * getHeight();
|
88 |
|
89 |
canvas.drawRect(x, y, w, h, paint); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
int a1mod = 1; |
95 |
|
96 |
public void updateAlpha () |
97 |
{ |
98 |
a1 += a1mod; |
99 |
|
100 |
if (a1 > 255 || a1 < 0) |
101 |
a1mod *= -1;
|
102 |
|
103 |
|
104 |
} |
105 |
|
106 |
public void run () |
107 |
{ |
108 |
|
109 |
|
110 |
while (true) |
111 |
{ |
112 |
|
113 |
|
114 |
try
|
115 |
{ |
116 |
Thread.sleep(100); |
117 |
} |
118 |
catch (Exception e) |
119 |
{} |
120 |
|
121 |
mHandler.sendEmptyMessage(0);
|
122 |
|
123 |
} |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean onTouchEvent(MotionEvent event) { |
128 |
|
129 |
|
130 |
int action = event.getAction();
|
131 |
if (action==MotionEvent.ACTION_MOVE){
|
132 |
float x = event.getX();
|
133 |
float y = event.getY();
|
134 |
|
135 |
// radius = (float) Math.sqrt(Math.pow(x-initX, 2) + Math.pow(y-initY, 2));
|
136 |
//updateAlpha();
|
137 |
|
138 |
a1 = (int)(255*(y/((float)getHeight()))); |
139 |
|
140 |
} |
141 |
else if (action==MotionEvent.ACTION_DOWN){ |
142 |
initX = event.getX(); |
143 |
initY = event.getY(); |
144 |
radius = 1;
|
145 |
drawing = true;
|
146 |
|
147 |
|
148 |
} |
149 |
else if (action==MotionEvent.ACTION_UP){ |
150 |
drawing = false;
|
151 |
|
152 |
|
153 |
} |
154 |
|
155 |
return true; |
156 |
} |
157 |
|
158 |
} |