問題:
Android 螢幕的直向?橫向?
一、程式碼
Android 螢幕的直向?橫向?
一、程式碼
Activity.java
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //強置為橫向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //強置為直向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //強置為橫向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //強置為直向
package tw.G000E004;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class G000E004Activity extends Activity {
Animation myanimation;
ImageView img;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myanimation=AnimationUtils.loadAnimation(this, R.anim.myanim);
img=(ImageView)findViewById(R.id.img);
img.startAnimation(myanimation);
}
}
android:fillafter="false"
android:fromxscale="0.0"
android:fromyscale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="50%"
android:pivoty="50%"
android:toxscale="1.4"
android:toyscale="1.4">
android:fromxdelta="30"
android:fromydelta="30"
android:toxdelta="0"
android:toydelta="50">
android:fromdegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="50%"
android:pivoty="50%"
android:todegrees="+350">
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:text="@string/hello">
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon">
package tw.test2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;
public class Test2Activity extends Activity {
private Gallery gallery;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery=(Gallery)findViewById(R.id.gallery);
//希宣告一個類別,用來設定圖片資訊
ImageAdapter imageAdapter=new ImageAdapter(this);
//加入圖片
Integer[] mImageIds={R.drawable.icon,R.drawable.ha};
//設定圖片位置
imageAdapter.setmImageIds(mImageIds);
//圖片高度
imageAdapter.setHeight(100);
//圖片寬度
imageAdapter.setWidth(200);
gallery.setAdapter(imageAdapter);
//當按下圖片時,所觸發的事件
gallery.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(Test2Activity.this, "您選的是第"+position+"張圖", Toast.LENGTH_LONG).show();
//換頁
Intent intent = new Intent();
intent.setClass(Test2Activity.this, Report.class);
/*
* 這邊是你要帶過去的資料
*/
intent.putExtra("test", "第二頁囉");
startActivity(intent);
}});
}
}
ImageAdapter.java
package tw.test2;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer width;
private Integer height;
private Integer[] mImageIds;
public ImageAdapter(Context c) {
mContext = c;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
//設定圖片來源
imageView.setImageResource(mImageIds[position]);
//設定圖片的寬、高
imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer[] getmImageIds() {
return mImageIds;
}
public void setmImageIds(Integer[] mImageIds) {
this.mImageIds = mImageIds;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}
Report.java
package tw.test2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class Report extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
Intent intent = this.getIntent();
String showtest=intent.getStringExtra("test");
Toast.makeText(Report.this,showtest.toString(), Toast.LENGTH_LONG).show();
}
}
記得加入(AndroidManifest.xml)
package tw.Recording;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class RecordingActivity extends Activity {
private Button start,stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
start.setOnClickListener(new OnClickListener(){
//執行背景作業
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
startService(intent);
}});
stop.setOnClickListener(new OnClickListener(){
//停止背景作業
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
stopService(intent);
}});
}
}
Service.java
package tw.Recording;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
public class RecordingService extends Service {
private MediaRecorder mediaRecorder = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
//錄音
public void onStart(Intent intent,int startId)
{
//設定錄音檔名
String fileName = "test.amr";
try{
File SDCardpath=Environment.getExternalStorageDirectory();
File myDataPath=new File(SDCardpath.getAbsolutePath()+"/download");
if( !myDataPath.exists() ) myDataPath.mkdirs();
File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName);
mediaRecorder = new MediaRecorder();
//設定音源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//設定輸出檔案的格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
//設定編碼格式
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//設定錄音檔位置
mediaRecorder.setOutputFile(recodeFile.getAbsolutePath());
mediaRecorder.prepare();
//開始錄音
mediaRecorder.start();
}
catch(IOException e){
e.printStackTrace();
}
super.onStart(intent, startId);
}
@Override
//停止背景時
public void onDestroy() {
if(mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
super.onDestroy();
}
}
AndroidManifest.xml(記得加入)