Android常用的一些小知识?

导读:本篇文章讲解 Android常用的一些小知识?,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、判断时候是正确的手机号?

 public boolean isMobileNO(String mobiles) {
        Pattern p = Pattern ..compile(“^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$”);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

 public static boolean isPhone(String str) {
        String regex = “^(1)\\d{10}$”;//正则表达式
        return str.matches(regex);
    }

二、强制隐藏输入法键盘

 private void hideInput(Context context,View view){
        InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
    }

三、获取当前日期和判断两个日期的大小

/**
     * 获取当前日期
     *返回时间类型: yyyy-MM-dd HH:mm:ss
     * */
    public String getCurrentData(){
        SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
        Date curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
        String    str    =    formatter.format(curDate);
        return str;
    }

 /**
     *判断两个日期的大小
     */
    private Date date1 ,date2 ;
    private boolean isDAteTrue = true;
    public boolean dateTrue(String mindate ,String  maxdate){
        //设定时间的模板
        SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
       //得到指定模范的时间
        try {
            date1 = sdf.parse(mindate);
            date2 = sdf.parse(maxdate);
            if(date2.getTime() – date1.getTime() >= 0) {
                isDAteTrue =  true;
            }else if (date2.getTime() – date1.getTime() <0 ){
                isDAteTrue =  false;
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
            Toast.makeText(context ,”格式不正确” ,Toast.LENGTH_SHORT).show();
        }
        return isDAteTrue;
    }
   四、XML中 软键盘和横竖屏属性

 <activity android:name=”.ui.activity.erweima.JiHuoMaActivity”
            android:screenOrientation=”landscape”
            android:windowSoftInputMode=”stateHidden”
            android:theme=”@android:style/Theme.NoTitleBar”/>

在开发android的应用中,有时候需要限制横竖屏切换。只需要在AndroidManifest.xml文件中加入android:screenOrientation属性限制。

android:screenOrientation=”landscape”是限制此页面横屏显示,  
android:screenOrientation=”portrait”是限制此页面数竖屏显示。 
在横竖屏切换时,activity与Fragment都会重建,但是要是直接给Fragment传值则会丢失 

五、有视频的路径来获取缩略图的类?

package ar.system.evaluation.utile;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video.Thumbnails;

public class Utils {

    /**
     * 生成缩略图
     * 
     * @param str
     *            图片路径
     * @return
     */
    public static Bitmap AbbreviationsIcon(String str) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高
        Bitmap bitmap = BitmapFactory.decodeFile(str, options); // 此时返回bm为空
        options.inJustDecodeBounds = false;
        // 计算缩放比
        int be = (int) (options.outHeight / (float) 200);
        if (be <= 0)
            be = 1;
        options.inSampleSize = be;
        // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
        bitmap = BitmapFactory.decodeFile(str, options);
        return bitmap;
    }

    public static Bitmap AbbreviationsIconForVideo(String str, int imageWidth,
            int imageHeight) {
        if (imageWidth == 0) {
            imageWidth = 200;
        }
        if (imageHeight == 0) {
            imageHeight = 200;
        }
        Bitmap bitmap = null;
        if (str.startsWith(“http”)) {

        } else {
            bitmap = ThumbnailUtils.createVideoThumbnail(str,
                    Thumbnails.MINI_KIND);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, imageWidth,
                    imageHeight);
        }
        return bitmap;
    }

    /**
     * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
     * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
     * 
     * @param videoPath
     *            视频的路径
     * @param width
     *            指定输出视频缩略图的宽度
     * @param height
     *            指定输出视频缩略图的高度
     * @param kind
     *            参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
     *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
     * @return 指定大小的视频缩略图
     */
    public static Bitmap getVideoThumbnail(String videoPath, int width,
            int height, int kind) {
        Bitmap bitmap = null;
        // 获取视频的缩略图
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
        if (width != 0 && height != 0) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

    /**
     * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1.
     * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
     * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。 2.
     * 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使 用这个工具生成的图像不会被拉伸。
     * 
     * @param imagePath
     *            图像的路径
     * @param width
     *            指定输出图像的宽度
     * @param height
     *            指定输出图像的高度
     * @return 生成的缩略图
     */
    public static Bitmap getImageThumbnail(String imagePath, int width, int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高,注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; // 设为 false
        // 计算缩放比
        int h = options.outHeight;
        int w = options.outWidth;
        int beWidth = w / width;
        int beHeight = h / height;
        int be = 1;
        if (beWidth < beHeight) {
            be = beWidth;
        } else {
            be = beHeight;
        }
        if (be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }

    public static Bitmap AbbreviationsIconForNetVideo(String url,int imageWidth, int imageHeight) { //通用 (imageWidth = img.getWight…)
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                retriever.setDataSource(url, new HashMap<String, String>());
            } else {
                retriever.setDataSource(url);
            }
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, imageWidth,
                    imageHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

}
 

 

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/119219.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!