日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

[转]大话企业级Android应用开发实战 音乐播放器的开发

發(fā)布時間:2024/4/14 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]大话企业级Android应用开发实战 音乐播放器的开发 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

29.2? 創(chuàng) 建 界 面

在main.xml中添按鈕等控件,完成音樂播放器的界面,代碼如下:

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? >

??? <TextView?

??? ??? android:layout_width="fill_parent"

??? ??? android:layout_height="wrap_content"

??? ??? android:text="@string/filename"

??? ??? />

??? <EditText???

??? android:layout_width="fill_parent"

??? android:layout_height="wrap_content"

??? android:text="aa.mp3"

??? android:id="@+id/filename"

??? />

??? <LinearLayout

??? android:orientation="horizontal"

??? ??? android:layout_width="fill_parent"

??? ??? android:layout_height="wrap_content"

??? ??? >

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:text="@string/start"

??? ??? android:id="@+id/startbutton"

??? ??? />

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"??? ????

??? ??? android:text="@string/pause"

??? ??? android:id="@+id/pausebutton"

??? ??? />

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"???

??? ??? android:text="@string/stop"

??? ??? android:id="@+id/stopbutton"

??? ??? />

??? ?</LinearLayout>

</LinearLayout>

29.3? 業(yè) 務(wù) 代 碼

具體實(shí)現(xiàn)代碼如下:

MusicPlayerActivity.java

package com.sharpandroid.activity;

import android.app.Activity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MusicPlayerActivity extends Activity {

??? private static final String TAG = "MusicPlayerActivity";

??? private EditText namefileText;

??? private MediaPlayer mediaPlayer;

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? setContentView(R.layout.main);

??????? ButtonClickEnvent envent = new ButtonClickEnvent();

??????? Button startbutton = (Button)this.findViewById(R.id.startbutton);

??????? startbutton.setOnClickListener(envent);

??????? Button pausebutton = (Button)this.findViewById(R.id.pausebutton);

??????? pausebutton.setOnClickListener(envent);

??????? Button stopbutton = (Button)this.findViewById(R.id.stopbutton);

??????? stopbutton.setOnClickListener(envent);

??????? namefileText = (EditText) this.findViewById(R.id.filename);

??????? mediaPlayer = new MediaPlayer();

??? }

???

??? private class ButtonClickEnvent implements View.OnClickListener{

??????? @Override

??????? public void onClick(View v) {

??????????? try {

??????????????? switch (v.getId()) {

??????????????? case R.id.startbutton://播放

??????????????????? String filename = namefileText.getText().toString();

??????????????????? mediaPlayer.reset();//重設(shè)

??????????????????? mediaPlayer.setDataSource("/sdcard/"+ filename);

??????????????????? mediaPlayer.prepare();//緩沖????????????

??????????????????? mediaPlayer.start();//播放

??????????????????? break;

?

??????????????? case R.id.pausebutton://暫停

??????????????????? if(mediaPlayer.isPlaying()){//正在播放就暫停

??????????????????????? mediaPlayer.pause();

??????????????????????? ((Button) v).setText("繼續(xù)");

??????????????????? }else{//否則就播放

??????????????????????? mediaPlayer.start();

??????????????????????? ((Button) v).setText("暫停");

??????????????????? }

??????????????????? break;

???????????????????

??????????????? case R.id.stopbutton://停止

??????????????????? if(mediaPlayer.isPlaying()){

??????????????????????? mediaPlayer.stop();

??????????????????? }

??????????????????? break;

??????????????? }

??????????? } catch (Exception e) {

??????????????? Log.e(TAG, e.toString());

??????????? }??????????

??????? }??? ???

??? }

?

??? @Override

??? protected void onPause() {

??????? if(mediaPlayer!=null){

??????????? if(mediaPlayer.isPlaying()) mediaPlayer.pause();

??????? }

??????? super.onPause();

??? }

?

??? @Override

??? protected void onResume() {

??????? if(mediaPlayer!=null){

??????????? if(!mediaPlayer.isPlaying()) mediaPlayer.start();

??????? }

??????? super.onResume();

??? }

?

??? @Override

??? protected void onDestroy() {

??????? if(mediaPlayer!=null){

??????????? if(mediaPlayer.isPlaying()) mediaPlayer.stop();

??????????? mediaPlayer.release();

??????? }

??????? super.onDestroy();

??? }

???

???

}

? ? ? ? ? ? ? ? ? ? ? ? 30? 音樂在線播放的開發(fā)?

?

?

30.2? 界??? 面

在main.xml中的代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout ? xmlns:android="http://schemas.android.com/apk/res/

android"

??????????? ??? android:orientation="vertical"

??????????? ??? android:layout_width="fill_parent"

??????????? ??? android:layout_height="fill_parent"

??????????? ??? android:padding="10px"

??????????? ??? android:background="@drawable/back">

?

??? <TextView? android:id="@+id/text_kb_streamed"

??????????????? android:layout_width="fill_parent"

??????????? ??? android:layout_height="wrap_content"

??????????? ??? android:textStyle="bold"

??? ???????? android:text="流媒體測試"/>

???

?<Button??? android:id="@+id/button_stream"

??? ??????? android:layout_width="wrap_content"

??? ??????? android:layout_height="wrap_content"

??????? ??? android:layout_marginTop="10px"

??????????? style="?android:attr/buttonStyleSmall"

??? ??????? android:text="開始緩沖"/>

?? <RelativeLayout

???????????????? android:layout_width="wrap_content"

??????????? ??? android:layout_height="wrap_content"

??? >

??????? ??? <SeekBar

????????????????? android:id="@+id/progress_bar"

????????????????? android:layout_height="wrap_content"

?? ???????????????android:layout_width="200px"

????????????????? style="?android:attr/progressBarStyleHorizontal"

????????????? />

????? <TextView?

????????????? android:id="@+id/playTime"

????????????? android:layout_width="wrap_content"

????????????? android:layout_height="wrap_content"

????????????? android:layout_toRightOf="@id/progress_bar"

????????????? android:text="00:00"

?????? ></TextView>

</RelativeLayout>??

?? <RelativeLayout

???????????????? android:layout_width="wrap_content"

???

??????? ??? android:layout_height="wrap_content"

??? >

??? <ImageButton android:id="@+id/button_play"

??????????? ??????? android:layout_width="wrap_content"

