主要的功能是点击button按钮,将当前的屏幕截取,将截取的图片设置为imageView的内容。
MainActivity的代码如下
public class MainActivity extends Activity {
Button btn_capture;
ImageView iv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_capture=(Button)findViewById(R.id.btn_capture);
iv_show=(ImageView)findViewById(R.id.iv_show);
btn_capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();//获取当前屏幕的图片
iv_show.setImageBitmap(bitmap);//将图片设置为ImageView的内容
}
});
}
}
布局文件activity_main的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="截屏"
/>
<ImageView
android:id="@+id/iv_show"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/btn_capture"
/>
</RelativeLayout>
其他的地方不需要修改,使用默认就行。
510

被折叠的 条评论
为什么被折叠?



