Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 的一些特殊技巧的代碼實現

Android 的一些特殊技巧的代碼實現

編輯:高級開發

1.讓一個圖片透明:

  復制到剪貼板 Java代碼

  1. Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);buffer.eraseColor(Color.TRANSPARENT);

  2.直接發送郵件:

  復制到剪貼板 Java代碼

  1. Intent intent = new Intent(Intent.ACTION_SENDTO, Uri .fromParts( "mailto" , "[email protected]" , null ));

  2. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  3. context.startActivity(intent);

  3.程序控制屏幕變亮:

  復制到剪貼板 Java代碼

  1. WindowManager.LayoutParams lp = getWindow().getAttributes();

  2. lp.screenBrightness = 100 / 100 .0f;

  3. getWindow().setAttributes(lp);

  4.過濾特定文本

  復制到剪貼板 Java代碼

  1. Filter filter = myAdapter.getFilter();

  2. filter.filter(mySearchText);

  5.scrollVIEw scroll停止事件

  復制到剪貼板 Java代碼

  1. setOnScrollListener( new OnScrollListener(){

  2. public void onScroll(AbsListView vIEw, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

  3. // TODO Auto-generated method stub }

  4. public void onScrollStateChanged(AbsListView vIEw, int scrollState) {

  5. // TODO Auto-generated method stub

  6. if (scrollState == 0 ) Log.i( "a" , "scrolling stopped..." ); } });}

  6. 對於特定的程序 發起一個關聯供打開

  復制到剪貼板 C/C++代碼

  1. Bitmap bmp = getImageBitmap(jpg);

  2. String path = getFilesDir().getAbsolutePath() + "/test.png" ;

  3. File file = new File(path);

  4. FileOutputStream fos = new FileOutputStream(file);

  5. bmp.compress( CompressFormat.PNG, 100, fos );

  6. fos.close();

  7.

  8. Intent intent = new Intent();

  9. intent.setAction(android .content.Intent.ACTION_VIEW);

  10. intent.setDataAndType(Uri .fromFile( new File(path)), "image/png"

  接上頁

);

  11. startActivity(intent);

  12. 對於圖片上邊的不適用索引格式會出錯。

  13. Intent intent = new Intent();

  14. intent.setAction(android .content.Intent.ACTION_VIEW);

  15. File file = new File( "/sdcard/test.mp4" );

  16. intent.setDataAndType(Uri .fromFile(file), "video/*" );

  17. startActivity(intent);

  18.

  19. Intent intent = new Intent();

  20. intent.setAction(android .content.Intent.ACTION_VIEW);

  21. File file = new File( "/sdcard/test.mp3" );

  22. intent.setDataAndType(Uri .fromFile(file), "audio/*" );

  23. startActivity(intent);

  7.設置文本外觀

  復制到剪貼板 Java代碼

  1. setTextAppearance(context, android .R.style.TextAppearance_Medium);

  2. android :textAppearance= "?android :attr/textAppearanceMedium"

  8.設置單獨的發起模式:

  復制到剪貼板 Java代碼

  1. < activity

  2. android :name= ".ArtistActivity"

  3. android :label= "Artist"

  4. android :launchMode= "singleTop" >

  5. < /activity>

  6.

  7. Intent i = new Intent();

  8. i.putExtra(EXTRA_KEY_ARTIST, id);

  9. i.setClass( this , ArtistActivity. class );

  10. i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

  11. startActivity(i);

  9.創建一個圓角圖片

  這個的主要原理其實就是利用遮罩,先創建一個圓角方框 然後將圖片放在下面:

  復制到剪貼板 Java代碼

  1. Bitmap myCoolBitmap = ... ;

  2. int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();

  3. Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);

  4. Canvas canvas = new Canvas(rounder);

  5. Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  6. xferPaint.setColor(Color.RED);

  7. canvas.drawRoundRect( new RectF( 0 , 0 ,w,h), 20 .0f, 20 .0f,

  接上頁

xferPaint);

  8. xferPaint.setXfermode( new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

  復制到剪貼板 Java代碼

  1. //然後呢實現

  2. canvas.drawBitmap(myCoolBitmap, 0 , 0 , null );

  3. canvas.drawBitmap(rounder, 0 , 0 , xferPaint);

  10.在notification 上的icon上加上數字 給人提示有多少個未讀

  復制到剪貼板 Java代碼

  1. Notification notification = new Notification (icon, tickerText, when);

  2. notification .number = 4 ;

  11背景漸變:

  首先建立文件drawable/shape.XML

  復制到剪貼板 Java代碼

  1. < ?XML version= "1.0" encoding= "utf-8" ?>

  2. < shape XMLns:android = "http://schemas.android .com/apk/res/android " android :shape= "rectangle" >

  3. < gradIEnt android :startColor= "#FFFFFFFF" android :endColor= "#FFFF0000"

  4. android :angle= "270" />

  5. < /shape>

  在該文件中設置漸變的開始顏色(startColor)、結束顏色(endColor)和角度(angle)

  接著創建一個主題values/style.XML

  復制到剪貼板 Java代碼

  1. < ?XML version= "1.0" encoding= "utf-8" ?>

  2. < resources>

  3. < style name= "NewTheme" parent= "android :Theme" >

  4. < item name= "android :background" > @drawable /shape< /item>

  5. < /style>

  6. < /resources>

  然後在androidManifest.XML文件中的application或activity中引入該主題,如:

  復制到剪貼板 Java代碼

  1. < activity android :name= ".ShapeDemo" android :theme= "@style/NewTheme" >

  該方法同樣適用於控件 http://17f8.cn/trackback.PHP?tbID=259&extra=9d45e9

  12. 儲存數據 當你在一個實例中保存靜態數據,此示例關閉後 下一個實例想引用 靜態數據就會為null,這裡呢必須重寫applition

  復制到剪貼板 Java代碼

  1. public class MyApplication extends Application{

  接上頁

  2. private String thing = null ;

  3. public String getThing(){

  4. return thing;

  5. }

  6. public void setThing( String thing ){

  7. this .thing = thing; }

  8. }

  9. public class MyActivity extends Activity {

  10. private MyApplication app;

  11. public void onCreate(Bundle savedInstanceState) {

  12. super .onCreate(savedInstanceState);

  13. app = ((MyApplication)getApplication());

  14. String thing = app.getThing();

  15. }

  16. }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved