我有两个Linearlayouts
(A和B),其可见性在我的xml布局中互相排斥。当A可见时,B是GONE(android:visibility="gone"
),反之亦然。两个LinearLayout
都包含一个ImageView
。我有一个按钮,截取当前屏幕的截图。
最初A是可见的,B是Gone。按下上面的按钮应该使B可见并且A消失,然后截取屏幕截图并将生成的图像保存在SD卡中。我没有在图像中看到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
我的错是什么?请回复。
答案 0 :(得分:0)
已编辑&gt;&gt;&gt;&gt;&gt;测试此工作正常确保您的视图适合屏幕
if(first_set.getVisibility()!= View.VISIBLE){
zoomed_image.setVisibility(View.GONE);
first_set.setVisibility(View.VISIBLE);
}else if(zoomed_image.getVisibility() != View.VISIBLE){
first_set.setVisibility(View.GONE);
zoomed_image.setVisibility(View.VISIBLE);
}
答案 1 :(得分:0)
use this one
if(first_set.getVisibility()==View.GONE){
first_set.setVisibility(View.VISIBLE);
zoomed_image.setVisibility(View.GONE);
}else {
// if(zoomed_image.getVisibility()==View.GONE){
zoomed_image.setVisibility(View.VISIBLE);
first_set.setVisibility(View.GONE);
}