2014年1月12日

LinearLayout

LinearLayout 線性位置版面配置

XML宣告

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>



android:layout_gravity="center"
將LinearLayout中的元件置中對齊 


android:orientation
屬性值為"horizontal"(水平),所以元件的排版方式是以水平排版呈現



RelativeLayout

RelativeLayout 相對位置版面配置


XML宣告
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 </RelativeLayout>


屬性說明
android:layout_width該屬性定義元件的寬度
android:layout_height該屬性定義元件的高度,可使用屬性值同上
android:text該屬性可設定文字顯示在元件上
android:layout_above將此元件置於指定元件上方
android:layout_below將此元件置於指定元件下方
android:layout_toLeftOf將此元件置於指定元件左方
android:layout_toRightOf將此元件置於指定元件右方
android:layout_alignParentTop將此元件對齊於佈局畫面上邊線
android:layout_alignParentRight將此元件對齊於佈局畫面右邊線
android:layout_alignParentLeft將此元件對齊於佈局畫面左邊線
android:layout_alignParentBottom將此元件對齊於佈局畫面底線


程式碼宣告
RelativeLayout layout;
RelativeLayout.LayoutParams params;


layout = new RelativeLayout(this);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);


2014年1月5日

Drawable dpi

Drawable

A>drawable (hdpi,mdpi,ldpi)的區別

dpi(dot per inch),每英寸像素數。
四種密度分類:
ldpi (low), normal size 120
mdpi (medium), normal size 160
hdpi (high), normal size 240
xhdpi (extra high)normal size 320
一般情況下的普通屏幕:ldpi是120,mdpi是160,hdpi是240,xhdpi是320。


B>WVGA,HVGA,QVGA的區別

XGA,1024*768
WVGA,768*480
VGA(Video Graphics Array),640*480。
WVGA(Wide VGA),480*800
HVGA(Half VGA)即VGA的一半分辨率為,320*480
QVGA(Quarter VGA)即VGA非四分之一分辨率,240*320


C>資料夾drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA

drawable-ldpi,存放如QVGA (240×320)
drawable-mdpi,存放如HVGA (320×480)
drawable-hdpi,存放如WVGA (480×800),FWVGA (480×854)
drawable-xhdpi,存放更高解析度圖片

D>Android系統會根據機器的分辨率來分別到這幾個文件夾裏面去找對應的圖片。

Camera

Camera

在 AndroidManifest.xml 中加上 permission、feature

<uses-permission android:name="android.permission.CAMERA">
<uses-feature android:name="android.hardware.camera">
<uses-feature android:name="android.hardware.camera.autofocus">


Flashlight Control Code

public static Camera camera;
public static Parameters parameters;

camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();

parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
camera.release();

ImageButton

ImageButton 自訂義類別

class ImageButtonA extends ImageButton implements OnTouchListener,
OnClickListener, OnFocusChangeListener {
MainActivity mainActivity;
public ImageButtonA(Context context) {
super(context);
mainActivity = (MainActivity) context;
setBackgroundResource(R.drawable.ic_launcher);
  //載入圖片
setOnClickListener(this);
setOnFocusChangeListener(this);
setOnTouchListener(this);
}

public boolean onTouch(View v, MotionEvent event) {
return false;
}

public void onClick(View arg0) {
setBackgroundResource(R.drawable.ic_launcher);
}

public void onFocusChange(View arg0, boolean isFocused) {
// TODO Auto-generated method stub
}
}

2014年1月1日

Bitmap

Bitmap

public Bitmap bollBitmap;
bollBitmap = BitmapFactory.decodeResource(mainActivity.getResources(),
R.drawable.image_0);

STEP 1
將圖檔加入workspace路徑\res\drawable-hdpi下

STEP 2
Refresh Eclipe res檔案



STEP 3
public Bitmap bollBitmap;
bollBitmap = BitmapFactory.decodeResource(mainActivity.getResources(),
R.drawable.image_0);