sdk之截屏

网上有很多关于截屏的的案例,但是我想说的是一个更加简单的截屏!只有一个类,同时让截屏出来的bitmap做动画!
首先bitmap是不能直接进行动画的,他需要一个载体,即是imageview来承载这个动画:
主体代码如下:

//注册成功截屏返回相册
                final Bitmap bitmap = ScreenUtils.snapShotWithStatusBar(getActivity());
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                ViewGroup.LayoutParams layoutParams = mImageCupView.getLayoutParams();
                layoutParams.height = height;
                layoutParams.width = width;
                //mImagerCupView是布局文件中创建的
                mImageCupView.setLayoutParams(layoutParams);
                mImageCupView.setImageBitmap(bitmap);
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator animatory = ObjectAnimator.ofFloat(mImageCupView, "scaleY", 1.0f, 0.0f);
                ObjectAnimator animatorx = ObjectAnimator.ofFloat(mImageCupView, "scaleX", 1.0f, 0.0f);
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ScreenUtils.saveGoodBitmap(mContext, bitmap);
                        //后面可以继续接着写其他活动,如页面的跳转等等
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animatorSet.setDuration(1000);
                animatorSet.play(animatory).with(animatorx);//两个动画同时开始
                animatorSet.start();

工具类如下:

@SuppressLint("SimpleDateFormat")
public class ScreenUtils {
    private ScreenUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获得屏幕高度
     * 
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;

    }

    /**
     * 获得屏幕宽度
     * 
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 获得状态栏的高度
     * 
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context) {

        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 获取当前屏幕截图,包含状态栏
     * 
     * @param activity
     * @return
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = convertViewToBitmap(activity, view);
        return bitmap;

    }

    public static void saveGoodBitmap(Context context, Bitmap bitmap) {
        String sdCardDir = Environment.getExternalStorageDirectory() + "/DCIM/";
        File appDir = new File(sdCardDir, "sdkui");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "sdkui" + getCurrentTime() + ".png";
        File f = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

            notifyDICM(context, f);
            try {
                fos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static String getCurrentTime() {

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy_MM_dd_HH_mm_ss");
        return dateFormat.format(date);
    }

    public static Bitmap convertViewToBitmap(Context context, View view) {
        int width = (int) (getScreenWidth(context) * 0.65);
        int height = (int) (getScreenHeight(context) * 0.7);

        view.layout(0, 0, width, height);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        return bitmap;
    }

    private static void notifyDICM(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        context.sendBroadcast(intent);

        Toast.makeText(context, "您的账号密码已截图保存至相册", Toast.LENGTH_SHORT).show();
    }
}

实际上主体代码就二行:

final Bitmap bitmap = ScreenUtils.snapShotWithStatusBar(getActivity());
ScreenUtils.saveGoodBitmap(mContext, bitmap);

以上,效果图就不贴了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值