«

在Android中实现截图功能

时间:2024-3-2 19:58     作者:韩俊     分类: Android


在实际应用中,经常需要对图片进行处理,包括压缩、截图等等,其实android系统提供了一个可以截图的activity,我们只需调用它就行了,下面示例完成一个通过相机照相或相册选取图片后对其截图的功能,其实知道图片的路劲都可以调用截图activity,代码如下:

测试activity:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

import

java.io.File;

import

android.app.Activity;

import

android.content.Intent;

import

android.graphics.Bitmap;

import

android.graphics.drawable.BitmapDrawable;

import

android.net.Uri;

import

android.os.Bundle;

import

android.os.Environment;

import

android.provider.MediaStore;

import

android.provider.MediaStore.Images.Media;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

import

android.widget.ImageView;

import

android.widget.Toast;

public

class
TestClipImageActivity extends

Activity implements

OnClickListener {

private

Button gallaryBtn;

private

Button cameraBtn;

private

ImageView showImage;

private

File file;

@Override 

protected

void
onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    gallaryBtn

= (Button) findViewById(R.id.main_btn_gallery);

    cameraBtn

= (Button) findViewById(R.id.main_btn_camera);

    gallaryBtn.setOnClickListener(this); 

    cameraBtn.setOnClickListener(this); 

    showImage

= (ImageView) findViewById(R.id.main_image);

} 

@Override 

public

void
onClick(View v) {

    if

(v == gallaryBtn) {

        //

跳转至相册界面

        Intent

intent = new

Intent(Intent.ACTION_PICK,

                Media.EXTERNAL_CONTENT_URI); 

        startActivityForResult(intent, 0); 

    } 

    if

(v == cameraBtn) {

        if

(!Environment.MEDIA_MOUNTED.equals(Environment

                .getExternalStorageState()))

{

            Toast.makeText(this, "请插入SD卡",

Toast.LENGTH_SHORT).show();

            return; 

        } 

        //

跳转至相机界面

        Intent

intent = new

Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        file

= new

File(Environment.getExternalStorageDirectory(),

                getPhotoName()); 

        //

指定相机拍照后相片的存储位置

        intent.putExtra(MediaStore.EXTRA_OUTPUT,

Uri.fromFile(file));

        startActivityForResult(intent, 1); 

    } 

} 

@Override 

protected

void
onActivityResult(int

requestCode, int

resultCode, Intent data) {

    super.onActivityResult(requestCode,

resultCode, data);

    //

系统相册返回

    if

(requestCode == 0

&& resultCode == Activity.RESULT_OK

            &&

data != null)
{

        startPhotoZoom(data.getData(), 150);//

截图

    } 

    //

系统相机返回

    if

(requestCode == 1

&& resultCode == Activity.RESULT_OK) {

        startPhotoZoom(Uri.fromFile(file), 150);//

截图

    } 

    //

截图后返回

    if

(requestCode == 2

&& data != null)
{

        Bundle

bundle = data.getExtras();

        if

(bundle != null)
{

            Bitmap

bitmap = bundle.getParcelable("data");

            showImage.setBackgroundDrawable(new

BitmapDrawable(bitmap));

        } 

    } 

} 

/**

 *

跳转至系统截图界面进行截图

 * 

 *

@param data

 *

@param size

 */ 

private

void
startPhotoZoom(Uri data, int

size) {

    Intent

intent = new

Intent("com.android.camera.action.CROP");

    intent.setDataAndType(data, "image/*"); 

    //

crop为true时表示显示的view可以剪裁

    intent.putExtra("crop", "true"); 

    //

aspectX aspectY 是宽高的比例

    intent.putExtra("aspectX", 1); 

    intent.putExtra("aspectY", 1); 

    //

outputX,outputY 是剪裁图片的宽高

    intent.putExtra("outputX",

size);

    intent.putExtra("outputY",

size);

    intent.putExtra("return-data", true); 

    startActivityForResult(intent, 2); 

} 

/** 

 *

产生照片名称

 *  

 *

@return

 */ 

private

String getPhotoName() {

    String

name = System.currentTimeMillis() + ".jpg";

    return

name;

} 

}

布局文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android&quot;

android:layout_width=&quot;match_parent&quot; 

android:layout_height=&quot;match_parent&quot; 

android:orientation=&quot;vertical&quot;

>

&lt;LinearLayout 

    android:layout_width=&quot;match_parent&quot; 

    android:layout_height=&quot;wrap_content&quot; 

    android:gravity=&quot;center_horizontal&quot; 

    android:orientation=&quot;horizontal&quot;

>

    &lt;Button 

        android:id=&quot;@&#43;id/main_btn_gallery&quot; 

        android:layout_width=&quot;wrap_content&quot; 

        android:layout_height=&quot;wrap_content&quot; 

        android:text=&quot;相册&quot;

/>

    &lt;Button 

        android:id=&quot;@&#43;id/main_btn_camera&quot; 

        android:layout_width=&quot;wrap_content&quot; 

        android:layout_height=&quot;wrap_content&quot; 

        android:text=&quot;相机&quot;

/>

&lt;/LinearLayout&gt; 

&lt;ImageView 

    android:id=&quot;@&#43;id/main_image&quot; 

    android:layout_width=&quot;150dp&quot; 

    android:layout_height=&quot;150dp&quot; 

    android:layout_gravity=&quot;center_horizontal&quot; 

    android:layout_marginTop=&quot;30dp&quot;

/>

</LinearLayout>

加上权限:

?

1

2

<uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission

android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

标签: android

热门推荐