<?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:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <TextView
android:id="@+id/tv_capture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:textColor="#000000"
android:scrollbars="vertical"
android:textSize="17sp" /> <ImageView
android:id="@+id/iv_capture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/btn_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="聊天"
android:textColor="#000000"
android:textSize="17sp" /> <Button
android:id="@+id/btn_capture"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="截图"
android:textColor="#000000"
android:textSize="17sp" /> </LinearLayout> </LinearLayout>
java
package com.example.alimjan.hello_world; /**
* Created by alimjan on 7/1/2017.
*/ import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public class class__2_3_3_2 extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
private TextView tv_capture;
private ImageView iv_capture; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_2_3_3_2);
tv_capture = (TextView) findViewById(R.id.tv_capture);
iv_capture = (ImageView) findViewById(R.id.iv_capture);
tv_capture.setDrawingCacheEnabled(true);
tv_capture.setGravity(Gravity.LEFT|Gravity.BOTTOM);
tv_capture.setLines(17);
tv_capture.setMaxLines(17);
tv_capture.setMovementMethod(new ScrollingMovementMethod());
Button btn_chat = (Button) findViewById(R.id.btn_chat);
Button btn_capture = (Button) findViewById(R.id.btn_capture);
btn_chat.setOnClickListener(this);
btn_chat.setOnLongClickListener(this);
btn_capture.setOnClickListener(this);
} private String[] mChatStr = { "你吃饭了吗?", "今天天气真好呀。",
"我中奖啦!", "我们去看电影吧。", "晚上干什么好呢?" }; @Override
public boolean onLongClick(View v) {
if (v.getId() == R.id.btn_chat) {
tv_capture.setText("");
}
return true;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_chat) {
int random = (int)(Math.random()*10) % 5;
String newStr = String.format("%s\n%s %s",
tv_capture.getText().toString(), DateUtil.getCurDateStr(), mChatStr[random]);
tv_capture.setText(newStr);
} else if (v.getId() == R.id.btn_capture) {
Bitmap bitmap = tv_capture.getDrawingCache();
iv_capture.setImageBitmap(bitmap);
// 注意这里在截图完毕后不能马上关闭绘图缓存,因为画面渲染需要时间,
// 如果立即关闭缓存,渲染画面就会找不到位图对象,会报错
// “java.lang.IllegalArgumentException: Cannot draw recycled bitmaps”。
mHandler.postDelayed(mResetCache, 200);
}
} private Handler mHandler = new Handler();
private Runnable mResetCache = new Runnable() {
@Override
public void run() {
tv_capture.setDrawingCacheEnabled(false);
tv_capture.setDrawingCacheEnabled(true);
}
}; public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class__2_3_3_2.class);
mContext.startActivity(intent);
}
}