??????????? ??????? android:layout_height="wrap_content"

??????? ??????????? android:layout_marginTop="5px"

??????? ??????????? style="?android:attr/buttonStyleSmall"

??????? ??? ???? ????android:src="@drawable/button_pause"/>

</RelativeLayout>

</LinearLayout>

30.3? 流 媒 體 類

(3)d代碼如下:

??? public void downloadAudioIncrement(String mediaUrl) throws IOException {

??? URLConnection cn = new URL(mediaUrl).openConnection();??

??????? cn.connect();??

??????? InputStream stream = cn.getInputStream();

??????? if (stream == null) {

??????? Log.e(getClass().getName(), "Unable to create InputStream for

mediaUrl:" + mediaUrl);

??????? }

??????? downloadingMediaFile = new File(context.getCacheDir(),

"downloadingMedia.dat");

??????? if (downloadingMediaFile.exists()) {

??????????? downloadingMediaFile.delete();????????? //如果下載完成則刪除

??????? }

??????? FileOutputStream out = new FileOutputStream(downloadingMediaFile);??

??????? byte buf[] = new byte[16384];

??????? int totalBytesRead = 0, incrementalBytesRead = 0;

??????? do {

??????? int numread = stream.read(buf);??

?????????? ?if (numread <= 0)??

??????????????? break;??

??????????? out.write(buf, 0, numread);

??????????? totalBytesRead += numread;

??????????? incrementalBytesRead += numread;

?????????? totalKbRead = totalBytesRead/1000;//totalKbRead表示已經(jīng)下載的文件大小

????????? testMediaBuffer();

?????????? ? fireDataLoadUpdate();

??????? } while (validateNotInterrupted());??

?????? ????? stream.close();

??????? if (validateNotInterrupted()) {

??? ?????? ? fireDataFullyLoaded();

??????? }

}

??? private boolean validateNotInterrupted() {

??????? if (isInterrupted) {

??????????? if (mediaPlayer != null) {

??????????????? mediaPlayer.pause();

??????????? }

??????????? return false;

??????? } else {

??????????? return true;

??????? }

??? }

?private void? testMediaBuffer() {

??? ??? Runnable updater = new Runnable() {

??? ??????? public void run() {

??? ??????????? if (mediaPlayer == null) {

??? ??????????? ? if ( totalKbRead >= INTIAL_KB_BUFFER) {

??? ??????????? ????? try {

??????? ??????????? ????? startMediaPlayer();

??? ??????????? ????? } catch (Exception e) {

??? ??????????? ????????? Log.e(getClass().getName(), "Error copying

buffered conent.", e);

??? ??????????? ????? }

??? ??????????? ? }

??? ??????????? } else if ( mediaPlayer.getDuration() - mediaPlayer.

getCurrentPosition() <= 1000 ){

??? ??????????? ? transferBufferToMediaPlayer();

??? ??????????? }

??? ??????? }

??? ??? };

??? ??? handler.post(updater);

}

(4)startMediaPlayer()方法用來播放音頻文件,首先調(diào)用moveFile()方法將下載的文件復(fù)制,用mediaPlayer的API播放文件,startPlayProgressUpdater()方法更新進(jìn)度條。代碼如下:

??? private void startMediaPlayer() {

??????? try {??

??????? File bufferedFile = new File(context.getCacheDir(),"playingMedia"

?+ (counter++) + ".dat");

??????? moveFile(downloadingMediaFile,bufferedFile);

??????? Log.e(getClass().getName(),"Buffered File path: " + bufferedFile.

getAbsolutePath());

??????? Log.e(getClass().getName(),"Buffered File length: " + bufferedFile.

length()+"");

??????? mediaPlayer = createMediaPlayer(bufferedFile);

??????? mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

??? ??? mediaPlayer.start();

??? ??? startPlayProgressUpdater();???

??????????? playButton.setEnabled(true);

??????? } catch (IOException e) {

??????? Log.e(getClass().getName(), "Error initializing the MediaPlayer.", e);

??????? }??

}

(5)createMediaPlayer()方法用來得到一個MediaPlayer對象。代碼如下:

private MediaPlayer createMediaPlayer(File mediaFile)

??? throws IOException {

??? MediaPlayer mPlayer = new MediaPlayer();

??? mPlayer.setOnErrorListener(

??????????????? new MediaPlayer.OnErrorListener() {

??????????? ??????? public boolean onError(MediaPlayer mp, int what, int

extra) {

??????????? ??????? Log.e(getClass().getName(), "Error in MediaPlayer: ("

+ what +") with extra (" +extra +")" );

??????????? ??? ???? return false;

??????????? ??????? }

??????????? ??? });

??????? FileInputStream fis = new FileInputStream(mediaFile);

???????

??????? mPlayer.setDataSource(fis.getFD());//此方法返回與流相關(guān)聯(lián)的文件說明符。

??????? mPlayer.prepare();

???????

??????? return mPlayer;

}

(6)transferBufferToMediaPlayer()方法將下載的文件復(fù)制到一個臨時的緩存文件中,暫停音頻播放,如果音頻正在播放或者多媒體的總長度和多媒體已播放的長度之差小于一秒的時候繼續(xù)播放。代碼如下:

??? private void transferBufferToMediaPlayer() {

??? ??? try {

??? ??? boolean wasPlaying = mediaPlayer.isPlaying();

??? ??? int curPosition = mediaPlayer.getCurrentPosition();

??? ???

??? ??? File oldBufferedFile = new File(context.getCacheDir(),

"playingMedia" + counter + ".dat");

??? ??? File bufferedFile = new File(context.getCacheDir(),"playingMedia"

+ (counter++) + ".dat");

?

??? ??? bufferedFile.deleteOnExit();??

??? ??? moveFile(downloadingMediaFile,bufferedFile);

??? ??? mediaPlayer.pause();

?

??????? mediaPlayer = createMediaPlayer(bufferedFile);

??? ??? mediaPlayer.seekTo(curPosition);

??? ???

??? ??? boolean atEndOfFile = mediaPlayer.getDuration() - mediaPlayer.

getCurrentPosition() <= 1000;

??????? if (wasPlaying || atEndOfFile){

??????? ???? mediaPlayer.start();

??????? }

?

??? ??? oldBufferedFile.delete();

??? ???

??? ??? }catch (Exception e) {

??? ??? Log.e(getClass().getName(), "Error updating to newly loaded

content.", e);??????????? ???????

??????? }

}

(7)fireDataLoadUpdate()方法開啟一個線程根據(jù)已下載的文件大小更新進(jìn)度條。代碼如下:

private void fireDataLoadUpdate() {

??? Runnable updater = new Runnable() {

??????? public void run() {

??? ??? float loadProgress = ((float)totalKbRead/(float)mediaLengthInKb);

??? ??? progressBar.setSecondaryProgress((int)(loadProgress*100));

??????? }

??? };

??? handler.post(updater);

}

(8)fireDataFullyLoaded()方法開啟一個線程調(diào)用transferBufferToMediaPlayer()方法復(fù)制緩存文件,之后播放,然后刪除下載的臨時文件。代碼如下:

private void fireDataFullyLoaded() {

??? Runnable updater = new Runnable() {

??????? public void run() {

??????? transferBufferToMediaPlayer();

?

??????? downloadingMediaFile.delete();

??????? }

??? };

??? handler.post(updater);

}

(9)startPlayProgressUpdater()方法根據(jù)播放的進(jìn)度更新進(jìn)度條,并且顯示播放的時間進(jìn)度。代碼如下:

??? public void startPlayProgressUpdater() {

??? float progress = (((float)mediaPlayer.getCurrentPosition()/

1000)/mediaLengthInSeconds);

??? progressBar.setProgress((int)(progress*100));

??? int pos=mediaPlayer.getCurrentPosition();

??? int min = (pos/1000)/60;

??????? int sec = (pos/1000)%60;

??????? if(sec<10)

??????????? playTime.setText(""+min+":0"+sec);//把音樂播放的進(jìn)度,轉(zhuǎn)換成時間

??????? else

??????????? playTime.setText(""+min+":"+sec);

???

??????? if (mediaPlayer.isPlaying()) {

??????????? Runnable notification = new Runnable() {

??????? ??????? public void run() {

??????? ??????? startPlayProgressUpdater();

??????????????? }

??????? ??? };

??????? ??? handler.postDelayed(notification,1000);

??? }

}???

(10)moveFile(File????? oldLocation, File newLocation)方法用來實(shí)現(xiàn)文件的復(fù)制。代碼如下:

??? public void moveFile(File?? oldLocation, File?? newLocation)

??? throws IOException {

??????? if ( oldLocation.exists( )) {

??????????? BufferedInputStream? reader = new BufferedInputStream( new

FileInputStream(oldLocation) );

??????????? BufferedOutputStream? writer = new BufferedOutputStream( new

FileOutputStream(newLocation, false));

??????????? try {

??????? ??????? byte[]? buff = new byte[8192];

??????? ??????? int numChars;

??????? ??????? while((numChars = reader.read(buff, 0, buff.length))!= -1) {

??????? ??????? writer.write( buff, 0, numChars );

????? ?????? ????}

??????????? } catch( IOException ex ) {

??????????????? throw new IOException("IOException when transferring " +

oldLocation.getPath() + " to " + newLocation.getPath());

??????????? } finally {

??????????????? try {

??????????????????? if ( reader != null ){

??????????????????? ?? writer.close();

??????????????????????? reader.close();

??????????????????? }

??????????????? } catch( IOException ex ){

??????????????? ??? Log.e(getClass().getName(),"Error closing files when

transferring " + oldLocation.getPath() + " to " + newLocation.getPath() );

??????????????? }

??????????? }

??????? } else {

??????????? throw new IOException("Old location does not exist when

transferring " + oldLocation.getPath() + " to " + newLocation.getPath() );

??????? }

??? }

30.4? 業(yè) 務(wù) 代 碼

代碼如下:

package com.sharpandroid.music.activity;

import java.io.IOException;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.SeekBar;

import android.widget.TextView;

?

import com.sharpandroid.music.R;

import com.sharpandroid.music.StreamingMediaPlayer;

?

public class MediaPlayer extends Activity {

?

??? private Button streamButton;

??? private ImageButton playButton;

??? private boolean isPlaying;

??? private TextView playTime;

??? private StreamingMediaPlayer audioStreamer;

???

??? @Override

??? public void onCreate(Bundle icicle) {

???

??????? super.onCreate(icicle);

?

? ??????setContentView(R.layout.main);

??????? initControls();

??? }

???

??? private void initControls() {

??? playTime=(TextView) findViewById(R.id.playTime);

??????? streamButton = (Button) findViewById(R.id.button_stream);

???????

??????? streamButton.setOnClickListener(new View.OnClickListener() {

??????????? public void onClick(View view) {

??????????????? startStreamingAudio();

??????? }});

?

??????? playButton = (ImageButton) findViewById(R.id.button_play);

??????? playButton.setEnabled(false);

??????? playButton.setOnClickListener(new View.OnClickListener() {

??????????? public void onClick(View view) {

??????????????? if (audioStreamer.getMediaPlayer().isPlaying()) {

??????????????????? audioStreamer.getMediaPlayer().pause();

??????????????????? playButton.setImageResource(R.drawable.button_play);

??????????????? } else {

??????????????????? audioStreamer.getMediaPlayer().start();

??????????????????? audioStreamer.startPlayProgressUpdater();

??????????????????? playButton.setImageResource(R.drawable.button_pause);

??????????????? }

??????????????? isPlaying = !isPlaying;

??????? }});

??? }

???

??? private void startStreamingAudio() {

??? try {

??? ??? final SeekBar progressBar = (SeekBar) findViewById(R.id.

progress_bar);

??? ??? if ( audioStreamer != null) {

??? ??????? audioStreamer.interrupt();

??? ??? }

??? ??? audioStreamer = new StreamingMediaPlayer(this, playButton,

streamButton,? progressBar,playTime);

????? ?????? audioStreamer.startStreaming("http://www.ktcm.net/music/

00.mp3",5208, 216);

??? ??? streamButton.setEnabled(false);

??? } catch (IOException e) {

??? ??? Log.e(getClass().getName(), "Error starting to stream audio.", e);

??? }

??? ????

??? }

}

31? 視頻播放器的開發(fā)?

31.2? 創(chuàng) 建 界 面

在main.xml中實(shí)現(xiàn)播放器的界面,代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? android:background="#FFFFFF"

??? >

<SurfaceView?

??? android:layout_width="fill_parent"

??? android:layout_height="240dip"

??? android:id="@+id/surfaceView"

??? />

??? <LinearLayout

??? android:orientation="horizontal"

??? android:layout_width="fill_parent"

??? android:layout_height="wrap_content"

??? >

??? <ImageButton???

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:src="@drawable/play"

??? ??? android:id="@+id/startbutton"

??? ??? />?? ???

??? <ImageButton???

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"

??? ??? android:src="@drawable/pause"

??? ??? android:id="@+id/pausebutton"

??? ??? />?? ???

??? <ImageButton???

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"

??? ??? android:src="@drawable/reset"

??? ??? android:id="@+id/resetbutton"

??? ??? />?? ???

??? <ImageButton???

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"

??? ??? android:src="@drawable/stop"

??? ??? android:id="@+id/stopbutton"

??? ??? />

??? </LinearLayout>

</LinearLayout>

31.3? 業(yè) 務(wù) 代 碼

做完界面之后,在VideoPlayerActivity中首先要找到SurfaceView,然后調(diào)用getHolder(). setFixedSize()方法設(shè)置分辨率,單擊按鈕的事件的處理和之前我們做過的音樂播放器大致相同,VideoPlayerActivity的代碼如下:

VideoPlayerActivity.java

package com.sharpandroid.activity;

import android.app.Activity;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.util.Log;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.ImageButton;

public class VideoPlayerActivity extends Activity {

??? private SurfaceView surfaceView;

??? private MediaPlayer mediaPlayer;

??? private static final String TAG = "VideoPlayActivity";

???

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? setContentView(R.layout.main);

??????? surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);

???????

??????? ButtonOnclickEnvent envent = new ButtonOnclickEnvent();

??????? ImageButton startbutton = (ImageButton)this.findViewById

(R.id.startbutton);

??????? startbutton.setOnClickListener(envent);

??????? ImageButton pausebutton = (ImageButton)this.findViewById

(R.id.pausebutton);

??????? pausebutton.setOnClickListener(envent);

??????? ImageButton resetbutton = (ImageButton)this.findViewById

(R.id.resetbutton);

??????? resetbutton.setOnClickListener(envent);

??????? ImageButton stopbutton = (ImageButton)this.findViewById

(R.id.stopbutton);

??????? stopbutton.setOnClickListener(envent);

??????? surfaceView.getHolder().setFixedSize(176, 144);? //設(shè)置分辨率

??????? surfaceView.getHolder().setType(SurfaceHolder.SURFACE_

TYPE_PUSH_BUFFERS);

??????? mediaPlayer = new MediaPlayer();

??? }

??? @Override

??? protected void onPause() {

??????? if(mediaPlayer!=null){

??????????? if(mediaPlayer.isPlaying()) mediaPlayer.pause();

??????? }

??????? super.onPause();

??? }

?

??? @Override

??? protected void onResume() {

??????? if(mediaPlayer!=null){

??????????? if(!mediaPlayer.isPlaying()) mediaPlayer.start();

??????? }

??????? super.onResume();

??? }

??? @Override

??? protected void onDestroy() {

??????? if(mediaPlayer!=null){

??????????? if(mediaPlayer.isPlaying()) mediaPlayer.stop();

??????? ??? mediaPlayer.release();

??????? }

??????? super.onDestroy();

??? }

?

??? private class ButtonOnclickEnvent implements View.OnClickListener{

??????? @Override

??????? public void onClick(View v) {

??????????? try {

??????????????? switch (v.getId()) {

??????????????? case R.id.startbutton://播放

??????????????????? mediaPlayer.reset();//重設(shè)

??????????????????? mediaPlayer.setAudioStreamType(AudioManager.

STREAM_MUSIC);

??????????????????? /* 設(shè)置Video影片以SurfaceHolder播放 */

??????????????????? mediaPlayer.setDisplay(surfaceView.getHolder());

??????????????????? mediaPlayer.setDataSource("/sdcard/aa.3gp");

??????????????????? mediaPlayer.prepare();//緩沖????????????

??????????????????? mediaPlayer.start();//播放

??????????????????? break;

?

??????????????? case R.id.pausebutton://暫停

??????????????????? if(mediaPlayer.isPlaying()){//正在播放就暫停

??????????????????????? mediaPlayer.pause();

??????????????????? }else{//否則就播放

??????????????????????? mediaPlayer.start();

??????????????????? }

??????????????????? break;

??????????????? case R.id.resetbutton://重播

??????????????????? mediaPlayer.seekTo(0);

??????????????????? break;

???????????????????

??????????????? case R.id.stopbutton://停止

??????????????????? if(mediaPlayer.isPlaying()){

??????????????????????? mediaPlayer.stop();

??????????????????? }

??????????????????? break;

??????????????? }

??????????? } catch (Exception e) {

??????????????? Log.e(TAG, e.toString());

??????????? }??????????

??????? }??? ??? ???

??? }

???

}

32? 手機(jī)拍照的開發(fā)?

32.2? 創(chuàng) 建 界 面

代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? >

?<SurfaceView android:id="@+id/preview_view"

?????????????? android:layout_width="fill_parent"

?????????????? android:layout_height="fill_parent"

?????????????? android:layout_centerInParent="true"/>

</LinearLayout>

32.3? 業(yè) 務(wù) 代 碼

代碼如下:

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.KeyEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.Window;

import android.view.WindowManager;

public class CameraActivity extends Activity {

??? private static final String TAG = "CameraActivity";

??? private CameraManager cameraManager;

??? private SurfaceView surfaceView;

??? private boolean hasSurface;//SurfaceView是否已經(jīng)創(chuàng)建完成

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? Window window = getWindow();

??????? requestWindowFeature(Window.FEATURE_NO_TITLE);

??????? window.setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,

??????? ????????????? WindowManager.LayoutParams. FLAG_FULLSCREEN);

??????? window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_

SCREEN_ON);//高亮

??????? setContentView(R.layout.main);

??????? cameraManager = new CameraManager(this);

??????? surfaceView = (SurfaceView) findViewById(R.id.preview_view);

??? }

33? 手機(jī)錄音的開發(fā)?

33.2? 創(chuàng) 建 界 面

在main.xml中,加入三個按鈕控件,代碼如下:

Main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? >

??? <TextView

?????? android:layout_width="fill_parent"

?????? android:layout_height="wrap_content"

?????? android:id="@+id/status"

????

?? />

??? <LinearLayout

??? android:orientation="horizontal"

??? ??? android:layout_width="fill_parent"

??? ??? android:layout_height="wrap_content"

??? ??? >

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:text="@string/record"

??? ??? android:id="@+id/recordbutton"

??? ??? />

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"??? ????

??? ??? android:text="@string/stop"

??? ??? android:id="@+id/stopbutton"

??? ??? />

??? ??? <Button

??? ??? android:layout_width="wrap_content"

??? ??? android:layout_height="wrap_content"

??? ??? android:layout_marginLeft="10dip"???

??? ??? android:text="@string/play"

??? ??? android:id="@+id/playbutton"

??? ??? />

??? ?</LinearLayout>

</LinearLayout>

在AndroidManifest.xml文件中加入,讀寫SD卡的權(quán)限和錄音權(quán)限,代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

????? package="com.sharpandroid.redcorder"

????? android:versionCode="1"

????? android:versionName="1.0">

??? <application android:icon="@drawable/icon" android:label="@string/

app_name">

??????? <activity android:name=".RecorderActivity"

????????????????? android:label="@string/app_name">

??????????? <intent-filter>

??????????????? <action android:name="android.intent.action.MAIN" />

??????????????? <category android:name="android.intent.category.LAUNCHER" />

??????????? </intent-filter>

??????? </activity>

?

??? </application>

??? <uses-sdk android:minSdkVersion="7" />

???

???? <uses-permission android:name="android.permission.RECORD_AUDIO"/>

??? <uses-permission android:name="android.permission.WRITE_EXTERNAL_

STORAGE"></uses-permission>

??? <uses-permission android:name="android.permission.MOUNT_UNMOUNT_

FILESYSTEMS"></uses-permission>

??

</manifest>

33.3? 業(yè) 務(wù) 代 碼

代碼如下:

package com.sharpandroid.redcorder;

import java.io.File;

import java.io.IOException;

import android.app.Activity;

import android.content.Intent;

import android.media.MediaPlayer;

import android.media.MediaRecorder;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

public class RecorderActivity extends Activity {

??? private MediaRecorder mediaRecorder;

??? private TextView title;

??? private Button recordButton;

??? private Button stopButton;

??? private Button playButton;

??? private File mediaFile;

??? private boolean sd;

??? private File dir;

??? private boolean isRecord;

??? public MediaPlayer recordPlayer=new MediaPlayer();

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? setContentView(R.layout.main);

??????

??????? Button recordButton=(Button)findViewById(R.id.recordbutton);

??????? Button stopButton=(Button)findViewById(R.id.stopbutton);

??????? Button playButton=(Button)findViewById(R.id.playbutton);

??????? title=(TextView)findViewById(R.id.status);

??????? recordButton.setOnClickListener(listener);

??????? stopButton.setOnClickListener(listener);

??????? playButton.setOnClickListener(listener);

??????? title.setText("開始錄音。。。");

???????

??????? /* 判斷是否存在SD卡 */

??????? sd = Environment.getExternalStorageState().equals(

??????????? android.os.Environment.MEDIA_MOUNTED);

? ??????

?

??????? if (sd)???????? //如果SD卡存在的話,得到音頻文件要保存的路徑

????????? dir = Environment.getExternalStorageDirectory();

???????

??? }

???

??? //處理多個按鈕的單擊事件

??? private View.OnClickListener listener=new View.OnClickListener() {

??????? @Override

??????? public void onClick(View v) {

??????????? Button button=(Button)v;

??????????? switch(button.getId())

??????????? {

??????????? case R.id.recordbutton:?? //錄音

??????????????? ?if(!sd){

??????????????????? ?Toast.makeText(RecorderActivity.this, "SD卡沒有準(zhǔn)備好",

2).show();

??????????????? ?}

??????????????? ?try {

??????????????????? mediaFile = File.createTempFile("newFile", ".amr",

??????????? ??????????????? ?dir);? //在SD卡上創(chuàng)建音頻文件

??????????????????? ?mediaRecorder = new MediaRecorder();

??????????????????? ?????????????????????? /* 設(shè)置錄音來源為麥克風(fēng) */

??????????????????? ?mediaRecorder

??????????????????? ????????? .setAudioSource(MediaRecorder.

AudioSource.MIC);

??????????????????? ?mediaRecorder

??????????????????? ????????? .setOutputFormat(MediaRecorder.

OutputFormat.DEFAULT);

??????????????????? ?mediaRecorder

??????????????????? ????????? .setAudioEncoder(MediaRecorder.

AudioEncoder.DEFAULT);

??????????????????? ?mediaRecorder.setOutputFile(mediaFile

??????????????????? ????????? .getAbsolutePath());

??????????????????? ?mediaRecorder.prepare();

??????????????????? ?mediaRecorder.start();

??????????????? }catch (IOException e) {

??????????????????? e.printStackTrace();

??????????????? }

??????????? ????????? title.setText("錄音中");

??????????? ????????? isRecord=true;

??????????? ????????? break;

??????????? case R.id.stopbutton:

??????? ?????????????????????? /* 停止錄音 */

??????????????? if(mediaFile!=null)

??????????????? {

??????????????? ? mediaRecorder.stop();

??????????????? ? mediaRecorder.release();

??????????????? ? mediaRecorder = null;

??????????????? ? title.setText("停止:" + mediaFile.getName());

??????? ????????? isRecord = false;

??????? ????????? }

??????????? ?????? break;

??????????? case R.id.playbutton:

???????????

??????????????? if(mediaFile!=null&&isRecord==false)

??????????????? {

??????????? ????????? /* 打開播放的程序 */

??????????? ????????? openFile(mediaFile);

??????????????? }??

??????????? }

???????????

??????? }

??? };

???

??? ? private void openFile(File f) //根據(jù)相應(yīng)的文件類型,打開相應(yīng)的程序

??? ? {

??? ??? Intent intent = new Intent();

??? ??? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

??? ??? intent.setAction(android.content.Intent.ACTION_VIEW);

??? ??? String type = getFileType(f);

??? ??? intent.setDataAndType(Uri.fromFile(f), type);

??? ??? startActivity(intent);

??? ? }

?

??? ? private String getFileType(File f) //判斷要打開的文件類型

??? ? {

??? ??? String end = f.getName().substring(

??? ??????? f.getName().lastIndexOf(".") + 1, f.getName().length())

??? ??????? .toLowerCase();

??? ? ??String type = "";

??? ??? if (end.equals("mp3") || end.equals("aac") || end.equals("aac")

??? ??????? || end.equals("amr") || end.equals("mpeg")

??? ??????? || end.equals("mp4"))

??? ??? {

??? ????? type = "audio";

??? ??? }

??? ??? else if (end.equals("jpg") || end.equals("gif")

??? ??????? || end.equals("png") || end.equals("jpeg"))

??? ??? {

??? ????? type = "image";

??? ??? }

??? ??? else

??? ??? {

??? ????? type = "*";

??? ??? }

??? ??? type += "/*";

??? ??? return type;

??? ? }

???

}

34? 手機(jī)鬧鐘的開發(fā)?

34.2? 創(chuàng) 建 界 面

在main.xml文件中加入兩個按鈕,用來實(shí)現(xiàn)鬧鐘和取消鬧鐘,代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? >

<RelativeLayout

??? android:layout_width="fill_parent"

??? android:layout_height="wrap_content"

>?

? <Button

??? android:layout_width="wrap_content"

??? android:layout_height="wrap_content"

??? android:text="定制鬧鐘"

? ??android:id="@+id/okButton"

? ></Button>

??? <Button

??? android:layout_width="wrap_content"

??? android:layout_height="wrap_content"

??? android:layout_toRightOf="@id/okButton"

??? android:layout_alignTop="@id/okButton"

??? android:text="取消鬧鐘"

??? android:id="@+id/cannelButton"

? ></Button>

</RelativeLayout>

</LinearLayout>

34.3? 業(yè) 務(wù) 代 碼

代碼如下:

TimeReceive.java

package com.sharpandroid.time;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

public class TimeReceive extends BroadcastReceiver {

??? ? @Override

??? ? public void onReceive(Context context, Intent intent)

??? ? {

??????? ? Toast.makeText(context, "你的時間到了!!!", 3).show();

??? ? }

}

因?yàn)槭褂昧薆roadcastReceiver服務(wù),因此在AndroidManifest.xml文件中要加入聲明,代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

