android

 

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){  
            //竖屏,键盘隐藏了  
    }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章