2013年9月29日 星期日
write & read object to file by Serializable
ex:
public class FitPlan implements Serializable {
public long startDateNumber;
public long endDateNumber;
public float targetWeight;
public float startWeight;
public String name;
public int calorieQuota;
public int bmr;
public float workPercentage;
public String uuid;
public void writeToFile(Context context)
{
File file = new File(context.getFilesDir(), "plan.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static FitPlan createFromFile(Context context)
{
File file = new File(context.getFilesDir(), "plan.txt");
FitPlan plan = null;
try{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
plan = (FitPlan) in.readObject();
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return plan;
}
}
說明
FitPlan的每個欄位也都必須是Serializable,才能寫入檔案。
不想寫入檔案的欄位,可加上transient
2013年9月23日 星期一
取得前一天/後一天的時間
currentDate的類別是Time
void preDay() {
this.currentDate.monthDay -= 1;
this.currentDate.normalize(true);
}
void nextDay() {
this.currentDate.monthDay += 1;
this.currentDate.normalize(true);
}
2013年9月17日 星期二
從Finder打開檔案
1. 設定External Tools Configuration
2. 新增打開Finder的指令
輸入Location : /usr/bin/open
Arguments: ${container_loc}
3. 點選檔案
4. 點選finder
2013年9月11日 星期三
取得screen的大小
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
2013年9月10日 星期二
設定action bar的title color
ex
<style name="MyTitleTextStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">#000000</item>
</style>
<style name="MyActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">@android:color/transparent</item>
<item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
</style>
<style name="FitMiTheme" parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:actionMenuTextColor">#0000ff</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyTitleTextStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">#000000</item>
</style>
<style name="MyActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">@android:color/transparent</item>
<item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
</style>
<style name="FitMiTheme" parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:actionMenuTextColor">#0000ff</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
設定action bar上button icon
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:showAsAction="always"
android:icon="@drawable/setting"/>
</menu>
移除action bar divider
ex:
<style name="MyActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">@android:color/transparent</item>
</style>
<style name="FitMiTheme" parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:actionMenuTextColor">#0000ff</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
2013年9月9日 星期一
write bitmap to file
ex:
File imageFile;
Bitmap bitmap;
FileOutputStream out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
save file in external storage
1. 設定權限 in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.passionbean.fitmi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
2. 建立file
File albumDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
),
"FitMi"
);
if(albumDir.exists() == false)
{
albumDir.mkdir();
}
File imageFile = new File(albumDir.getPath(), "test.jpg");
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.passionbean.fitmi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
2. 建立file
File albumDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
),
"FitMi"
);
if(albumDir.exists() == false)
{
albumDir.mkdir();
}
File imageFile = new File(albumDir.getPath(), "test.jpg");
2013年9月6日 星期五
AlertDialog的使用
ex:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AddFoodActivity.this);
alertDialogBuilder.setTitle("選擇來源");
alertDialogBuilder.setCancelable(true)
.setPositiveButton("拍照",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
}
})
.setNeutralButton("相簿",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, ALBUM_IMAGE_ACTIVITY_REQ );
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
建立list單選的dialog:
final CharSequence[] items = {"靜態活動(大部份時間都坐著)", "輕度活動(每天散步的人)", "中度活動(每天做劇烈運動的人)",
"重度活動(參與每日體育訓練的人)" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("工作活動量")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AddFoodActivity.this);
alertDialogBuilder.setTitle("選擇來源");
alertDialogBuilder.setCancelable(true)
.setPositiveButton("拍照",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
}
})
.setNeutralButton("相簿",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, ALBUM_IMAGE_ACTIVITY_REQ );
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
建立list單選的dialog:
final CharSequence[] items = {"靜態活動(大部份時間都坐著)", "輕度活動(每天散步的人)", "中度活動(每天做劇烈運動的人)",
"重度活動(參與每日體育訓練的人)" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("工作活動量")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
2013年9月3日 星期二
ViewPager的使用
建立頁面UI元件
public Object instantiateItem(View container, int position)
delete頁面UI元件
public void destroyItem(View arg0, int arg1, Object arg2)
更新ViewPager
ex:
ImageAdapter adapter = (ImageAdapter)viewPager.getAdapter();
viewPager.setAdapter (null);
viewPager.setCurrentItem(newIndex);
viewPager.setAdapter (adapter);
2013年9月2日 星期一
dp轉pixel
ex:
int height = (int)TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
當程式或XML裡設定單位為dp時,對應的pixel計算範例:
10 dp:
MDPI : 10 pixel
HDPI: 15 pixel
XHDPI: 20 pixel
UI文字設定 (font)
XML:
ex:
<Button
android:id="@+id/addFoodCategoryBreakfastButton"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="早餐"
android:textColor="#000000"
android:textSize="12sp"
android:background="@drawable/roundcornerforaddfoodcategorybutton"
/>
ex:
<Button
android:id="@+id/addFoodCategoryBreakfastButton"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="早餐"
android:textColor="#000000"
android:textSize="12sp"
android:background="@drawable/roundcornerforaddfoodcategorybutton"
/>
設定圓角
1. 新增設定圓角圖形的XML在res/drawable下
ex: 新增roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#7fc7b299" />
</shape>
ex:
<corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp"
android:topLeftRadius="0dp" android:topRightRadius="0dp"/>
說明:
solid欄位設定圖形的背景顏色
2. 在layout的xml指定background為roundcorner
ex:
<View
android:id="@+id/addFoodCategoryBackground"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/addFoodDateTitleImageView"
android:layout_toRightOf="@+id/addFoodPhotoButton"
android:background="@drawable/roundCorner"
/>
ex: 新增roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#7fc7b299" />
</shape>
ex:
<corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp"
android:topLeftRadius="0dp" android:topRightRadius="0dp"/>
說明:
solid欄位設定圖形的背景顏色
2. 在layout的xml指定background為roundcorner
ex:
<View
android:id="@+id/addFoodCategoryBackground"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/addFoodDateTitleImageView"
android:layout_toRightOf="@+id/addFoodPhotoButton"
android:background="@drawable/roundCorner"
/>
訂閱:
文章 (Atom)