????? package="com.sharpandroid.time"

????? android:versionCode="1"

????? android:versionName="1.0">

??? <application android:icon="@drawable/icon" android:label="@string/

app_name">

??? <receiver android:name="TimeReceive" android:process=":remote" />

??????? <activity android:name=".TimeActivity"

????????????????? android:label="@string/app_name">

??????????? <intent-filter>

???????????? ???<action android:name="android.intent.action.MAIN" />

??????????????? <category android:name="android.intent.category.LAUNCHER" />

??????????? </intent-filter>

??????? </activity>

?

??? </application>

??? <uses-sdk android:minSdkVersion="7" />

</manifest>

主要代碼如下:

TimeActivity.java

package com.sharpandroid.time;

import java.util.Calendar;

import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.app.TimePickerDialog;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TimePicker;

import android.widget.Toast;

public class TimeActivity extends Activity {

??? /** Called when the activity is first created. */

???

??? private Calendar calendar=Calendar.getInstance();

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? setContentView(R.layout.main);

??????? Button ok=(Button)findViewById(R.id.okButton);

??????? Button cannel=(Button)findViewById(R.id.cannelButton);

??????? //設(shè)置鬧鐘

??????? ok.setOnClickListener(new View.OnClickListener() {

???????????

??????????? @Override

??????????? public void onClick(View v) {

???????????????

???????????????

??????????????? /* 取得系統(tǒng)時間作為TimePickerDialog的默認(rèn)值 */

??????????????? calendar.setTimeInMillis(System.currentTimeMillis());

??????????????? int mHour=calendar.get(Calendar.HOUR_OF_DAY);

??????????????? int mMinute=calendar.get(Calendar.MINUTE);

??????????????? new TimePickerDialog(TimeActivity.this,

??????????????????????? new TimePickerDialog.OnTimeSetListener() {

??????????????????????????? @Override

??????????????????????????? public void onTimeSet(TimePicker view, int

hourOfDay, int minute) {

??????????????????????????????? // TODO Auto-generated method stub

??????????????????????????????? ?/* 取得設(shè)置后的時間 */

??????????????????????????????? calendar.setTimeInMillis(System.

currentTimeMillis());

??????????????????????????????? calendar.set(Calendar.HOUR_OF_DAY,

hourOfDay);

??????????????????????????????? calendar.set(Calendar.MINUTE,minute);

??????????????????????????????? calendar.set(Calendar.SECOND,0);

??????????????????????????????? calendar.set(Calendar.MILLISECOND,0);

??????????????????? ?????????????

??????????????????? ????????????? /* 指定鬧鐘設(shè)置時間到時提示 */

??????????????????? ????????????? Intent intent = new Intent(TimeActivity.

this, TimeReceive.class);

??????????????????? ????????????? /* 創(chuàng)建PendingIntent */

??????????????????? ????????????? PendingIntent sender=PendingIntent.

getBroadcast(

??????????????????? ??????????? ????? ??TimeActivity.this,0, intent, 0);

??????????????????? ????????????? /*

??????????????????? ?????????????? * 以set()設(shè)置的PendingIntent只會運(yùn)行一次

??????????????????? ?????????????? * */

??????????????????? ????????????? AlarmManager am;

??????????????????? ????????????? am = (AlarmManager)getSystemService

(ALARM_SERVICE);

??????????????????? ????????????? am.set(AlarmManager.RTC_WAKEUP,

??????????????????? ??????????? ????? ??calendar.getTimeInMillis(),

??????????????????? ???????????????????? sender

??????????????????? ??????????????????? );

??????????????????? ????????????? /* 更新顯示的設(shè)置鬧鐘時間 */

??????????????????? ????????????? String tmpS=format(hourOfDay)+":

"+format(minute);

??????????????????? ????????????? /* 以Toast提示設(shè)置已完成 */

??????????????????? ????????????? Toast.makeText(TimeActivity.this,"設(shè)置鬧

鐘時間為"+tmpS,

??????????????????? ??????????????? Toast.LENGTH_SHORT)

??????????????????? ??????????????? .show();

?

???????????????????????????????

??????????????????????????? }

??????????????????????? }

??????????????????????? , mHour, mMinute, true).show();

??????????? }

??????? });

????????? //取消鬧鈴

??????? cannel.setOnClickListener(new View.OnClickListener() {

???????????

??????????? @Override

??????????? public void onClick(View v) {

??????????????? Intent intent=new Intent(TimeActivity.this,

TimeReceive.class);

??????????????? PendingIntent pendingIntent=PendingIntent.getBroadcast

(TimeActivity.this, 0, intent, 0);

??????????????? AlarmManager am;??? //取消鬧鐘管理

??????????????? am=(AlarmManager)getSystemService(ALARM_SERVICE);

??????????????? am.cancel(pendingIntent);

??????????????? ?Toast.makeText(TimeActivity.this,"鬧鐘已取消!",3)

??????????? ??????????????? .show();

??????????? }

??????? });

??? }

