我们正在尝试创建一个应用,让用户上传图片,在上面写文字,然后用文字保存新图片。
我们当前的实现是使用ImageView来保存图像,然后使用TextViews在其上面进行书写。
整体保存图像的最佳方法是什么?以下是我们当前的代码。
感谢您的帮助! :)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generator);
final EditText topTextInput = (EditText)findViewById(R.id.editTopText);
final EditText bottomTextInput = (EditText)findViewById(R.id.editBottomText);
final TextView topTextView = (TextView)findViewById(R.id.topText);
final TextView bottomTextView = (TextView)findViewById(R.id.bottomText);
imageView = (ImageView)findViewById(R.id.imageView2);
Button pickImage = (Button) findViewById(R.id.button2);
pickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
topTextInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
topTextView.setText(topTextInput.getText() + "");
}
@Override
public void afterTextChanged(Editable editable) {
}
});
bottomTextInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
bottomTextView.setText(bottomTextInput.getText() + "");
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
使用它:
public Bitmap takeScreenshot() {
View rootView = getView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap b1 = Bitmap.createBitmap(rootView.getDrawingCache(true));
rootView.setDrawingCacheEnabled(false); // clear drawing cache
return b1;
}
并将位图保存到图像:
public void saveBitmap(Bitmap bitmap) {
if(bitmap==null){
return;
}
File imagePath1 = new File(savePath,"screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath1);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}