Android 開發筆記___映像視圖__簡單截屏

來源:互聯網
上載者:User

標籤:phi   weight   緩衝   .text   util   []   ack   news   context   

 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         <TextView14             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         <ImageView24             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     <LinearLayout32         android:layout_width="match_parent"33         android:layout_height="wrap_content"34         android:layout_marginTop="10dp"35         android:orientation="horizontal">36 37         <Button38             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         <Button47             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     @Override26     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     @Override47     public boolean onLongClick(View v) {48         if (v.getId() == R.id.btn_chat) {49             tv_capture.setText("");50         }51         return true;52     }53     @Override54     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         @Override73         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 }

 

Android 開發筆記___映像視圖__簡單截屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.