-1

这是我如何获得根视图:

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

它对我有用,可以截取 rootview 的屏幕截图。但我的问题是如何只imageView为此定义一个View?然后只有我可以采取screenshoot特定的图像视图。

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

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

活动类:

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>
4

1 回答 1

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>

现在无需查找 ID forImageView而不是查找 ID for LinearLayout

LinearLayout ll;

从今起onCreate()

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

现在在你的方法中

View rootView = ll.getRootView();
于 2014-05-22T07:02:02.737 回答