【发布时间】:2014-04-01 08:51:52
【问题描述】:
我有两个 Linearlayouts(A 和 B),它们的可见性在我的 xml 布局中相互排斥。当 A 可见时,B 消失 (android:visibility="gone"),反之亦然。两个LinearLayouts 都包含一个ImageView。我有一个按钮可以截取当前屏幕的屏幕截图。
最初 A 是可见的,而 B 已经消失了。按上面的按钮应该使 B 可见而 A 消失,然后截屏并将生成的图像保存在 sdcard 中。我没有在图像中看到 B。 A 仍然可见。
android代码如下:
public class AScreenshotActivity extends Activity {
LinearLayout zoomed_image,first_set;
Button screenshot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ascreenshot);
zoomed_image=(LinearLayout)findViewById(R.id.zoomed_image);
first_set=(LinearLayout)findViewById(R.id.first_set);
screenshot=(Button)findViewById(R.id.button1);
screenshot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(first_set.getVisibility()==View.GONE){
first_set.setVisibility(View.VISIBLE);
zoomed_image.setVisibility(View.GONE);
}
if(zoomed_image.getVisibility()==View.GONE){
zoomed_image.setVisibility(View.VISIBLE);
first_set.setVisibility(View.GONE);
}
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
}
private void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory()+ File.separator + "screenshot.png");
FileOutputStream fos;
if(imagePath.exists()){
imagePath.delete();
}
if(!imagePath.exists()) {
try {
imagePath.createNewFile();
Toast.makeText(this, "CAPTURING SCREENSHOT !", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("FILE INPUT FOR SCREENSHOT NOT CREATED", e.getMessage(), e);
}
}
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 20, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("FILE NOT FOUND - SCREENSHOT", e.getMessage(), e);
} catch (IOException e) {
Log.e("FILE INPUT FOR SCREENSHOT NOT FOUND", e.getMessage(), e);
}
}
private Bitmap takeScreenshot() {
//View rootView = context.findViewById(android.R.id.content).getRootView();
View rootView = getWindow().getDecorView().getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
}
The xml layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/zoomed_image"
>
<ImageView
android:id="@+id/imgzoom_large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pack01_11"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone"
android:id="@+id/first_set"
>
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pack01_12"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Button screenshot"
></Button>
</LinearLayout>
What is the mistake?? Please help. Thanks A LOT
我的错误是什么?请回复。
【问题讨论】: