Android如何截取当前View 为图片

转自:http://dev.10086.cn/cmdn/supesite/?uid-103795-action-viewspace-itemid-6194


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Midlet extends Activity {
    Button bn;
    ImageView iv,v2;
    ImageButton ib;
    Bitmap bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bn = (Button) findViewById(R.id.Button01);
        iv = (ImageView) findViewById(R.id.ImageView01);
        v2 =(ImageView) findViewById(R.id.ImageView02);
        ib=(ImageButton) findViewById(R.id.ImageButton01);
        bn.setText("cancan");
        ib.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        View v1 = v2; //获取单个View
        // View v1 = v2.getRootView();//获取整个屏幕显示
        v1.setDrawingCacheEnabled(true);
        bitmap = v1.getDrawingCache();
        if (bitmap != null) {
            iv.setImageBitmap(bitmap);
         }
         else {
            iv.setImageResource(R.drawable.icon);
         }
      }    });
   }
}
/*************************************************************************************************/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<ImageView android:id="@+id/ImageView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" ></ImageView>

<ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageButton>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

</LinearLayout>

你可能感兴趣的:(xml,android,OS)