???

??? /* 日期時間顯示兩位數(shù)的方法 */

??? private String format(int x)

??? {

????? String s=""+x;

????? if(s.length()==1) s="0"+s;

????? return s;

??? }

}

35? 手勢識別的開發(fā)?

35.2? Android手勢識別

35.2.2? 創(chuàng)建應(yīng)用

Layout的代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"?

??? android:orientation="vertical"?

??? android:layout_width="fill_parent"?

??? android:layout_height="fill_parent"?

??? >?

<TextView???

??? android:layout_width="fill_parent"??

??? android:layout_height="wrap_content"??

??? android:text="@string/hello"?

??? />?

<android.gesture.GestureOverlayView?

??? android:id="@+id/ usergestures"

??? android:layout_width="fill_parent"??

??? android:layout_height="fill_parent"?

??? android:layout_weight="1.0"??

??? />?

</LinearLayout>?

(3)編寫Activity。

進(jìn)行手勢識別,得到最配對的,獲取手勢的名字,并打印。

public class MainActivity extends Activity implements

OnGesturePerformedListener{

???

??? GestureLibrary myLibraries;?

??? @Override

??? public void onCreate(Bundle savedInstanceState) {

??????? super.onCreate(savedInstanceState);

??????? setContentView(R.layout.main);

??????? //獲取Layout中的手勢圖層

??????? GestureOverlayView gestures = (GestureOverlayView)

findViewById(R.id.usergestures);?

??????? //設(shè)置對該圖層的監(jiān)聽

??????? gestures.addOnGesturePerformedListener(this);?

??????? //加載已經(jīng)創(chuàng)建的手勢文件

??????? myLibraries = GestureLibraries.fromRawResource(this,

R.raw.gestures);?

??????? //檢測手勢文件讀取是否正確

??????? if (!myLibraries.load()) {?

??????????? Toast.makeText(MainActivity.this, "讀取手勢文件出錯,程序不能正常運(yùn)

行。", Toast.LENGTH_LONG);

??????? }?

??? }

??? //傳入圖層與用戶的手勢

??? public void onGesturePerformed(GestureOverlayView overlay, Gesture

gesture) {?

??????? //將用戶手勢與已經(jīng)建立好的手勢進(jìn)行比對,將結(jié)果放入List中

??????? ArrayList<Prediction> predictions = myLibraries.recognize

(gesture);?

??????????? //判斷,如果相似,即打印出手勢的名字

if (predictions.size() > 0) {?

??????????? Prediction prediction = (Prediction) predictions.get(0);?

??????????? //這里,用戶可設(shè)置一些自定義的操作,來增加用戶體驗(yàn)

??????????? if (prediction.score > 1.0) {?

??????????????? Toast.makeText(this, prediction.name,

Toast.LENGTH_SHORT).show();?

??????????? }?

??????? }?

??? }?

}

36? 圖片瀏覽的開發(fā)?

36.1? 圖 片 介 紹

36.1.1? 圖片列表

(2)在main.xml中修改Layout。這里需要一個GridView的控件。

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"

??? android:id="@+id/grid_photo"

??? android:layout_width="fill_parent"

??? android:layout_height="fill_parent"

??? android:numColumns="auto_fit"

??? android:verticalSpacing="5dp"

??? android:horizontalSpacing="5dp"

??? android:columnWidth="90dp"

??? android:gravity="center"

/>

(3)編寫Activity。

MainActivity.java

public class MainActivity extends Activity {

??? //將raw的圖片索引值放入數(shù)組中

??? private Integer[] photos = {

??????????? R.drawable.android_01, R.drawable.android_02, R.drawable.

android_03,

??????? …

?R.drawable.android_24

??? };

??? @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//加載在layout中添加的GridView控件

GridView grid_photo = (GridView)findViewById(R.id.grid_photo);

grid_photo.setAdapter(new PhotoAdapter(this));

???????

?????? ?grid_photo.setOnItemClickListener(new OnItemClickListener() {

??????????? @Override

??????????? public void onItemClick(AdapterView<?> parent, View view,

??????????????????? int position, long id) {

Intent intent = new

//通過Intent打開showPhoto,并將相應(yīng)的圖片ID傳遞給它

Intent(MainActivity.this,showPhoto.class);

??????????????? intent.putExtra("position", position);

??????????????? startActivity(intent);

???????????????

??????????? }

??????? });

??? }

??? //繼承了BaseAdapter的方法,對其進(jìn)行封裝,添加相應(yīng)顯示圖片的參數(shù)

??? public class PhotoAdapter extends BaseAdapter {

??????? private Context mContext;

??????? public PhotoAdapter Context c) {

??????????? mContext = c;

??????? }

??????? public int getCount() {

??????????? return photos.length;

??????? }

??????? public Object getItem(int position) {

??????????? return null;

??????? }

??????? public long getItemId(int position) {

?? ?????????return 0;

??????? }

??????? public View getView(int position, View convertView, ViewGroup parent) {

??????????? ImageView imageView;

??????????? if (convertView == null) {?

??????????????? imageView = new ImageView(mContext);

??????????????? //分別代表著左、上、右、下的邊框?qū)挾?/p>

??????????????? imageView.setPadding(4, 4, 4, 4);

??????????????? //圖片的顯示大小的參數(shù)

??????????????? imageView.setLayoutParams(new GridView.LayoutParams(85,

100));

??????????????? //圖片的顯示方式(拉伸、居中、適應(yīng)等類似與windows中壁紙的顯示方式)

?????????????? imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

??????????? } else {

??????????????? imageView = (ImageView) convertView;

??????????? }

??????????? imageView.setImageResource(photos[position]);

??????????? return imageView;

??????? }

??? }

}

