-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvibration.java
More file actions
169 lines (109 loc) · 3.91 KB
/
vibration.java
File metadata and controls
169 lines (109 loc) · 3.91 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.kawabata.tsukuba.randomvibration;
import android.content.Context;
import android.os.Vibrator;
import android.renderscript.Sampler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Locale;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView textView;
RandomVibration v2 = new RandomVibration();
RepeatVibration v1 = new RepeatVibration();
boolean stopF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stopF=true;
// textView = findViewById(R.id.text_view);
// SeekBar
SeekBar seekBar = findViewById(R.id.seekbar);
// 初期値
seekBar.setProgress(0);
// 最大値
seekBar.setMax(14);
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
//ツマミがドラッグされると呼ばれる
@Override
public void onProgressChanged(
SeekBar seekBar, int progress, boolean fromUser) {
// 68 % のようにフォーマト、
// この場合、Locale.USが汎用的に推奨される
//String str = String.format(Locale.US, "%d mode%",progress);
// textView.setText(str);
v1.setFreq(progress);
v1.doVibration();
}
//ツマミがタッチされた時に呼ばれる
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
stopF=false;
}
//ツマミがリリースされた時に呼ばれる
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void onStop(View v){
v1.stopVibration();
v2.stopVibration();
stopF=false;
}
public void onClickRandom(View v){
//v2.doVibration();
stopF=true;
new Thread(new Runnable() {
@Override
public void run() {
v2.doVibration1();
// マルチスレッドにしたい処理 ここまで
}
}).start();
}
abstract class Vibration{
abstract void doVibration();
long[] NumberData={160,150,140,130,120,110,100,90,80,70,60,50,40,30,10};
void stopVibration(){
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).cancel();
}
}
class RepeatVibration extends Vibration{
int freq;
void RepeatVibration(){
}
void setFreq(int frequancy){
this.freq=frequancy;
}
void doVibration() {
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate( new long[]{NumberData[freq],NumberData[freq]}, 0 );
}
}
class RandomVibration extends Vibration{
long dv;
long nv;
Random rand= new Random();
Random rand2=new Random();
void doVibration() {
doVibration1();
}
void doVibration1() {
while(true) {
if (stopF) {
this.dv = NumberData[rand.nextInt(12)];
this.nv = NumberData[rand2.nextInt(8)]+4;
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(new long[]{this.dv, this.nv}, -1);
// doVibration();
} else {
super.stopVibration();
break;
}
}
}
}
}