如何从视图的屏幕截图中获得高质量的图片

时间:2015-06-10 06:29:42

标签: android

我想在里面看到视图(不是ImageView)的图片,但我得到的图片不清楚,当然原始图片很清楚,这里是我的代码

public static Bitmap getDrawBitmap(View designView){
        designView.destroyDrawingCache();
        designView.setDrawingCacheEnabled(true);
        designView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        return designView.getDrawingCache();
}

我试图扩展View,然后getDrawingCache(),但它没有工作,视图的大小没有改变

Bitmap bitmap=ImgUtils.getDrawBitmap(testBtn);
float w=1260f;
float h=1660f;

float x= w/(bitmap.getWidth()/DrawAttribute.density);
float y= h/(bitmap.getHeight()/DrawAttribute.density);

Log.e("x-y",x+"-"+y);
Bitmap result=Bitmap.createBitmap(1260,1660,Bitmap.Config.ARGB_8888);
Canvas offScreen = new Canvas(result);
Matrix matrix=new Matrix();
matrix.setScale(x,y);
offScreen.drawBitmap(bitmap, matrix, null);
//存储路径
OutputStream stream = null;
try {
     if (LiApplication.creatDir()) {
           stream = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/yc/img/"
                                        + "save.png");
                    } 
                } catch (FileNotFoundException e) {

                }

     Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG;
     result.compress(format, 100, stream);

1 个答案:

答案 0 :(得分:0)

这是一个用于拍摄整个视图的屏幕截图的演示代码。

 View v = (View) findViewById(R.id.myView);
  Bitmap mybitmap = Bitmap.createBitmap(
        v.getChildAt(0).getWidth(), 
        v.getChildAt(0).getHeight(), 
        Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(mybitmap);
  iv.getChildAt(0).draw(c);

  //remember here View may be any view including ScrollView or what ever it may be.
  saveBitmap(bitmap);

希望这会有所帮助。感谢