36.1.3? 加入手勢

(1)將我們前面提到的android.gesture.GestureOverlayView添加到layout中。

<android.gesture.GestureOverlayView

??? xmlns:android="http://schemas.android.com/apk/res/android"?

??? android:id="@+id/usergestures"

??? android:layout_width="fill_parent"??

??? android:layout_height="fill_parent"?

??? android:layout_weight="1.0"

??? >???????

<ImageSwitcher

?? android:id="@+id/switcher"

?? ………

/>

? </android.gesture.GestureOverlayView>?????????????

(2)在showPhoto.java中添加相應(yīng)的處理代碼。

public class showPhoto extends Activity implements? ViewSwitcher.

ViewFactory,

??? OnGesturePerformedListener{

??????? private Integer[] photos = {

??????????????? R.drawable.android_01, R.drawable.android_02,

R.drawable.android_03,

??????????????? ……

R.drawable.android_24

??????? };

??????? private ImageSwitcher mSwitcher;

??????? //記錄當(dāng)前圖片的索引值

??????? private int currentposition ;

??????? //圖片數(shù)量,這里是事先加載好的可以直接賦值,實(shí)際應(yīng)用中,我們可以在GridView

??????? 頁面

??????? //時,通過adapter中g(shù)etCount()來獲得,然后通過Intent傳遞過來

??????? private static final int MAXSIZE = 23;

??????? GestureLibrary myLibraries;?

???????

??????? public void onCreate(Bundle savedInstanceState) {

??? ??????? super.onCreate(savedInstanceState);

??? ??????? //取消應(yīng)用Title欄的顯示,增加圖片的顯示效果

??? ??????? requestWindowFeature(Window.FEATURE_NO_TITLE);

?

??? ??????? setContentView(R.layout.show_photo);

?

??? ??????? mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);

??? ??????? //在使用mSwitcher之前一定要調(diào)用setFactory方法,它的作用是創(chuàng)建兩個圖

??? ??????? ???層,用以ImageSwitcher過渡時使用

??? ??????? mSwitcher.setFactory(this);

??? ??????? //設(shè)置圖片文件出現(xiàn)的動畫效果

??? ??????? mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,

??? ??????????????? android.R.anim.slide_in_left));

