【问题标题】:Android Screenshot programming errorAndroid截图编程错误
【发布时间】: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

我的错误是什么?请回复。

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    已编辑 >>>>> 测试过 工作正常 确保您的视图适合屏幕

     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);
    
                    }
    

    【讨论】:

    • 没有。添加上面的代码会导致两个线性布局都不可见的屏幕截图。只看到按钮。
    • GONE 表示完全隐藏,就好像没有添加视图一样。最初哪个视图可见,哪个视图不可见。
    • first_set 已消失,并且 zoomed_image 可见。请看xml布局代码。
    • 感觉这行 View rootView = getWindow().getDecorView().getRootView();正在制造问题。
    • @GaneshHegde 检查编辑后的代码是否正常工作。我使用了 2 张图像,所以我将线性布局的布局高度和宽度更改为 wrap_content。它的变化正确。
    【解决方案2】:
    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);
                }
    

    【讨论】:

    • 没有。添加上述代码会导致两个线性布局都不可见的屏幕截图。只看到按钮。这个答案也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多