一些android代码的特殊用法

来源:互联网 发布:苹果手机网络电话软件 编辑:程序博客网 时间:2024/04/29 07:04

一些android代码的特殊用法

1.让一个图片透明:

[java] view plaincopy
  1. Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);buffer.eraseColor(Color.TRANSPARENT);    


2.直接发送邮件:

[java] view plaincopy
  1.                  Intent intent = new Intent(Intent.ACTION_SENDTO,  Uri .fromParts("mailto"""null));      
  2. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
  3. context.startActivity(intent);     


3.程序控制屏幕变亮:

[java] view plaincopy
  1.              WindowManager.LayoutParams lp = getWindow().getAttributes();       
  2. lp.screenBrightness = 100 / 100.0f;       
  3. getWindow().setAttributes(lp);    


4.过滤特定文本

[java] view plaincopy
  1. Filter filter = myAdapter.getFilter();       
  2. SearchText);     


5.scrollView scroll停止事件

[java] view plaincopy
  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. 对于特定的程序 发起一个关联供打开

[java] view plaincopy
  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);      


 
对于图片上边的不适用索引格式会出错。     

[java] view plaincopy
  1.                   Intent intent = new Intent();        
  2. intent.setAction(android .content.Intent.ACTION_VIEW);        
  3. File file = new File("/sdcard/test.mp4");        
  4. intent.setDataAndType(Uri .fromFile(file), "video/*");        
  5. startActivity(intent);       
  6.      
  7. Intent intent = new Intent();        
  8. intent.setAction(android .content.Intent.ACTION_VIEW);        
  9. File file = new File("/sdcard/test.mp3");        
  10. intent.setDataAndType(Uri .fromFile(file), "audio/*");        
  11. startActivity(intent);     


7.设置文本外观

[java] view plaincopy
  1. setTextAppearance(context, android .R.style.TextAppearance_Medium);       
  2. rance="?android :attr/textAppearanceMedium"    


8.设置单独的发起模式:

[java] view plaincopy
  1.               <activity              
  2. android :name=".ArtistActivity"              
  3. android :label="Artist"              
  4. android :launchMode="singleTop">          
  5. </activity>       
  6.    
  7. tent 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] view plaincopy
  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));     
  9. //然后呢实现      
  10. canvas.drawBitmap(myCoolBitmap, 0,0null);      
  11. canvas.drawBitmap(rounder, 00, xferPaint);     


 

10.在notification 上的icon上加上数字 给人提示有多少个未读

[java] view plaincopy
  1. Notification notification = new Notification (icon, tickerText, when);      
  2. mber = 4;     


11背景渐变: 
首先建立文件drawable/shape.xml

[java] view plaincopy
  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] view plaincopy
  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] view plaincopy
  1. <activity android :name=".ShapeDemo" android :theme="@style/NewTheme">      

 

   该方法同样适用于控件  http://17f8.cn/trackback.php?tbID=259&extra=9d45e9

12. 储存数据 当你在一个实例中保存静态数据,此示例关闭后 下一个实例想引用 静态数据就会为null,这里呢必须重写applition


 

[java] view plaincopy
  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.    }     
0 0