??? ?? ?????//設(shè)置圖片文件消失的動畫效果

??? ????? mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,

??? ??????????????? android.R.anim.slide_out_right));

??? ???????

??? ??????? //獲取Layout中的手勢圖層

??? ??????? GestureOverlayView gestures = (GestureOverlayView)

findViewById(R.id.usergestures);?

??? ??????? //設(shè)置對該圖層的監(jiān)聽

??? ??????? gestures.addOnGesturePerformedListener(this);?

??? ??????? //加載已經(jīng)創(chuàng)建的手勢文件

??? ??????? myLibraries = GestureLibraries.fromRawResource(this,

R.raw.gestures);?

??? ??????? //檢測手勢文件讀取是否正確

??? ??????? if (!myLibraries.load()) {?

??? ??????????? Toast.makeText(showPhoto.this, "讀取手勢文件出錯,程序可能無法

正常運(yùn)行", Toast.LENGTH_LONG).show();

??? ??????? }

??? ??????? //獲得攜帶數(shù)據(jù)的Intent,

??????????? Intent intent = getIntent();

??? ??????? int index = intent.getIntExtra("position", 0);

??? ??????? currentposition = index;

??? ??????? //通過傳遞過來的值,設(shè)定當(dāng)前需要顯示的圖片

??? ??????? mSwitcher.setImageResource(photos[index]);

??? ??? }

