id="@+id/seekbar01"
android:
layout_width="fill_parent"
android:
layout_height="20px"
style="?
android:
attr/progressBarStyleHorizontal"
/>
实验三:
图形绘制与OpenGL
【目的】
在屏幕绘制各种图形,了解OpenGL
【要求】
1、了解在屏幕绘图方法
2、了解OpenGL
【原理】
【过程】
1、绘制直线、园、曲线等各种图形
2、显示字符
3、利用OpenGL编程方法
publicvoidonDrawFrame(GL10gl){
//一般的opengl程序,首先要做的就是清屏
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
//紧接着设置模型视图矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();//清空矩阵
GLU.gluLookAt(gl,0,0,3,0,0,0,0,1,0);//视点变换,将相机位置设置为(0,0,3),同时指向(0,0,0)点
//设置模型位置旋转及缩放信息
gl.glTranslatef(0.0f,0.0f,-1.0f);//将模型位置设置为(0,0,-1)
floatangle=30.0f;
gl.glRotatef(angle,0,1,0);//绕模型自身Y轴旋转30度
gl.glRotatef(angle,1,0,0);//绕模型自身X轴旋转30度
gl.glScalef(1.2f,1.2f,1.2f);//设置三方向的缩放系数
//设置颜色
gl.glColor4f(0.0f,0.0f,0.0f,1.0f);
//渲染立方体
mCube.draw(gl,gl.GL_TRIANGLES);
//mCube.draw(gl,gl.GL_LINES);
}
实验四:
网络访问与服务
【目的】
掌握Android网络访问方法
【要求】
1、了解手机WEB网站访问编程
2、通过网络进行数据访问
3、了解数据库使用
【原理】
利用Android网络访问协议
【过程】
1、访问WEB网站,通过HttpResponse类,读入网络数据。
HttpClientclient=newDefaultHttpClient();
HttpGetget=newHttpGet(url);
HttpResponseresponse=client.execute(get);
HttpEntityentity=response.getEntity();
//尝试读取entity的长度,返回-1表示长度未知
longlength=entity.getContentLength();
InputStreamis=entity.getContent();
Strings=null;
if(is!
=null){
ByteArrayOutputStreambaos=newByteArrayOutputStream();
byte[]buf=newbyte[512];
intch=-1;
intcount=0;
while((ch=is.read(buf))!
=-1){
baos.write(buf,0,ch);
count+=ch;
//如果长度已知,可以通过taskProgress()通知监听者任务执行的比例
if(length>0){
listener.taskProgress(this,count,length);
}
//为了更好的演示进度,让线程休眠100ms
Thread.sleep(100);
}
Log.e("HttpTask","length="+baos.toByteArray().length);
//返回内容
s=newString(baos.toByteArray());
}
returns;
读入网站数据:
实验五:
硬件访问与传感器
【目的】
通过底层API访问手机硬件及手机上的各种传感器
【要求】
1、获取手机上电话、短信等各种功能的编程
2、了解手机上各种传感器的功能与使用方法
【原理】
利用手机本身的功能与相关传感器的使用
【过程】
1、了解程序使用手机电话功能的方法
短信收发:
packagecom.android.TinySMS;
importandroid.app.Activity;
importandroid.app.PendingIntent;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.telephony.gsm.SmsManager;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassTinySMSextendsActivity{
publicstaticfinalStringSMS_ACTION="com.android.TinySMS.RESULT";
//privateTextViewmessage;
privateButtonsnd;
privateEditTexttel;
privateEditTexttxt;
privateSentReceiverreceiver=newSentReceiver();
privateclassSentReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
if(intent.getAction().equals(SMS_ACTION)){
intcode=getResultCode();
//短消息发送成功
if(code==Activity.RESULT_OK)
Toast.makeText(TinySMS.this,R.string.msg_sent,
Toast.LENGTH_SHORT).show();
}
}
};
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tel=(EditText)findViewById(R.id.EditText01);
tel.setText("5554");//模拟器之间互发短信
txt=(EditText)findViewById(R.id.EditText02);
txt.setText("我用自己的程序试试发短信。
");
snd=(Button)findViewById(R.id.Button01);
snd.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewarg0){
StringphoneNo=tel.getText().toString();
Stringmessage=txt.getText().toString();
if(phoneNo.length()>0&&message.length()>0){
sendSMS(phoneNo,message);
}else{
Toast.makeText(TinySMS.this,
"请重新输入电话号码和短信内容",
Toast.LENGTH_LONG).show();
}
}
});
}
privatevoidsendSMS(Stringaddress,Stringcontent)
{
SmsManagermanager=SmsManager.getDefault();
Intenti=newIntent(SMS_ACTION);
//生成PendingIntent,当消息发送完成,接收到广播
PendingIntentsentIntent=PendingIntent.getBroadcast(
this,
0,
i,
PendingIntent.FLAG_ONE_SHOT);
manager.sendTextMessage(
address,
null,
content,
sentIntent,
null);
}
}
如果要发短信,还需在AndroidManifest.xml中声明权限:
name="android.permission.READ_SMS">
name="android.permission.SEND_SMS">
2、手机上有多种传感器,可以对这些传感器进行编程。
相机拍摄:
packagecom.android.cameraAndroid;
importandroid.app.Activity;
importandroid.os.Bundle;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.graphics.PixelFormat;
importandroid.hardware.Camera;
importandroid.hardware.Camera.Parameters;
importandroid.hardware.Camera.PictureCallback;
importandroid.hardware.Camera.ShutterCallback;
importandroid.media.AudioManager;
importandroid.media.ToneGenerator;
import.Uri;
importandroid.os.Environment;
importandroid.os.StatFs;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
publicclassCameraAndroidextendsActivity{
privateCameraPreviewpreview;
privateCameracamera;
privateToneGeneratortone;
privatestaticfinalintOPTION_SNAPSHOT=0;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
preview=newCameraPreview(this);
setContentView(preview);
}
@Override
publicbooleanonOptionsItemSelected(MenuItemitem){
intitemId=item.getItemId();
switch(itemId){
caseOPTION_SNAPSHOT:
//拍摄照片
camera.takePicture(shutterCallback,null,jpegCallback);
break;
}
returntrue;
}
//返回照片的JPEG格式的数据
privatePictureCallbackjpegCallback=newPictureCallback(){
publicvoidonPictureTaken(byte[]data,Cameracamera){
Parametersps=camera.getParameters();
if(ps.getPictureFormat()==PixelFormat.JPEG){
//存储拍照获得的图片
Stringpath=save(data);
//将图片交给Image程序处理
Uriuri=Uri.fromFile(newFile(path));
Intentintent=newIntent();
intent.setAction("android.intent.action.VIEW");
intent.setDataAndType(uri,"image/jpeg");
startActivity(intent);
}
}
};
//快门按下的时候onShutter()被回调
privateShutterCallbackshutterCallback=newShutterCallback(){
publicvoidonShutter(){
if(tone==null)
//发出提示用户的声音
tone=newToneGenerator(AudioManager.STREAM_MUSIC,
ToneGenerator.MAX_VOLUME);
tone.startTone(ToneGenerator.TONE_PROP_BEEP2);
}
};
privateStringsave(byte[]data){
Stringpath="/sdcard/"+System.currentTimeMillis()+".jpg";
try{
//判断SD卡上是否有足够的空间
Stringstorage=Environment.getExternalStorageDirectory().toString();
StatFsfs=newStatFs(storage);
longavailable=fs.getAvailableBlocks()*fs.getBlockSize();
if(available//空间不足直接返回空
returnnull;
}
Filefile=newFile(path);
if(!
file.exists())
//创建文件
file.createNewFile();
FileOutputStreamfos=newFileOutputStream(file);
fos.write(data);
fos.close();
}catch(Exceptione){
e.printStackTrace();
retu