网上有很多关于截屏的的案例,但是我想说的是一个更加简单的截屏!只有一个类,同时让截屏出来的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);
以上,效果图就不贴了