android 特殊用法二

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

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));  

1.             //然後呢實現    

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

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

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

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

2.             notification .number = 4;  

11背景漸變:
首先建立文件drawable/shape.xml

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

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中引入該主題,如:

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

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

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

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.               }    

      } 

摘自 LuoXianXiong,您的夥伴

發佈留言