如何截取imageView的截图?

时间:2014-05-22 05:13:10

标签: android imageview rootview

这是获取root视图的方法:

View rootView = findViewById(android.R.id.content).getRootView();

我可以截取rootview的截图。但我的问题是如何仅针对此imageView定义View?然后我只能拍摄特定图像视图的screenshoot

注意: View rootView = findViewById(android.R.id.**imageview**).getRootView();导致错误。

更新:类和xml文件,可以拍摄特定imageView的屏幕截图。解决后更新!

活动类:

public class SStest extends Activity {

Button button1;
LinearLayout ll2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sstest);

    Button button1= (Button) findViewById(R.id.button1);
    ll2 = (LinearLayout )findViewById(R.id.ll2);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

                    Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);

        }
    });


}
public Bitmap takeScreenshot() {

    View rootView=getWindow().getDecorView().findViewById(R.id.ll2);
rootView.setDrawingCacheEnabled(true);     
return rootView.getDrawingCache();

    }

public void saveBitmap(Bitmap bitmap) {

String root = Environment.getExternalStorageDirectory().toString();
    File newDir = new File(root + "/MapCards");    
    newDir.mkdirs();
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);
    String fotoname = "Photo-"+ n +".jpg";
    File file = new File (newDir, fotoname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
}

.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/stainbck"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="TextView"
    android:textSize="15dp" />

<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"           
        android:background="@drawable/ic_launcher" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您需要在此处更改

View rootView = findViewById(android.R.id.content).getRootView();

View rootView = findViewById(R.id.imageview).getRootView();

你可以做的另一件事如下。

ImageView iv;

现在在onCrate()方法

上找到它的ID
iv = (ImageView)findViewById(R.id.imageview);

现在在你的方法

View rootView = iv.getRootView();

<强> 更新:

更改您的xml文件,如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="TextView"
        android:textSize="15dp" />

    <LinearLayout
        android:id="@+id/rootView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

现在无需查找ImageView的ID,而无需找到LinearLayout的ID。

LinearLayout ll;

现在onCreate()

ll = (LinearLayout )findViewById(R.id.rootView);

现在在您的方法中

View rootView = ll.getRootView();