android-------截屏的两种方法

在android中截屏是一种很常用的功能,现在来实现一下android的截屏。

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/rootLayout"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/my_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="截屏方式二" >
    </Button>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/my_button"
        android:layout_centerHorizontal="true"
        android:text="截屏方式一" />

</RelativeLayout>


因为截屏得到的图片保存在sd卡上,因此要加上以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


第一种方法截屏:

 bt1=(Button) findViewById(R.id.button1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //Context context = getApplicationContext();
                    View rootView = findViewById(R.id.rootLayout);
                    
                    Bitmap newb = Bitmap.createBitmap( 320, 480, Config.ARGB_8888 );
                    
                    Canvas canvas = new Canvas(newb);
                    
                    rootView.draw(canvas);
                    
                    File file = new File(Environment.getExternalStorageDirectory().getPath()+ "/" + "1.png");
                    
                    FileOutputStream f = null;
                    try {
                            f = new FileOutputStream(file);
                    } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                    boolean b = newb.compress(Bitmap.CompressFormat.PNG, 100, f);
                    
                    if(b){
                            //截图成功
                    	Toast.makeText(MainActivity.this, "截图成功", 1).show();
                            
                    }
            }
    });

 

截图效果:

这种方法截屏是没有截取标题栏的,并且中间的白色部分根本就没显示出来,都是透明色的。



第二种方法截屏:

this.button = (Button) this.findViewById(R.id.my_button); 
        
        this.button.setOnClickListener(new Button.OnClickListener(){      
        	public void onClick(View v){ 
        		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
        		//String fname ="/sdcard/"+sdf.format(new Date()) + ".png"; 
        		String fname =Environment.getExternalStorageDirectory().getPath()+"/"+sdf.format(new Date()) + ".png";
        		
        		View view = v.getRootView(); 
        		view.setDrawingCacheEnabled(true); 
        		view.buildDrawingCache();   
        		Bitmap bitmap = view.getDrawingCache(); 
        		if(bitmap!= null){      
        			try{     
        				FileOutputStream  out = new FileOutputStream(fname);    
        				bitmap.compress(Bitmap.CompressFormat.PNG,100, out); 
        				Toast.makeText(MainActivity.this, "截图成功", 1).show();
        			}catch(Exception e) {  
        				e.printStackTrace();   
        			}        
        		}else{      
        			Toast.makeText(MainActivity.this, "截图失败", 1).show();
        		}      
        	}    
        	});


截图效果:

这种方法截屏把标题栏也截取进去了


运行代码后再sd卡上生成以下文件:

观察上面的生成的文件的大小,发现第一种方法生成的文件比较小(没有截取标题栏),若对内存要求比较高的可以选择第一种方法截图。

 

源代码下载地址:http://download.csdn.net/detail/dangnianmingyue_gg/9092983

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值