sqlite的時間默認值設定
博客分類:
android
SQLiteSQL
不能使用getdate()函數
應該用datetime(CURRENT_TIMESTAMP,'localtime')來代替
另外在sql語中有時間比較條件的時候,也應該這樣先使用轉換datetime('2010-1-15 12:00:00'),然後再比較
android中判斷數據庫表是否已經創建
博客分類:
android
Android
create table IF NOT EXISTS tablename (id integer primary key autoincrement);
android中使用Thumbnails批量加載sdcard中的縮略圖片
博客分類:
android
AndroidThumbnails批量加載縮略圖
研究了一上午,終於可以讀取縮略圖了。
這樣得到的是卡中所有圖片的縮略圖,另外可以使用異步加載,提高速度
代碼如下:
Java代碼
String[] projection = { MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY_NAME };
Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");
Cursor c = Thumbnails.queryMiniThumbnails(getContentResolver(), uri,
Thumbnails.MINI_KIND, null);
String[] projection = { MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY_NAME };
Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");
Cursor c = Thumbnails.queryMiniThumbnails(getContentResolver(), uri,
Thumbnails.MINI_KIND, null);
第二行的代碼意思爲取得sdcard的路徑Uri
Java代碼
Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");
Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");
化爲縮略圖再加載
博客分類:
android
stream = new FileInputStream(new File(path+"test.jpg"));
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(stream , null, opts);
iv.setImageBitmap(bitmap);
Animation中多段動畫的連續播放
博客分類:
android
AndroidXML
舉簡單的例子
有兩段動畫,第一個是從左向右平移,第二個是從上往下平移
現在需要在第一個平移結束之後立即開始第二段動畫(並不是使用startAnimation方法兩次來實現)
動畫的xml代碼如下
Xml代碼
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0" android:toXDelta="200"
android:fillAfter="true"
android:duration="@android:integer/config_longAnimTime" />
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="0" android:toYDelta="300"
<SPAN style="COLOR: #ff0000"> android:startOffset="@android:integer/config_longAnimTime" </SPAN>
android:duration="@android:integer/config_longAnimTime" />
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0" android:toXDelta="200"
android:fillAfter="true"
android:duration="@android:integer/config_longAnimTime" />
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="0" android:toYDelta="300"
android:startOffset="@android:integer/config_longAnimTime"
android:duration="@android:integer/config_longAnimTime" />
代碼中紅的一段意思爲這一段動畫的開始時間設置爲第一段動畫的android:duration,意思即第二段動畫在第一段動畫結束之時立即開始,一般來說android:duration手動設置爲整數,即使有多段動畫需要連續播放的話,也可以根據每一段動畫的播放時間來累加,從而計算出第一個動畫的開始運行時間
android中使用線程(比如修改textview的text)
博客分類:
android
AndroidUIthread
線程的實現類如下
Java代碼
class UpdateStatus extends Thread {
@Override
public void run() {
super.run();
while (true) {
if (i == GlobalValues.AUIO_LENGTH || MODE != TIME) {
MODE = 0;
updateStatus.stop();
updateStatus = null;
break;
}
Message m = new Message();
m.what = VoiceForm.TIME;
VoiceForm.this.handler.sendMessage(m);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class UpdateStatus extends Thread {
@Override
public void run() {
super.run();
while (true) {
if (i == GlobalValues.AUIO_LENGTH || MODE != TIME) {
MODE = 0;
updateStatus.stop();
updateStatus = null;
break;
}
Message m = new Message();
m.what = VoiceForm.TIME;
VoiceForm.this.handler.sendMessage(m);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在android的oncreate()方法中添加如下代碼
Java代碼
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case VoiceForm.TIME:
i++;
myTextView1.setText("正在錄音:" + i + "秒");
break;
}
super.handleMessage(msg);
}
};
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case VoiceForm.TIME:
i++;
myTextView1.setText("正在錄音:" + i + "秒");
break;
}
super.handleMessage(msg);
}
};
然後在需要用到線程的地方,啓動線程就行了
這樣做是因爲android只能在它自己開的主線程中進行ui操作,用戶開啓的線程通過Message對象告知handler進行如何操作,相當於用戶開啓的線程只是起到一個通知作用,在handler的實現類中通過switch與case可實現用戶的多種操作
使用Chronometer 間斷計時
博客分類:
android
Android
比較重要的方法
setBase(long time);
start();
getBase();
其中,對setBase加入參數如下:
setBase(SystemClock.elapsedRealtime());
這樣可以實現從0開始計時,並且可以停止與繼續計時的效果(android默認是打開含有Chronometer 的activity時,Chronometer 就開始計時,不管你有有沒有通過按鈕調用它的start()方法)
其它的SystemClock方法如下
SystemClock.elapsedRealtime();//基礎時間
SystemClock.uptimeMillis();
SystemClock.currentThreadTimeMillis();//當前線程開啓的時間長度
Android中VideoView播放當前工程中視頻文件的方法
博客分類:
android
Android
前兩天跟老闆一起研究了很久播放本地工程中的文件,怎麼也試不出來
最後還是老闆發現了一個東西
在VideoView設置uri的時候,加上"android:resource//你的應用包名"+視頻文件在R文件中的ID名稱
例如:
Java代碼
videoView = (VideoView) this.findViewById(R.id.VideoView01);
MediaController controller = new MediaController(this);
this.videoView.setMediaController(controller);
//下面android:resource://是固定的,org.dengzh是我的包名,R.raw.movie_1是id名稱
videoView.setVideoURI(Uri.parse("android.resource://org.dengzh/"+R.raw.movie_1));
videoView = (VideoView) this.findViewById(R.id.VideoView01);
MediaController controller = new MediaController(this);
this.videoView.setMediaController(controller);
//下面android:resource://是固定的,org.dengzh是我的包名,R.raw.movie_1是id名稱
videoView.setVideoURI(Uri.parse("android.resource://org.dengzh/"+R.raw.movie_1));
這樣的話,就可以播放本地的視頻了
Android橫豎屏切換方法
博客分類:
android
Android遊戲XML活動Blog
Java代碼
關於Android橫豎屏切換的解決方法
轉載自:http://rayleung.iteye.com/blog/426972
在開發遊戲的時候,有些遊戲是隻能橫屏玩的,所以手機豎立放置的時候,要保持遊戲畫面依然橫屏。要做到這個要求其實很簡單,在 AndroidManifest.xml裏面配置一下就可以了。加入這一行 android:screenOrientation="landscape"。
例如(landscape是橫向,portrait是縱向):
Java代碼
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.ray.linkit"
4. android:versionCode="1"
5. android:versionName="1.0">
6. <application android:icon="@drawable/icon" android:label="@string/app_name">
7. <activity android:name=".Main"
8. android:label="@string/app_name"
9. android:screenOrientation="portrait">
10. <intent-filter>
11. <action android:name="android.intent.action.MAIN" />
12. <category android:name="android.intent.category.LAUNCHER" />
13. </intent-filter>
14. </activity>
15. <activity android:name=".GamePlay"
16. android:screenOrientation="portrait"></activity>
17. <activity android:name=".OptionView"
18. android:screenOrientation="portrait"></activity>
19. </application>
20. <uses-sdk android:minSdkVersion="3" />
21. </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ray.linkit"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GamePlay"
android:screenOrientation="portrait"></activity>
<activity android:name=".OptionView"
android:screenOrientation="portrait"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
另外,android中每次屏幕的切換動會重啓Activity,所以應該在Activity銷燬前保存當前活動的狀態,在Activity再次 Create的時候載入配置,那樣,進行中的遊戲就不會自動重啓了!
當屏幕變爲橫屏的時候,系統會重新呼叫當前Activity的OnCreate方法,你可以把以下方法放在你的OnCreate中來檢查當前的方向,然後可以讓你的SetContentView來載入不同的Layout xml.
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}
關於屏幕切換的時候
首先需要在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
這樣在程序中. Activity就不會重複的調用onCreate()
甚至不會調用onPause.onResume.
只會調用一個onConfigurationChanged(Configuration newConfig)
這是在XML加入配置選項的前提下.
如果在就加入選項的前提下.如上所說. Activity會重新激活onCreate方法
根據你自己的需求來選擇配置改變時的處理機制這樣比較好一點。
關於Android橫豎屏切換的解決方法
轉載自:http://rayleung.iteye.com/blog/426972
在開發遊戲的時候,有些遊戲是隻能橫屏玩的,所以手機豎立放置的時候,要保持遊戲畫面依然橫屏。要做到這個要求其實很簡單,在 AndroidManifest.xml裏面配置一下就可以了。加入這一行 android:screenOrientation="landscape"。
例如(landscape是橫向,portrait是縱向):
Java代碼
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.ray.linkit"
4. android:versionCode="1"
5. android:versionName="1.0">
6. <application android:icon="@drawable/icon" android:label="@string/app_name">
7. <activity android:name=".Main"
8. android:label="@string/app_name"
9. android:screenOrientation="portrait">
10. <intent-filter>
11. <action android:name="android.intent.action.MAIN" />
12. <category android:name="android.intent.category.LAUNCHER" />
13. </intent-filter>
14. </activity>
15. <activity android:name=".GamePlay"
16. android:screenOrientation="portrait"></activity>
17. <activity android:name=".OptionView"
18. android:screenOrientation="portrait"></activity>
19. </application>
20. <uses-sdk android:minSdkVersion="3" />
21. </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ray.linkit"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GamePlay"
android:screenOrientation="portrait"></activity>
<activity android:name=".OptionView"
android:screenOrientation="portrait"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
另外,android中每次屏幕的切換動會重啓Activity,所以應該在Activity銷燬前保存當前活動的狀態,在Activity再次 Create的時候載入配置,那樣,進行中的遊戲就不會自動重啓了!
當屏幕變爲橫屏的時候,系統會重新呼叫當前Activity的OnCreate方法,你可以把以下方法放在你的OnCreate中來檢查當前的方向,然後可以讓你的SetContentView來載入不同的Layout xml.
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}
關於屏幕切換的時候
首先需要在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
這樣在程序中. Activity就不會重複的調用onCreate()
甚至不會調用onPause.onResume.
只會調用一個onConfigurationChanged(Configuration newConfig)
這是在XML加入配置選項的前提下.
如果在就加入選項的前提下.如上所說. Activity會重新激活onCreate方法
根據你自己的需求來選擇配置改變時的處理機制這樣比較好一點。
設置ImageButton按下後的效果
博客分類:
android
UP
Java代碼
ImageButton imgb = (ImageButton) findViewById(R.id.ImageButton01);
imgb.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.TextView01);
txt.setText("圖片按鈕被單擊了");
v.setBackgroundResource(R.drawable.img_10_10);
}
});
imgb.setOnTouchListener(new Button.OnTouchListener() {
//按下時進行圖片顏色的過濾處理
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BT_SELECTED));
v.setBackgroundDrawable(v.getBackground());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BT_NOT_SELECTED));
v.setBackgroundDrawable(v.getBackground());
}
return false;
}
});
}
/**
* 按下這個按鈕進行的顏色過濾
*/
public final static float[] BT_SELECTED = new float[] { 2, 0, 0, 0, 2, 0,
2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0 };
/**
* 按鈕恢復原狀的顏色過濾
*/
public final static float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
ImageButton imgb = (ImageButton) findViewById(R.id.ImageButton01);
imgb.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.TextView01);
txt.setText("圖片按鈕被單擊了");
v.setBackgroundResource(R.drawable.img_10_10);
}
});
imgb.setOnTouchListener(new Button.OnTouchListener() {
//按下時進行圖片顏色的過濾處理
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BT_SELECTED));
v.setBackgroundDrawable(v.getBackground());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BT_NOT_SELECTED));
v.setBackgroundDrawable(v.getBackground());
}
return false;
}
});
}
/**
* 按下這個按鈕進行的顏色過濾
*/
public final static float[] BT_SELECTED = new float[] { 2, 0, 0, 0, 2, 0,
2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0 };
/**
* 按鈕恢復原狀的顏色過濾
*/
public final static float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
Android中去除標題,全屏,獲得屏幕方向及鍵盤狀態
博客分類:
android
Android遊戲
Android全屏設置代碼
如果你在開發遊戲或一個主題風格很特別的應用可能需要全屏顯示,在Android中全屏窗口的代碼很簡單,主要分爲兩個步驟和一個注意點:
requestWindowFeature(Window.FEATURE_NO_TITLE); //隱藏標題
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //設置全屏
注意的是這些調用要放在SetContentView前面,否則無法生效或結果有出入。
獲取Android屏幕方向及鍵盤狀態
很多開發Android的網友可能需要判斷當前的屏幕方向或鍵盤狀態,下面的代碼可以判斷出橫屏landscape和常規的portrait縱握方式,如果使用的是G1這樣有QWERTY鍵盤硬件的,還可以判斷屏幕方向以及鍵盤的拉出狀態。
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){
//橫屏,比如 480x320
}else if(config.orientation == Configuration.ORIENTATION_PORTRAIT){
//豎屏 ,標準模式 320x480
}else if(config.hardKeyboardHidden == Configuration.KEYBOARDHIDDEN_NO){
//橫屏,Android123提示物理鍵盤滑出了
}else if(config.hardKeyboardHidden == Configuration.KEYBOARDHIDDEN_YES){
//豎屏,鍵盤隱藏了
}