2011年10月12日星期三
2011年10月2日星期日
從 SD Card 中讀取圖像檔案

package com.AndroidLoadFromSD;
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class AndroidLoadFromSD extends Activity {
ImageView Image;
Button LoadButton;
Bitmap bmImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Image = (ImageView)findViewById(R.id.image);
LoadButton = (Button)findViewById(R.id.load);
LoadButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String extStorage = Environment.getExternalStorageDirectory().toString();
String file = new File(extStorage, "myFile.PNG").toString();
Bitmap bm = BitmapFactory.decodeFile(file);
Image.setImageBitmap(bm);
Toast.makeText(AndroidLoadFromSD.this,
extStorage+"/myFile.PNG",
Toast.LENGTH_LONG).show();
}});
}
}
<?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/hello"
/>
<Button
android:id="@+id/load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image (myFile.PNG)"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
相關文章:
- 儲存圖像到 SD Card 中
2011年10月1日星期六
儲存圖像到 SD Card 中

package com.AndroidSaveOnSD;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class AndroidSaveOnSD extends Activity {
ImageView Image;
Button SaveButton;
Bitmap bmImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Image = (ImageView)findViewById(R.id.image);
SaveButton = (Button)findViewById(R.id.save);
bmImage = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Image.setImageBitmap(bmImage);
SaveButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String extStorage = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorage, "myFile.PNG");
try {
OutputStream outStream = new FileOutputStream(file);
bmImage.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(AndroidSaveOnSD.this,
extStorage+"/myFile.PNG",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidSaveOnSD.this,
e.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidSaveOnSD.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
}});
}
}
<?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/hello"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Image (myFile.PNG)"
/>
</LinearLayout>
注意須修改 AndroidManifest.xml 添加 "android.permission.WRITE_EXTERNAL_STORAGE" 權限.
相關文章:
- 從 SD Card 中讀取圖像檔案
2011年9月23日星期五
接近傳感器 (Proximity Sensor)
通過接近傳感器 (Proximity Sensor)可以檢測到 Android 手機靠近我的臉, 進而做一些動作, 例如禁用觸摸功能,或關閉屏幕.
public class AndroidProximitySensorActivity extends Activity {
/** Called when the activity is first created. */
SensorManager mySensorManager;
Sensor myProximitySensor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myProximitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (myProximitySensor == null){
//No Proximity Sensor!
}else{
mySensorManager.registerListener(proximitySensorEventListener, myProximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
}
}
SensorEventListener proximitySensorEventListener = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
//do something
}
}
};
}
2011年9月19日星期一
顯示標題進度條, setProgressBarIndeterminateVisibility()
要在標題欄顯示進度條, 首先在 setContentView() 前調用 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 函數要求FEATURE_INDETERMINATE_PROGRESS 功能. 再通過調用 setProgressBarIndeterminateVisibility(true/false) 函數顯示/隱藏標題進度條.


package com.AndroidProgressTitle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class AndroidProgressTitleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
Button showProgress = (Button)findViewById(R.id.showprogress);
Button hideProgress = (Button)findViewById(R.id.hideprogress);
showProgress.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setProgressBarIndeterminateVisibility(true);
}});
hideProgress.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setProgressBarIndeterminateVisibility(false);
}});
}
}
<?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/hello"
/>
<Button
android:id="@+id/showprogress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="顯示標題進度條"
/>
<Button
android:id="@+id/hideprogress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="隱藏標題進度條"
/>
</LinearLayout>
2011年9月10日星期六
通過 XML 在文本視圖 (TextView) 設定陰影效果 (shadow)

<?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/hello"
android:shadowColor="#FFFFFF"
android:shadowRadius="5"
android:shadowDx="10"
android:shadowDy="10"
/>
</LinearLayout>
2011年9月6日星期二
android:onClick - 定義 onClick 事件回調函數
我們可以在佈局文件(main.xml)中通過 android:onClick 屬性, 直接定義 onClick 事件的回調函數.
實例:
實例:
<?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/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="onClick"
/>
</LinearLayout>
package com.AndroidOnClick;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class AndroidOnClickActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view){
Toast.makeText(AndroidOnClickActivity.this,
"Button Clicked", Toast.LENGTH_LONG).show();
}
}
訂閱:
文章 (Atom)