Android 使屏幕截图代码支持多种屏幕大小
我有一个类,我采取的布局截图,并存储为JPEG文件…我的问题是,图像大小随屏幕分辨率。。。。例如,如果我使用中分辨率手机,图像大小约为50-60kb,但对于高分辨率手机,图像大小高达1.5mbs……那么,有没有办法保持图像大小恒定,而不受屏幕大小或分辨率的影响Android 使屏幕截图代码支持多种屏幕大小,android,Android,我有一个类,我采取的布局截图,并存储为JPEG文件…我的问题是,图像大小随屏幕分辨率。。。。例如,如果我使用中分辨率手机,图像大小约为50-60kb,但对于高分辨率手机,图像大小高达1.5mbs……那么,有没有办法保持图像大小恒定,而不受屏幕大小或分辨率的影响 public class abc extends Activity { View content; String fileName, fname; static File file; static Stri
public class abc extends Activity {
View content;
String fileName, fname;
static File file;
static String RECE;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rece);
ImageView iv = (ImageView) findViewById(R.id.Ivsignature);
iv.setImageBitmap(Signature.sign);
Initialize();
content = findViewById(R.id.Rlrece);
ViewTreeObserver vto = content.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
public void onGlobalLayout() {
content.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
getScreen();
}
});
}
private void getScreen() {
View view = content;
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String myPath = Environment.getExternalStorageDirectory() + "/Rece";
File myDir = new File(myPath);
try {
myDir.mkdirs();
} catch (Exception e) {
}
fname = fileName + ".jpg";
file = new File(myDir, fname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 100, fos);
// Toast.makeText(getApplicationContext(), fname + " saved",
// Toast.LENGTH_LONG).show();
Intent my = new Intent(getApplicationContext(),
DialogActivity.class);
my.putExtra("Rsystrace", DialogActivity.Systrace);
startActivityForResult(my, 1);
} catch (Throwable ex) {
Toast.makeText(getApplicationContext(),
"error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
当你做屏幕截图的时候,你当然会有屏幕大小相关的图像(图像的分辨率是不同的,因为屏幕是不同的) 您可以根据需要压缩或调整位图大小,并在调整到固定大小后-文件大小将固定
另外,我建议您使用除JPEG之外的PNG。您可以创建一个对象BitmapFactory.Options(),并在给定比例因子的情况下在SampleSize中设置属性(您需要计算)。 然后,您可以使用BitmapFactory.decodeFile()中的类似功能以所需大小(基于inSampleSize)拍摄图像 --编辑-- 在“photoPath”中使用磁盘中图像的示例:
我正在拍摄屏幕截图并将其保存为文件…我希望文件大小保持不变..如果我将图像放入固定位图,然后保存文件,会怎么样???有什么方法可以这样做吗?检查此项
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
BitmapFactory.decodeFile(photoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(newWidth/width, newHeigth/heigth);
bmOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, bmOptions);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
FileOutputStream fos;
fos = new FileOutputStream(photoPath);
fos.write(bytes.toByteArray());
fos.close();