代码情景备忘录

开机欢迎页面透明度变化

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)时置为trueprivate 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;
}

/**
 * shortbyte[]。数组的高位在后,如1,结果为 10 * @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处开始的四个字节翻转。如1234翻转为4321.
 * @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"
        };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值