2013年12月31日

DisplayMetrics

DisplayMetrics (Get the phone screen pixel)


DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthPixels = dm.widthPixels;
int heightPixels = dm.heightPixels;

float density = dm.density;
int screenWidth = widthPixels * density ;
int screenHeight = heightPixels * density ;





For example

320*480>
widthPixels is 320, heightPixels is 480, density is 1.0。

480*800>
widthPixels is 320, heightPixels is 533, densityis 1.5。

Android View

自定義view類別
透過class繼承view這個類別

class MainActivityView extends View {
private ShapeDrawable mDrawable;
private Paint mPaint;

public MainActivityView(Context context) {
super(context);
// TODO Auto-generated constructor stub
int height = 100;
int width = 100;
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.setBounds(0, 0, width, height);
mPaint = mDrawable.getPaint();
mPaint.setColor(Color.argb(255, 0, 0, 0));
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
mDrawable.draw(canvas);
}

}

2013年12月22日

SharedPreferences Code

SharedPreferences

可以使用SharedPreferences保存數據:
boolean,float,int,long,string
這些數據將保持不變,即使應用程序被終止。

記錄
SharedPreferences.Editor editor = spref.edit();
editor.clear();
editor.putInt("KEY_FLAGA", ImageOne.getFlagA());
editor.putInt("KEY_COLOR", PaintOne.getPaintColor());
editor.commit();

讀取
SharedPreferences spref;
spref = getPreferences(MODE_PRIVATE);
int IntFLAGA = spref.getInt("KEY_FLAGA", 0);
//0為預設值