활동이로드 될 때 스크롤 뷰에 텍스트 파일 (.txt)을 표시하는 응용 프로그램을 개발 중입니다. 텍스트 파일 바로 아래에 두 개의 버튼 (playAnimation 및 playAudio)이 있습니다.이 버튼을 누르면 해당 파일 (각각 swf 및 mp3)이 재생됩니다.동일한 활동을 사용하면서 표시되는 내용 변경하기
이제는 여러 파일에 대해 작동하도록 현재 코드를 수정하고 싶습니다. (여러 가지 활동을 통해 동일한 작업을 수행 할 수 있지만 중복되거나 프로그래밍하기에 적합하지는 않습니다.) 많은 수의 텍스트 파일과 해당 애니메이션 및 오디오 파일이 있습니다. 동일한 코드를 사용하고 파일 이름 (즉, 텍스트, 애니메이션 및 오디오 파일의 파일 이름) 만 동적으로 변경하려고합니다.
내 MainActivity는 다음과 같습니다 (코드의 주석 참조). 변경이 완료되어야 함) :
public class MainActivity extends Activity
{
Button playAnimationButton,playAudioButton;
MediaPlayer mp;
Context contextObject;
GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
setContentView(R.layout.activity_main);
ReadText readText=new ReadText(this);
TextView helloTxt = (TextView)findViewById(R.id.displaytext);
String fileName="textone";
// code to be written for getting the current page name and storing it in the String fileName
helloTxt.setText(readText.readTxt(fileName));
String audioName="pg3";
//Code to be written for getting the the current audioName
AssetFileDescriptor afd;
try {
afd=getAssets().openFd(audioName + ".mp3");
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepareAsync();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
}
return true;
}
});
playAnimationButton=(Button)findViewById(R.id.playanimation);
playAnimationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
startAnimation.putExtra("SWF_NAME","a");
// code for sending the particular animation file corresponding to the current page
startActivity(startAnimation);
}
});
playAudioButton=(Button)findViewById(R.id.playaudio);
playAudioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//code for dynamically sending the particular audio file associated with the current page
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The Code for loading the next text file with its corresponding audio and animation on buttons.
// left to right swipe
} else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The code for loading the previous text file with its corresponding audio and animation buttons.
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
이 작업을 어떻게 진행할 수 있는지에 대한 제안 사항은 매우 만족 스럽습니다.
미리 감사드립니다. (누군가가 질문에 투표했습니다. 그럴 이유는 분명합니다.)
파일 이름을 전달하고 그것을 역동적으로 팽창시킬 수있는 방법을 작성하십시오. – Abx
@Abhilash 답장을 보내 주셔서 감사합니다. 나는 그것을 할 수 있었지만 나는 많은 파일을 가지고있다. 그리고 내가 다시 스 와이프하면 어떻게 마지막 텍스트의 상태를 저장하고 해당 swf 및 .mp3를 다시로드 할 수 있습니다. –