Android 开发笔记___图像视图__简单截屏

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="300dp"
10         android:layout_marginTop="10dp"
11         android:orientation="horizontal">
12
13         <TextView
14             android:id="@+id/tv_capture"
15             android:layout_width="0dp"
16             android:layout_height="match_parent"
17             android:layout_weight="1"
18             android:background="#ffffff"
19             android:textColor="#000000"
20             android:scrollbars="vertical"
21             android:textSize="17sp" />
22
23         <ImageView
24             android:id="@+id/iv_capture"
25             android:layout_width="0dp"
26             android:layout_height="match_parent"
27             android:layout_weight="1" />
28
29     </LinearLayout>
30
31     <LinearLayout
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:layout_marginTop="10dp"
35         android:orientation="horizontal">
36
37         <Button
38             android:id="@+id/btn_chat"
39             android:layout_width="0dp"
40             android:layout_height="wrap_content"
41             android:layout_weight="1"
42             android:text="聊天"
43             android:textColor="#000000"
44             android:textSize="17sp" />
45
46         <Button
47             android:id="@+id/btn_capture"
48             android:layout_width="0dp"
49             android:layout_height="wrap_content"
50             android:layout_weight="1"
51             android:text="截图"
52             android:textColor="#000000"
53             android:textSize="17sp" />
54
55     </LinearLayout>
56
57 </LinearLayout>

java

 1 package com.example.alimjan.hello_world;
 2
 3 /**
 4  * Created by alimjan on 7/1/2017.
 5  */
 6
 7
 8 import android.content.Context;
 9 import android.content.Intent;
10 import android.graphics.Bitmap;
11         import android.os.Bundle;
12         import android.os.Handler;
13         import android.support.v7.app.AppCompatActivity;
14 import android.text.method.ScrollingMovementMethod;
15 import android.view.Gravity;
16 import android.view.View;
17         import android.widget.Button;
18         import android.widget.ImageView;
19         import android.widget.TextView;
20
21 public class class__2_3_3_2 extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
22     private TextView tv_capture;
23     private ImageView iv_capture;
24
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.code_2_3_3_2);
29         tv_capture = (TextView) findViewById(R.id.tv_capture);
30         iv_capture = (ImageView) findViewById(R.id.iv_capture);
31         tv_capture.setDrawingCacheEnabled(true);
32         tv_capture.setGravity(Gravity.LEFT|Gravity.BOTTOM);
33         tv_capture.setLines(17);
34         tv_capture.setMaxLines(17);
35         tv_capture.setMovementMethod(new ScrollingMovementMethod());
36         Button btn_chat = (Button) findViewById(R.id.btn_chat);
37         Button btn_capture = (Button) findViewById(R.id.btn_capture);
38         btn_chat.setOnClickListener(this);
39         btn_chat.setOnLongClickListener(this);
40         btn_capture.setOnClickListener(this);
41     }
42
43     private String[] mChatStr = { "你吃饭了吗?", "今天天气真好呀。",
44             "我中奖啦!", "我们去看电影吧。", "晚上干什么好呢?" };
45
46     @Override
47     public boolean onLongClick(View v) {
48         if (v.getId() == R.id.btn_chat) {
49             tv_capture.setText("");
50         }
51         return true;
52     }
53     @Override
54     public void onClick(View v) {
55         if (v.getId() == R.id.btn_chat) {
56             int random = (int)(Math.random()*10) % 5;
57             String newStr = String.format("%s\n%s %s",
58                     tv_capture.getText().toString(), DateUtil.getCurDateStr(), mChatStr[random]);
59             tv_capture.setText(newStr);
60         } else if (v.getId() == R.id.btn_capture) {
61             Bitmap bitmap = tv_capture.getDrawingCache();
62             iv_capture.setImageBitmap(bitmap);
63             // 注意这里在截图完毕后不能马上关闭绘图缓存,因为画面渲染需要时间,
64             // 如果立即关闭缓存,渲染画面就会找不到位图对象,会报错
65             // “java.lang.IllegalArgumentException: Cannot draw recycled bitmaps”。
66             mHandler.postDelayed(mResetCache, 200);
67         }
68     }
69
70     private Handler mHandler = new Handler();
71     private Runnable mResetCache = new Runnable() {
72         @Override
73         public void run() {
74             tv_capture.setDrawingCacheEnabled(false);
75             tv_capture.setDrawingCacheEnabled(true);
76         }
77     };
78
79     public static void startHome(Context mContext) {
80         Intent intent = new Intent(mContext, class__2_3_3_2.class);
81         mContext.startActivity(intent);
82     }
83 }
时间: 2024-08-10 02:17:09

Android 开发笔记___图像视图__简单截屏的相关文章

Android 开发笔记___图像视图

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:ori

Android 开发笔记___滚动视图__scroll view

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:l

Android 开发笔记___存储方式__共享参数__sharedprefences

Android 的数据存储方式有四种,这次是[共享参数__sharedprefences] 听起来挺别扭的,平时看到的app里面,当用户删除了一些软件以后下次安装,发现原来的设置还在,这种情况就是把一些用户的设置保存在手机里面的一个存储区域, 格式是XML key__Value 不方便保存关系比较复杂的数据 write 1 package com.example.alimjan.hello_world; 2 3 /** 4 * Created by alimjan on 7/4/2017. 5

Android 开发笔记___图像按钮__imageButton

IMAGEBUTTON 其实派生自image view,而不是派生自button.,image view拥有的属性和方法,image button 统统拥有,只是imagebutton有个默认的按钮外观. image button  只能显示图形 imagebutton 上面的图片可按比例拉伸 只能在背景显示一张图形,但分别在前景和背景显示两张图片,实现图片叠加的效果 在输入法无法输入的字符和特殊字体显示的字符串,就适合用imagebutton,   先切图再显示 要想在文字周围放图片可以用基于

Android 开发笔记___初级控件之实战__计算器

功能简单,实现并不难,对于初学者可以总和了解初级控件的基本使用. 用到的知识点如下: 线性布局 LinearLayout:整体界面是从上往下的,因此需要垂直方向的linearlayout:下面每行四个按钮,需要水平的linearlayout. 滚动视图 ScrollView    :虽然界面不宽也不高,以防万一,有可能会遇到屏幕特别小的手机,因此用一个垂直方向的scrollview. 文本视图 TextView      :上面标题就是一个textview,结果显示也是textview,但是更高

Android学习笔记-ImageView(图像视图)

原文来自:http://www.runoob.com/w3cnote/android-tutorial-imageview.html 本节引言: 本节介绍的UI基础控件是:ImageView(图像视图),见名知意,就是用来显示图像的一个View或者说控件! 官方API:ImageView;本节讲解的内容如下: ImageView的src属性和blackground的区别: adjustViewBounds设置图像缩放时是否按长宽比 scaleType设置缩放类型 最简单的绘制圆形的ImageVi

Android 开发笔记___复选框__checkbox

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:padding="10d

Android 开发笔记___时间选择器---timePicker

像datepicker一样,也有timepicker. 同样有timepickerdialog 所用到的方法还是一样,监听时间选择器的变化. 1 package com.example.alimjan.hello_world; 2 3 import java.util.Calendar; 4 5 /** 6 * Created by alimjan on 7/15/2017. 7 */ 8 9 import android.app.TimePickerDialog; 10 import andr

Android 开发笔记___登陆app

1 package com.example.alimjan.hello_world; 2 3 /** 4 * Created by alimjan on 7/4/2017. 5 */ 6 7 8 import android.content.Context; 9 import android.support.v7.app.AppCompatActivity; 10 import android.app.AlertDialog; 11 import android.content.DialogIn