zoukankan      html  css  js  c++  java
  • 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
    %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 }
  • 相关阅读:
    5分钟带你了解Kafka的技术架构
    聊聊我的知识体系
    你分得清楚Maven的聚合和继承吗?
    为抖音而生的多闪,如何获取抖音的用户数据?
    消息中间件系列第3讲:使用消息队列需要考虑的几个问题
    消息中间件系列第2讲:如何进行消息队列选型?
    消息中间件系列第1讲:为什么要用消息队列?
    JVM规范系列开篇:为什么要读JVM规范?
    安全编码实践之三:身份验证和会话管理防御
    安全编码实践之二:跨站脚本攻击防御
  • 原文地址:https://www.cnblogs.com/alimjan/p/7101719.html
Copyright © 2011-2022 走看看