Android有用代碼片段(三)

轉自http://blog.csdn.net/lilu_leo/article/details/7302343


前兩個已經到第四十個了,所以還得再開一篇,用於記錄,以前文章:Android有用代碼片段(二)android有用代碼片段,有需要的朋友可以去看一下。

         四十一、數據庫寫入圖片信息:

[java] view plaincopy
  1. <span style="font-family:Tahoma, 'Microsoft Yahei', Simsun;color:#444444;">數據庫中的字段設置爲 binary類型  
  2. Bitmap bitmap = BitmapFactory.decodeFile(path);  
  3. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  4.                 bitmap.compress(CompressFormat.JPEG, 50, baos);  
  5. String sql = "insert into pic_info(pic_data, pic_name,pic_size,send_date,is_success) " +"values(?,?,?,?,?)";  
  6.                 Object[] args = new Object[]{baos.toByteArray(), name, size, now, isSucess};  
  7. db.insert(sql, args);  
  8. 讀取數據庫的圖片信息:  
  9. byte[] picData = cursor.getBlob(cursor.getColumnIndex("pic_data"));  
  10. bitmap.setImageBitmap(BitmapFactory.decodeByteArray(picData, 0, picData.length));</span>  

       四十二、listView的addView的問題。

         在listView裏使用addView()、addFooterView(v)、addHeaderView(v)時,要在setAdepter以前添加,或者在重寫的Adapter中添加。因爲setAdapter以後,就是listView已經繪製完畢,不能再進行添加。

         四十三、progressBar修改顏色

        有的時候,我們使用progressBar的時候,後面的背景色是白色或者是亮色,使得progressBar效果很不明顯,所以,我們可以在下面三條中隨意添加一條熟悉就可以了:

[java] view plaincopy
  1. <ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>  
  2. <ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>  
  3. <ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>  

         這是比較簡單的,如果要完成複雜效果,需要自定義style了。

       

        四十四、窗口透明(背景模糊等)

       先看下效果圖:



第一種效果:

在styles.xml中定義

[html] view plaincopy
  1. <style name="Theme.Translucent" parent="android:style/Theme.Translucent">  
  2. <item name="android:windowBackground">  
  3. @drawable/translucent_background  
  4. </item>  
  5. <item name="android:windowNoTitle">true</item>  
  6. <item name="android:colorForeground">#fff</item>  
  7. </style>  

第二種效果是在源代碼裏面修改,在onCreate方法裏面添加:

[java] view plaincopy
  1. getWindow().  
  2. setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,  
  3. WindowManager.LayoutParams.FLAG_BLUR_BEHIND);  
  4. setContentView(R.layout.translucent_background);  

設置模糊效果是通過窗口管理器(WindowManager)設置參數來完成的,這種設置只有在背景設置爲透明後才能顯示效果。


  四十五、Android輸入框和文本框滾動條ScrollView

    我們都知道EditText與TextView是Android的文本輸入框和文本顯示框,但是基於手機屏幕的大小因素,如果在需要輸入較多文字或者顯示較多內容的時候,手機屏幕是遠遠不夠的,因此讓文本框具有滾動條的功能是手機上必備的,


要加上滾動條,其實很簡單,只需要在文本輸入框或者文本顯示框上面加上滾動條控件即可,該控件名字爲ScrollView,以下我們對比下(以TextView舉例)。

A、未加滾動效果

[html] view plaincopy
  1. <TextView   
  2. android:layout_width="fill_parent"   
  3. android:layout_height="wrap_content"   
  4. android:id="@+id/textView"   
  5. />  

B、加上滾動效果

[html] view plaincopy
  1. <ScrollView   
  2. android:id="@+id/scrollView"   
  3. android:layout_width="fill_parent"   
  4. android:layout_height="200px"   
  5. android:scrollbarStyle="outsideOverlay" android:background="@android:drawable/edit_text">   
  6. <TextView   
  7. android:layout_width="fill_parent"   
  8. android:layout_height="wrap_content"   
  9. android:id="@+id/textView"   
  10. />   
  11. </ScrollView>  


由以上例子可以看出,只需要在文本控件的外圍加上ScrollView控件就可以讓文本框具有滾動體的功能。

       四十六、Android模擬器啓動內存錯誤(內存不能爲read)

如圖

運行模擬器的時候總是會內存錯誤。




