開機歡迎頁面透明度變化
launchImageView= (ImageView) findViewById(R.id.launchshowimageview); AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f); alphaAnimation.setDuration(3000);// 設置動畫顯示時間 launchImageView.startAnimation(alphaAnimation); alphaAnimation.setAnimationListener(new AnimationImpl());
Android Studio中默認繼承AppCompatActivity,全屏方法
<!-- 某些Activity需要全屏展示 -->
<style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
AppCompatActivity
全屏:
<style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
無標題欄:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_flight);
getSupportActionBar().hide();
ListView Divider 寬度
用Drawable,Inset做根
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="10dp"
android:insetRight="10dp" >
<shape android:shape="rectangle" >
<solid android:color="@color/***" />
</shape>
</inset>
監聽WIFI
//當連接到某個WIFI上時,總是會接收到兩次廣播通知,原因不明。 //用該變量來控制,不處理第二次的通知。收到一次CONNECTED後就置爲false。 //當收到WIFI斷開(DISCONNECTED)時置爲true。 private boolean blockSecond=true; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) { //signal strength changed } else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {//wifi打開與否 int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); if (wifistate == WifiManager.WIFI_STATE_DISABLED) { Toast.makeText(context, "系統關閉wifi", Toast.LENGTH_SHORT).show(); System.out.println("狀態 WIFI_STATE_DISABLED"); } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) { Toast.makeText(context, "系統開啓wifi", Toast.LENGTH_SHORT).show(); System.out.println("狀態 WIFI_STATE_ENABLED"); } else if(wifistate == WifiManager.WIFI_STATE_DISABLING) { System.out.println("狀態 WIFI_STATE_DISABLING"); } else if(wifistate == WifiManager.WIFI_STATE_ENABLING) { System.out.println("狀態 WIFI_STATE_ENABLING"); } else if(wifistate == WifiManager.WIFI_STATE_ENABLING) { System.out.println("狀態 WIFI_STATE_ENABLING"); } } else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); System.out.println(info.getExtraInfo()+":"+info.getDetailedState().name()+":"+info.getSubtypeName()); if(info.getState().equals(NetworkInfo.State.CONNECTED)){ if(blockSecond) { WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //獲取當前wifi名稱 System.out.println("連接到網絡 " + wifiInfo.getSSID()+":"); blockSecond=false; } } else if(info.getState().equals(NetworkInfo.State.DISCONNECTED)) { blockSecond=true; } } }
字節轉換
/** * 將含兩個字節的字節數組轉爲short。低位字節在前。 * @param target * @return */ public static short byteArrayToShort(byte[] target)//低位字節在前 { short s = 0; short s0 = (short) (target[0] & 0xff);// 最低位 short s1 = (short) (target[1] & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; } /** * 將兩個字節轉爲short. * @param lowByte 低位字節 * @param highByte 高位字節 * @return */ public static short byteArrayToShort(byte lowByte,byte highByte)//低位字節在前 { short s = 0; short s0 = (short) (lowByte & 0xff);// 最低位 short s1 = (short) (highByte & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; } /** * 將int轉爲長度爲4的字節數組。返回的數組低位在前,高位在後。 * @param res * @return */ public static byte[] intToByteArray(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff);// 最低位 targets[1] = (byte) ((res >> 8) & 0xff);// 次低位 targets[2] = (byte) ((res >> 16) & 0xff);// 次高位 targets[3] = (byte) (res >>> 24);// 最高位,無符號右移。 return targets; } /** * 將長度爲4的字節數組轉爲int類型。數組中,低位在前,高位在後。 * @param res * @return */ public static int byteArrayToInt(byte[] res) { int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24); return targets; } /** * 將長度爲4的字節數組轉爲int類型。數組中,低位在前,高位在後。 * @param res * @param startIndex 四個字節中起始字節的索引。 * @return */ public static int byteArrayToInt(byte[] res,int startIndex) { int targets = (res[startIndex] & 0xff) | ((res[startIndex+1] << 8) & 0xff00) | ((res[startIndex+2] << 24) >>> 8) | (res[startIndex+3] << 24); return targets; } /** * short轉byte[]。數組的高位在後,如1,結果爲 1, 0。 * @param number * @return */ public static byte[] shortToByteArray(short number) { int temp = number; byte[] b = new byte[2]; for (int i = 0; i < b.length; i++) { b[i] = new Integer(temp & 0xff).byteValue(); temp = temp >> 8; // 向右移8位 } return b; } /** * 將target數組從索引index處開始的四個字節翻轉。如1、2、3、4翻轉爲4、3、2、1. * @param target */ public static void reverse(byte[] target,int index) { byte temp=target[index]; target[index]=target[index+3]; target[index+3]=temp; temp=target[index+1]; target[index+1]=target[index+2]; target[index+2]=temp; }
獲取手機電量:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intentBattery = registerReceiver(null, intentFilter);//注意,粘性廣播不需要廣播接收器 if(intentBattery!=null) { //獲取當前電量 int batteryLevel = intentBattery.getIntExtra("level", 0); //電量的總刻度 int batterySum = intentBattery.getIntExtra("scale", 100); float rotatio = 100*(float)batteryLevel/(float)batterySum; mobileElectricityTV.setText(rotatio+"%"); }
廣播獲取電量:
else if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()))//如果監聽到手機電池電量有變化 { Handler tempHandler=observer.get(ConstantValues.Key_FlightActivity); if(tempHandler!=null) { //獲取當前電量 int level = intent.getIntExtra("level", 0); //電量的總刻度 int scale = intent.getIntExtra("scale", 100); mobileElectricity=((level*100)/scale); Message msg=tempHandler.obtainMessage(); msg.what=ConstantValues.MW_MOBILEELECTRICITY; Bundle tempBundle=msg.getData(); tempBundle.clear(); tempBundle.putInt(ConstantValues.MK_MOBILEELECTRICITY,mobileElectricity); msg.sendToTarget(); } }
在自定義View的時候,獲取寬高在onSizeChanged( )方法,而不要在onMeasure( )方法
動畫:
ScaleAnimation scaleAnimation=new ScaleAnimation(0.6f, 0.4f, 0.6f, 0.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1000); view.startAnimation(scaleAnimation);
MD5生成工具
public class Md5Tools { /** * 默認的密碼字符串組合,用來將字節轉換成 16 進製表示的字符,apache校驗下載的文件的正確性用的就是默認的這個組合 */ protected static char hexDigits[] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; protected static MessageDigest messagedigest = null ; static { try { messagedigest = MessageDigest.getInstance("MD5" ); } catch (NoSuchAlgorithmException nsaex) { System.err.println(Md5Tools.class .getName() + "初始化失敗,MessageDigest不支持MD5Util。" ); nsaex.printStackTrace(); } } /** * 生成字符串的md5校驗值 * * @param s * @return */ public static String getMD5String(String s) { return getMD5String(s.getBytes()); } /** * 判斷字符串的md5校驗碼是否與一個已知的md5碼相匹配 * * @param password 要校驗的字符串 * @param md5PwdStr 已知的md5校驗碼 * @return */ public static boolean checkPassword(String password, String md5PwdStr) { String s = getMD5String(password); return s.equals(md5PwdStr); } /** * 生成文件的md5校驗值 * * @param file * @return * @throws IOException */ public static String getFileMD5String(File file) throws IOException { InputStream fis; fis = new FileInputStream(file); byte [] buffer = new byte [ 1024 ]; int numRead = 0 ; while ((numRead = fis.read(buffer)) > 0 ) { messagedigest.update(buffer, 0 , numRead); } fis.close(); return bufferToHex(messagedigest.digest()); } /** * JDK1.4中不支持以MappedByteBuffer類型爲參數update方法,並且網上有討論要慎用MappedByteBuffer, * 原因是當使用 FileChannel.map 方法時,MappedByteBuffer 已經在系統內佔用了一個句柄, * 而使用 FileChannel.close 方法是無法釋放這個句柄的,且FileChannel有沒有提供類似 unmap 的方法, * 因此會出現無法刪除文件的情況。 * * 不推薦使用 * * @param file * @return * @throws IOException */ public static String getFileMD5String_old(File file) throws IOException { FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0 , file.length()); messagedigest.update(byteBuffer); return bufferToHex(messagedigest.digest()); } public static String getMD5String( byte [] bytes) { messagedigest.update(bytes); return bufferToHex(messagedigest.digest()); } private static String bufferToHex( byte bytes[]) { return bufferToHex(bytes, 0 , bytes.length); } private static String bufferToHex( byte bytes[], int m, int n) { StringBuffer stringbuffer = new StringBuffer( 2 * n); int k = m + n; for ( int l = m; l < k; l++) { appendHexPair(bytes[l], stringbuffer); } return stringbuffer.toString(); } private static void appendHexPair( byte bt, StringBuffer stringbuffer) { char c0 = hexDigits[(bt & 0xf0 ) >> 4 ]; // 取字節中高 4 位的數字轉換, >>> 爲邏輯右移,將符號位一起右移,此處未發現兩種符號有何不同 char c1 = hexDigits[bt & 0xf ]; // 取字節中低 4 位的數字轉換 stringbuffer.append(c0); stringbuffer.append(c1); } /** * 在指定目錄搜索某文件是否存在 * @param fileName 要被搜索的文件的名字 * @param dir 要被搜索的目錄 * @return */ public static boolean scanFile(String fileName,String dir) { File fileDir=new File(dir); if(fileDir.isDirectory()) { File[] file=fileDir.listFiles(); for(File f:file) { if(f.getName().equals(fileName)) { return true; } } } return false; } }
圖片處理
public static Bitmap decodeSampledBitmap(Resources res,int resId,int reqWidth,int reqHeight) { BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeResource(res,resId,options); options.inSampleSize=calCulateInSampleSize(options,reqWidth,reqHeight); options.inJustDecodeBounds=false; return BitmapFactory.decodeResource(res,resId,options); } public static int calCulateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight) { final int height=options.outHeight; final int width=options.outWidth; int inSampleSize=1; if(height>reqHeight || width>reqWidth) { final int halfHeight=height/2; final int halfWidth=width/2; while((halfHeight/inSampleSize)>=reqHeight && (halfWidth/inSampleSize)>=reqWidth) { inSampleSize*=2; } } return inSampleSize; }
大圖片網址
public static String[] array=new String[] { "http://pic7.nipic.com/20100428/99940_111931251381_2.jpg", "http://img2081.poco.cn/mypoco/myphoto/20120123/15/55987978201201231506241753913643595_003.jpg", "http://hs.wenming.cn/tkhs/201509/W020150925287730686731.jpg", "http://hs.wenming.cn/tkhs/201509/W020150925287730920098.jpg", "http://www.photo0086.com/member/808/pic/2009012823422842285.JPG", "http://s3.lvjs.com.cn/uploads/pc/place2/2015-10-15/1e23b9bc-b0ba-4501-946b-a5c9fac10283.jpg", "http://youimg1.c-ctrip.com/target/fd/tg/g2/M03/9B/54/Cghzf1W5puWAPPMXAAnF2Uozels894.jpg", "http://www.xp71.com/uploads/allimg/150127/1-15012F94442F1.jpg", "http://www.ctps.cn/PhotoNet/Profiles2011/20110307/20113711928249.jpg", "http://pic.tourunion.com/ePic/2012/12/20121204044814a2f87_b.jpg", "http://yichan.cnair.com/uploadfile/2013/1022/20131022010715168.jpg", "http://youimg1.c-ctrip.com/target/tg/715/723/792/840606e60e134172afd1aa40a017d38c.jpg", "http://www.fansimg.com/album/201305/24/201222f0sdc1bqsco1en93.jpg", "http://image.xitek.com/photo/201312/20808/2080865/2080865_1385858197_53175900.jpg", "http://www.colourhs.com/uploads/allimg/1208/3-120R5094006.jpg", "http://images.china.cn/attachement/jpg/site1000/20141022/c03fd5e7be1c15b148d654.jpg", "http://images.rednet.cn/articleimage/2015/06/18/11522638.jpg", "http://images.rednet.cn/articleimage/2015/06/18/11522144.jpg", "http://img3.xiangshu.com/Day_140606/64_469573_51600efbff3ed06.jpg", "http://g4.hexunimg.cn/2016-03-11/182701393.jpg", "http://www.51766.com/img/haixintrip/1377249345201.jpg", "http://limg1.qunarzz.com/T1KyVTBKhT1R49Ip6K.jpeg_r_1920x1080x90_d8e5b9e0.jpeg", "http://cdn.duitang.com/uploads/item/201408/26/20140826180739_aXNfA.jpeg", "http://www.nbsy.com/data/attachment/forum/201303/31/155911nxfyqfzqfuanenfq.jpg", "http://cyjctrip.qiniudn.com/108284/1396423501194p18kgkkm7s1bmj1k0d1ap51rgc1kkk27.jpg", "http://img3.douban.com/pview/event_poster/raw/public/683132a28d8101f.jpg", "http://www.fjuu8.com/files/2013-1/20130124111302153351.jpg", "http://img.kpkpw.com/pic/2/28/28468f81-4443-4fd7-92b3-b46c78da700d.jpg", "http://images.takungpao.com/2013/0705/20130705033708538.jpg", "http://image.tianjimedia.com/uploadImages/2014/093/20/T04D8D2OM9Z4.jpg" };