import cn.sherlock.com.sun.media.sound.SF2Soundbank;
import cn.sherlock.com.sun.media.sound.SoftSynthesizer;
import jp.kshoji.javax.sound.midi.Instrument;
import jp.kshoji.javax.sound.midi.InvalidMidiDataException;
import jp.kshoji.javax.sound.midi.MidiUnavailableException;
SF2Soundbank drumsSF = new SF2Soundbank(mContext.getAssets().open("drums.sf2"));
SoftSynthesizer drums = new SoftSynthesizer();
metronome.loadAllInstruments(drumsSF);
for (int i = 0; i < drumsSF.getInstruments().length; i++) {
Instrument inst = drumsSF.getInstruments()[i];
Log.d("MyTest", "name" + inst.getName());
Log.d("MyTest", "bank" + inst.getPatch().getBank());
Log.d("MyTest", "program" + inst.getPatch().getProgram());
Log.d("MyTest", "==========");
}
// programChange(bank, program)
drums.getChannels()[0].programChange(0, 115);
カテゴリー: Java
import cn.sherlock.com.sun.media.sound.SF2Soundbank;
import cn.sherlock.com.sun.media.sound.SoftSynthesizer;
import jp.kshoji.javax.sound.midi.InvalidMidiDataException;
import jp.kshoji.javax.sound.midi.MidiSystem;
import jp.kshoji.javax.sound.midi.MidiUnavailableException;
import jp.kshoji.javax.sound.midi.Receiver;
import jp.kshoji.javax.sound.midi.ShortMessage;
// minimum setup for playing notes without using a Sequencer
// it plays random 3rd dyad each time
SF2Soundbank sf = new SF2Soundbank(getAssets().open("steinway.sf2"));
SoftSynthesizer synth = new SoftSynthesizer();
synth.open();
synth.loadAllInstruments(sf);
Receiver recv = synth.getReceiver;
ShortMessage msg = new ShortMessage();
int max = 72;
int min = 36;
int num = (int)Math.floor(Math.random()*(max-min)+min);
try {
msg.setMessage(ShortMessage.NOTE_ON, 0, num, 127);
recv.send(msg, synth.getMicrosecondPosition());
msg.setMessage(ShortMessage.NOTE_ON, 0, num+4, 127);
recv.send(msg, synth.getMicrosecondPosition());
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
LinearLayout layout = new LinearLayout(this);
layout.setGravity(Gravity.CENTER);
setContentView(layout);
RelativeLayout circleView = new RelativeLayout(this);
GradientDrawable gd = new GradientDrawable();
gd.setSize(screenHeight/3, screenHeight/3);
gd.setColor(Color.WHITE);
gd.setStroke(10, Color.GRAY);
gd.setCornerRadius(screenHeight/3/2);
circleView.setBackground(gd);
layout.addView(circleView);
TextView tvTop = new TextView(this);
tvTop.setText("Top");
tvTop.setTextSize(50);
RelativeLayout.LayoutParams tvTopLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvTopLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tvTopLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
tvTopLP.topMargin = screenHeight / 3 / 5;
tvTop.setLayoutParams(tvTopLP);
circleView.addView(tvTop);
View divider = new View(this);
divider.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams dividerLP = new RelativeLayout.LayoutParams(screenHeight / 3 / 10 * 9, 3);
dividerLP.addRule(RelativeLayout.CENTER_IN_PARENT);
divider.setLayoutParams(dividerLP);
circleView.addView(divider);
TextView tvBottom = new TextView(this);
tvBottom.setText("Bottom");
tvBottom.setTextSize(50);
RelativeLayout.LayoutParams tvBottomLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvBottomLP.addRule(RelativeLayout.ALIGN_PARENT_TOP); // when set to ALIGN_PARENT_TOP, the layout will break
tvBottomLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
tvBottomLP.topMargin = screenHeight / 3 / 5 * 3;
tvBottom.setLayoutParams(tvBottomLP);
circleView.addView(tvBottom);
getSupportActionBar().hide();
}
}
TextView textView = new TextView(this);
String string = "Some String";
// to make it bold (or other styling)
SpannableString spanString = new SpannableString(string);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
// to allow autosize of texts
textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
textView.setText(spanString);
textView.setTextColor(Color.BLUE);
Typeface appFont = getResources().getFont(R.font.oswald); // oswald
textView.setTypeface(appFont);
// centering texts
textView.setGravity(Gravity.CENTER);
// padding
textView.setPadding(10, 10, 10, 10);
// minimum width and hight
textView.setMinimumWidth(20);
textView.setMinimumHeight(20);
fontをAndroid Studioに追加するには
resフォルダ内にfontフォルダを作り、その中に.ttfファイルを置く
Typeface appFont = getResources().getFont(R.font.oswald);
// app launch
onCreate called
onStart called
onResume called
// app screen rotated
onResume called
onStop called
onDestroy called
onCreate called
onStart called
onResume called
// app rotated again
onResume called
onStop called
onDestroy called
onCreate called
onStart called
onResume called
public class MainActivity extends AppCompatActivity {
Button button;
LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
button = new Button(this);
button.setOnClickListener(new OnButtonClicked());
layout.addView(button);
setContentView(layout);
}
private class OnButtonClicked implements View.OnClickListener {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.RED);
ColorDrawable[] colors = {new ColorDrawable(Color.RED), new ColorDrawable(Color.WHITE)};
TransitionDrawable transition = new TransitionDrawable(colors);
layout.setBackground(transition);
transition.startTransition(2000);
Log.d("MyTest", "clicked");
}
}
}
Title Barを消すにはres -> values -> themes -> themes.xml 内の下記を変更する
<style name="Theme.MidiPractice" parent="Theme.MaterialComponents.DayNight.NoActionBar">
もしくは、ActivityのonCreate()で以下を実行する
getSupportActionBar().hide();
StatusBarの色を変更するには res -> values -> themes -> themes.xml 内の下記を変更する
<item name="android:statusBarColor" tools:targetApi="l">@color/purple_200</item>
StatusBarのBackgroundColorを白にする場合は以下のように変更
<item name="android:statusBarColor" tools:targetApi="l">@color/white</item>
<item name="android:windowLightStatusBar">true</item>
private int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
private int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
JavaでString型の値を比べるときは == 演算子ではなく、equals()関数を使います。
"blog.kobito.something" == "blog.kobito.something // this returns false
new String("blog.kobito.something").equals("blog.kobito.something") // this returns true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false