我正在尝试组合两个位图并将其设置为imageview并在我单击按钮时保存该位图,但是当我将位图组合到图像中时,它显示第二个位图比它的实际尺寸大我的代码是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBitmap(image1);
Bitmap drawingBitmap = Bitmap.createBitmap(parts.getWidth(),
parts.getHeight(),Bitmap.Config.ARGB_8888);
canvas = new Canvas(drawingBitmap);
canvas.drawBitmap(parts,0,0,null);
canvas.drawBitmap(touch,0,0,null);
bit=drawingBitmap;
public void getBitmap(View v) {
Bitmap bitmap= Bitmap.createBitmap(v.getWidth(),v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);
touch=bitmap;
}
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SaveImage save = new SaveImage();
save.Save(getApplicationContext(),bit);
}
});
}
此处部分是保留相机图片的位图, image1 是动态创建的图片视图。
SaveImage.java
package com.example.tattoodesign;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
public class SaveImage {
private Context TheThis;
private String NameOfFolder = "/PaintImage";
public String NameOfFile = null;
//private ArrayList<Bitmap> images = null;
public void Save(Context context,Bitmap ImageToSave){
TheThis = context;
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
File dir = new File(file_path);
NameOfFile="PaintImage"+CurrentDateAndTime+ ".jpg";
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir,NameOfFile);
if (file.exists ()) file.delete ();
try {
FileOutputStream fOut = new FileOutputStream(file);
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MakeSureFileWasCreatedThenMakeAvabile(file);
}
catch (FileNotFoundException e) {
UnableToSave();}
catch (IOException e){
UnableToSave();
}
}
private void MakeSureFileWasCreatedThenMakeAvabile(File file) {
MediaScannerConnection.scanFile(TheThis,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
private String getCurrentDateAndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
private void UnableToSave() {
Toast.makeText(TheThis, "Picture cannot to gallery", Toast.LENGTH_SHORT)
.show();
}
}
我该如何解决这个问题?
任何帮助都会受到赞赏。
感谢。
答案 0 :(得分:1)
图像不太可能显示为“大于实际尺寸”。 它的“实际尺寸”更可能不是你想象的那样。
请记住,表观尺寸与像素宽度/高度和dpi都有关。
我认为你应该考虑缩放其中一个图像以匹配另一个。 例如,请参阅Bitmap.createScaledBitmap