public class AndrodTActivity extends Activity implements OnClickListener {
View v;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v = findViewById(R.id.btn);
v.setOnClickListener(this);
// 从上到下的渐变色,上方蓝色,下方绿色
// 将应用程序的背景色设置成渐变色
GradientDrawable gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.BLUE, Color.GREEN });
getWindow().setBackgroundDrawable(gradientDrawable); }
@Override
public void onClick(View v) {
// 点击截屏按钮,进行截屏操作
View view = getLayoutInflater().inflate(R.layout.main, null);//此处main布局是LinearLayout 若Relativelayout 会报错
//打开图像缓存
view.setDrawingCacheEnabled(true);
//必须调用measure和layout方法才能保存可视组件的截图到png图像文件
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//发送位置和尺寸到view及所有的子View
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
try {
// 获取可视组件的截图
Bitmap drawingCache = view.getDrawingCache();
File file = new File("/emmc/che");
if (!file.exists())
file.mkdirs();
FileOutputStream out = new FileOutputStream("/emmc/che/abc.png");
drawingCache.compress(CompressFormat.PNG, 100, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权声明:本文为CSDN博主「weixin_34008784」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_34008784/article/details/92006034