???????

??????? //大圖顯示

??? ??? public View makeView() {

??? ??????? ImageView i = new ImageView(this);

??? ??????? i.setBackgroundColor(0xFF000000);

??? ??????? //添加圖片顯示的樣式

??? ??????? i.setScaleType(ImageView.ScaleType.FIT_CENTER);

??? ??????? //設(shè)置圖片全局顯示

??? ??????? i.setLayoutParams(new ImageSwitcher.LayoutParams

(LayoutParams.FILL_PARENT,

??? ??????????????? LayoutParams.FILL_PARENT));

??? ??????? return i;

??? ??? }

??????? public void onGesturePerformed(GestureOverlayView overlay, Gesture

gesture) {?

??? ??????? //將用戶手勢與已經(jīng)建立好的手勢進(jìn)行比對,將結(jié)果放入List中

??????????? ArrayList<Prediction> predictions = myLibraries.recognize

(gesture);?

??? ??????? if (predictions.size() > 0) {?

??? ??????????? Prediction prediction = (Prediction) predictions.get(0);?

??? ??????????? //判斷,如果相似

??? ??????????? if (prediction.score > 1.0) {?

??? ??????????????? if("R".equals(prediction.name)){

??? ??????????????? ? //這里需要做一個判斷,如果是最后一張的話,需要提示用戶

??? ??????????????? ? if(currentposition==MAXSIZE){

??? ??????????????? ????? Toast.makeText(showPhoto.this, "已經(jīng)是最后一張",

Toast.LENGTH_SHORT).show();

??? ??????????????? ? }else{

??????? ??????????????? ? ++currentposition;

??? ??????????????? ? mSwitcher.setImageResource(photos[currentposition]);

??? ??????????????? ? }

??? ??????????????? }

??? ???????????? ???//在第一張的時候,同樣需要做一個頁數(shù)的判斷,如果是第一張,需要

提示用戶

??? ??????????????? if("L".equals(prediction.name)){

??? ??????????????? ? if(currentposition==0){

??? ??????????????? ????? Toast.makeText(showPhoto.this, "已經(jīng)是第一張",

Toast.LENGTH_SHORT).show();

??? ??????????????? ? }else{

??? ??????? ????????? --currentposition;

??? ?????????? ?? mSwitcher.setImageResource(photos[currentposition]);

??? ??????????????? ? }

??? ??????????????? }

??? ??????????? }?

??? ??????? }?

??? ??? }

}

總結(jié)

以上是生活随笔為你收集整理的[转]大话企业级Android应用开发实战 音乐播放器的开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。