package com.example.musicwork.freeModel; //自由模式的主活动 //此模式只支持4/4拍,只能编写八分音符 import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; //import android.support.annotation.StringDef; //import android.support.v4.app.ActivityCompat; import android.provider.ContactsContract; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.widget.Toast; //import android.media.midi.; import com.example.musicwork.Data; import com.example.musicwork.R; import com.example.musicwork.chuangguan.PianoMusic; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.RandomAccessFile; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; //import android.support.v7.app.AppCompatActivity; public class FreemdActivity extends AppCompatActivity { //进行动态权限申请 private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" }; MusicScoreLayout musicScoreLayout;//自定义乐谱布局 boolean highFlag = false;//记录高音按钮是否选中 boolean lowFlag = false;//记录低音按钮是否选中 //boolean inctimeFlag = false;//记录增时线按钮是否选中 boolean dectimeFlag = false;//记录减时线按钮是否选中 //int position = 0;//记录每个layout在musicLayout里的位置 List noteList = new ArrayList();//存储简谱中的每个音符 int index = 0;//记录每个音符在音符列表里的索引 ArrayList noteLayoutList = new ArrayList();//存储音符layout和增时线 PianoMusic util; String musicName;//音乐名 final Data data = (Data)getApplication(); String username;//用户名 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_freemd); final Data data = (Data)getApplication(); username = data.getUsername(); util = new PianoMusic(this);//音乐播放器 musicScoreInit();//初始化乐谱每个组件大小 musicInfoInit();//初始化标题和拍号作者 btnInit();//初始化按钮并设置监听 } //接受Intent中的数据初始化简谱名和节拍,并设置作者 public void musicInfoInit(){ Bundle bundle = this.getIntent().getExtras(); String flag = bundle.getString("flag"); if (flag.equals("init")){ //传入音乐名和节号 //Log.e("map",""+flag); musicName = bundle.getString("musicName"); String musicBeat = bundle.getString("musicBeat"); //设置bundle中传入的数据(名字,节拍) TextView musicNameView = (TextView)findViewById(R.id.title); TextView musicBeatView = (TextView)findViewById(R.id.score_kind); musicNameView.setText(musicName); musicBeatView.setText(musicBeat); //设置作者 TextView usernameView = (TextView)findViewById(R.id.author); usernameView.setText(username); }else if (flag.equals("open")){ //传入音乐文件信息 int length = bundle.getInt("length"); for (int i=0;i7&¬e<15){ text = note-7; }else if (note>14){ text = note-14; }else { text = note; } noteView.setText(""+text); layout.addView(noteView); if (note>14){//高音 //加高音点 ImageView highDot = new ImageView(this); highDot.setImageResource(R.drawable.dot); highDot.setLayoutParams(new LinearLayout.LayoutParams(20,20)); layout.addView(highDot,0); }else if (note<8){//低音 ImageView lowDot = new ImageView(this); lowDot.setImageResource(R.drawable.dot); lowDot.setLayoutParams(new LinearLayout.LayoutParams(20,20)); layout.addView(lowDot,layout.getChildCount()); } if (beat<1){//加减时线 //Log.e("map","减时线"); TextView decLine = new TextView(this); decLine.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); decLine.setHeight(3); decLine.setBackgroundColor(Color.BLACK); if(note<8){//减时线放在低音点的上面 layout.addView(decLine,layout.getChildCount()-1); }else{//高音点 layout.addView(decLine,layout.getChildCount()); } musicScoreLayout.addView(layout); }else if (beat>1){//加增时线 int num = (int)beat; if (beat-num != 0){ //加减时线 TextView decLine = new TextView(this); decLine.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); decLine.setHeight(3); decLine.setBackgroundColor(Color.BLACK); if(note<8){//减时线放在低音点的上面 layout.addView(decLine,layout.getChildCount()-1); }else{//高音点 layout.addView(decLine,layout.getChildCount()); } } musicScoreLayout.addView(layout); //加增时线 for (int j=1;j14){//音符已经为升号 Toast.makeText(getApplicationContext(),"不允许同一个音符上存在升号和降号",Toast.LENGTH_SHORT).show(); }else if (note.getNote()-7>0){ note.setNote(note.getNote()-7);//更改音符为降号 ImageView lowDot = new ImageView(v.getContext()); lowDot.setImageResource(R.drawable.dot); lowDot.setLayoutParams(new LinearLayout.LayoutParams(20,20)); //下外边距被填满 param.setMargins(0,20,0,3); noteView.setLayoutParams(param); layout.addView(lowDot,layout.getChildCount()); }else{ Toast.makeText(getApplicationContext(),"不允许音符继续降号",Toast.LENGTH_SHORT).show(); } } //减时线 if(dectimeFlag){ MusicNote note = noteList.get(v.getId()); note.setBeat(note.getBeat()-(float)1/2);//更改节拍 if (note.isDectimeFlag()){ Toast.makeText(v.getContext(),"不允许重复添加减时线",Toast.LENGTH_SHORT).show(); }else { TextView decLine = new TextView(v.getContext()); decLine.setBackgroundColor(Color.BLACK); LinearLayout.LayoutParams decparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 3); //对齐 if (note.getNote() < 8) {//减时线放在低音点的上面 param.setMargins(0, 20, 0, 0); decLine.setLayoutParams(decparam); layout.addView(decLine, layout.getChildCount() - 1); } else if (note.getNote() > 14) {//高音点 param.setMargins(0, 0, 0, 0); decparam.setMargins(0, 0, 0, 20); decLine.setLayoutParams(decparam); layout.addView(decLine, layout.getChildCount()); } else { param.setMargins(0, 20, 0, 0); decparam.setMargins(0, 0, 0, 20); decLine.setLayoutParams(decparam); layout.addView(decLine, layout.getChildCount()); } noteView.setLayoutParams(param); note.setDectimeFlag(true);//表示添加了减时线 //只能先在前一个音符上加线,再在后一个音符上加? //判断前后音符是否添加了减时线 //有则进行布局合并 int pos = layout.getId();//布局在musicLayoutList中的索引 int dex = v.getId();//音符在音符列表中的索引 int forward = dex-1; int later = dex+1; if (pos-1>=0){//第一个音符不用看前面音符 View forview = noteLayoutList.get(pos-1);//得到此布局的前一个view if(!(forview instanceof TextView)){//前一个音符不是增时线 if (noteList.get(forward).isDectimeFlag()){//前一个音符加了减时线 LinearLayout anotherLayout = (LinearLayout)forview;//前一个布局 LinearLayout curLayout = (LinearLayout)noteLayoutList.get(pos);//本布局 if (anotherLayout.getChildAt(0) instanceof LinearLayout){ //Toast.makeText(v.getContext(),"只能有八分音符",Toast.LENGTH_SHORT).show(); Log.e("freemd_dectime","只能有八分音符"); }else { musicScoreLayout.removeAllViews();//把所有音符全部移除 LinearLayout newLayout = new LinearLayout(v.getContext()); //anotherLayout newLayout.addView(anotherLayout); newLayout.addView(curLayout); noteLayoutList.remove(pos); noteLayoutList.remove(pos-1); noteLayoutList.add(pos-1,newLayout); //重新画乐谱 for (int i=0;i