2011年7月20日星期三

動態改變文字的字體, setTypeface(typeface, style)

我們可以使用 Java 碼, 調用 setTypeface() 方法, 改變文字的字體.

textview.setTypeface(typeface, style)

其中 typeface 參數可以是:
- Typeface.DEFAULT
- Typeface.DEFAULT_BOLD
- Typeface.MONOSPACE
- Typeface.SANS_SERIF
- Typeface.SERIF

style 參數可以是:
- Typeface.BOLD
- Typeface.BOLD_ITALIC
- Typeface.ITALIC
- Typeface.NORMAL

實例:

動態改變文字的字體, setTypeface(typeface, style)

package com.AndroidText;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidTextActivity extends Activity {

TextView text;
Button buttonChangeTypeFace;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
buttonChangeTypeFace = (Button)findViewById(R.id.changetypeface);

buttonChangeTypeFace.setOnClickListener(new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub

/*
* setTypeface(typeface, style)
* typeface:
* - Typeface.DEFAULT
* - Typeface.DEFAULT_BOLD
* - Typeface.MONOSPACE
* - Typeface.SANS_SERIF
* - Typeface.SERIF
*
* style:
* - Typeface.BOLD
* - Typeface.BOLD_ITALIC
* - Typeface.ITALIC
* - Typeface.NORMAL
*/


//setTypeface for TextView
text.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
//setTypeface for Button
buttonChangeTypeFace.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
}});
}
}


<?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:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/changetypeface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Typeface"
/>
</LinearLayout>


2011年7月19日星期二

動態改變文字的大小, setTextSize()

實例:

動態改變文字的大小, setTextSize()

package com.AndroidText;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidTextActivity extends Activity {

TextView text;
Button buttonChangeTextSize;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
buttonChangeTextSize = (Button)findViewById(R.id.changetextsize);

buttonChangeTextSize.setOnClickListener(new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
//Change Text Size of TextView
text.setTextSize(50);
//Change Text Size of Button
buttonChangeTextSize.setTextSize(35);
}});
}
}


<?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:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/changetextsize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Size"
/>
</LinearLayout>


使用 Java 代碼動態改變按鈕的大小

我們可以使用 Java 碼, 通過改變佈局參數(LayoutParams)來改變按鈕的大小.

實例:

按鈕原來的大小
動態改變後的按鈕大小

package com.AndroidButtonSize;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class AndroidButtonSizeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
LayoutParams params = button.getLayoutParams();
params.width = 300;
params.height = 200;
button.setLayoutParams(params);
}});
}
}


2011年7月16日星期六

谷歌發佈 Android 3.2 SDK 工具

谷歌剛剛發佈新的 Android 3.2 SDK 工具, 為用戶和開發人員添加了一些新功能. 新平台 API Level 13 包括一些API的變化。
  • 更廣泛的平板電腦優化
  • 固定大小應用程序的 zoom 的兼容性
  • SD 卡的媒體同步
  • 支持擴展屏幕的API


谷歌還同時更新了 SDK Tools (r12), Eclipse 插件 (ADT 12) 以及 NDK (r6).

詳情:
http://android-developers.blogspot.com/2011/07/android-32-platform-and-updated-sdk.html

2011年7月15日星期五

通過EXTRA_OUTPUT, 指定並顯示使用 ACTION_IMAGE_CAPTURE 拍攝的圖像

在本博客的舊文章"通過EXTRA_OUTPUT, 指定儲存圖像的路徑"中, 描述了如何指定儲存圖像的路徑, 但沒有描述如何顯示拍攝的圖像. 本文將描述如何在onActivityResult()通過 BitmapFactory 顯示拍攝的圖像.

通過EXTRA_OUTPUT, 指定並顯示使用 ACTION_IMAGE_CAPTURE 拍攝的圖像

package com.AndroidImageCapture;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidImageCaptureActivity extends Activity {

ImageView imageiewImageCaptured;
String strImage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonImageCapture = (Button)findViewById(R.id.captureimage);
imageiewImageCaptured = (ImageView)findViewById(R.id.imagecaptured);

buttonImageCapture.setOnClickListener(buttonImageCaptureOnClickListener);
}

Button.OnClickListener buttonImageCaptureOnClickListener
= new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
//Specify the path of the captured image
strImage = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypicture.jpg";
File myImage = new File(strImage);
Uri uriMyImage = Uri.fromFile(myImage);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uriMyImage);
startActivityForResult(intent, 0);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Bitmap bmp = BitmapFactory.decodeFile(strImage);
imageiewImageCaptured.setImageBitmap(bmp);
}
}
}


<?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/captureimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call for ACTION_IMAGE_CAPTURE"
/>
<ImageView
android:id="@+id/imagecaptured"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


2011年7月13日星期三

如何鎖定屏幕方向, setRequestedOrientation

我們可以調用 getResources().getConfiguration().orientation 獲得當前的屏幕方向, 然後調用 setRequestedOrientation() 方法鎖定屏幕方向.

例子:

鎖定屏幕方向

package com.AndroidFixOrientation;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AndroidFixOrientationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

CheckBox fixOrientation = (CheckBox)findViewById(R.id.FixOrientation);

fixOrientation.setOnCheckedChangeListener(new OnCheckedChangeListener(){

int currentOrientation = getResources().getConfiguration().orientation;

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1){
switch(currentOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}});


}
}


2011年7月5日星期二

檢索字符串資源, getString()

有時,我們將定義我們的字符串形式的資源

我們會使用資源(resources)的方式定義我們的字符串; 例如 /res/values/strings.xml 中的 "app_name".

如果我們要檢索這樣的字符串資源, 不能直接使用 myAppName = R.string.app_name, 我們要通過 getString() 方法檢索字符串資源.

例子:

檢索字符串資源, getString()

package com.StringResource;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class StringResourceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView text1 = (TextView)findViewById(R.id.text1);
TextView text2 = (TextView)findViewById(R.id.text2);

text1.setText(R.string.app_name);

String myAppName;
//myAppName = R.string.app_name; //Error!
myAppName = getString(R.string.app_name);
text2.setText(myAppName);
}
}