這種情況偶爾出現,沒什麼關係,不用管他。點擊‘取消’就可以了。 

經常出現就危險了,弄不好就得重裝系統了。 

運行某些程序的時候,有時會出現內存錯誤的提示,然後該程序就關閉。 
“0x????????”指令引用的“0x????????”內存。該內存不能爲“read”。 
“0x????????”指令引用的“0x????????”內存,該內存不能爲“written”。 
一般出現這個現象有方面的,一是硬件,即內存方面有問題,二是軟件 

開始 運行 輸入:cmd 確定: 

在DOS提示符下輸入: 

for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1 

等待3分鐘,左右後,搞定了。(如果怕輸錯,就把這句話複製上去)。

     

    

四十七、通過路徑獲取媒體文件信息

    通過路徑獲取媒體文件信息  http://blog.csdn.net/aomandeshangxiao/article/details/6600725

        四十八、Java中有用的文件操作

      java文件操作 http://blog.csdn.net/aomandeshangxiao/article/details/6597302

       

       四十九、Android文件讀寫

    Android文件的讀寫 http://blog.csdn.net/aomandeshangxiao/article/details/6589510

        五十、

     scaleType屬性與ImagView中圖片的顯示的關係

      

    五十一、  Notification的屬性

                           notification的各種屬性:http://blog.csdn.net/aomandeshangxiao/article/details/6608101

    五十二、Android控件實現震動:

               先在res下面新創建anim文件夾,在該文件夾中創建shake.xml文件:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"   
  4.     android:fromYDelta="0"   
  5.     android:toYDelta="10"  
  6.     android:fromXDelta="0"  
  7.     android:toXDelta="10"  
  8.     android:interpolator="@anim/cycle_7" />  

最後引用了cycle_7,再在該文件夾中創建cycle_7.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:cycles="10" />  
  在Java代碼裏面:

[html] view plaincopy
  1. Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);  
  2.         findViewById(R.id.image).startAnimation(shake);  

就實現了該控件的震動效果。

         

    五十三、Android 頁面切換動畫效果

      http://hi.baidu.com/fountainblog/blog/item/66cb9918b0220eaa4bedbc2e.html

    五十四、Android2.2完全退出程序 

     http://www.eoeandroid.com/thread-62284-1-1.html

    五十五、Android按下back鍵非退出隱藏到後臺

[java] view plaincopy
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {    
  2.     if (keyCode == KeyEvent.KEYCODE_BACK) {    
  3.         Intent intent = new Intent(Intent.ACTION_MAIN);    
  4.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
  5.         intent.addCategory(Intent.CATEGORY_HOME);    
  6.         startActivity(intent);    
  7.         return true;    
  8.     }    
  9.     return super.onKeyDown(keyCode, event);    
  10. }    

    五十六、在Android開發中使用Gallery實現多級聯動

http://mobile.51cto.com/hot-230282.htm


    五十七、獲取view在屏幕中的位置:

[java] view plaincopy
  1. int[] points = { 00 };  
  2.             view.getLocationInWindow(points);  

這裏用數組存儲view的x和y座標,point[0]是x座標,point[1]是y座標。

    五十八、在圖形中添加文字

[java] view plaincopy
  1. @Override  
  2.     protected synchronized void onDraw(Canvas canvas) {  
  3.         super.onDraw(canvas);  
  4.         Rect rect = new Rect();  
  5.         this.mPaint.getTextBounds(this.text, 0this.text.length(), rect);  
  6.         int x = (getWidth() / 2) - rect.centerX();  
  7.         int y = (getHeight() / 2) - rect.centerY();  
  8.         canvas.drawText(this.text, x, y, this.mPaint);  
  9.     }  

     五十九、使用Vibrator實現手機震動

 

[java] view plaincopy
  1. @Override  
  2.     public boolean onTouchEvent(MotionEvent event) {  
  3.   
  4.         if(event.getAction() == MotionEvent.ACTION_DOWN){  
  5.              vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
  6.              long[] pattern = {80040,40030}; // OFF/ON/OFF/ON...  
  7.              vibrator.vibrate(pattern, 2);//-1不重複,非-1爲從pattern的指定下標開始重複  
  8.         }  
  9.         return super.onTouchEvent(event);  
  10.     }   

   六十、界面重繪

              invalidate()或者view.postinvalidate()方法

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