我需要将视图转换为位图以预览我的视图并将其保存为图像.我尝试使用以下代码,但它创建了一个空白图像.我无法理解我犯了什么错误.
View viewToBeConverted; Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
savephoto(“f1”, viewBitmap);
//// public void savephoto(String filename,Bitmap bit)
{
File newFile = new File(Environment.getExternalStorageDirectory() + Picture_Card/"+ filename+ ".PNG");
try
{
newFile.createNewFile();
try
{
FileOutputStream pdfFile = new FileOutputStream(newFile); Bitmap bm = bit; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100, baos); byte[] bytes = baos.toByteArray();
pdfFile.write(bytes);
pdfFile.close();
}
catch (FileNotFoundException e)
{ //
}
} catch (IOException e)
{ //
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用一个代码,通过使用canvas将图像组合成1.我向ImageView显示图像看起来很好.但是当我尝试向WebView显示它时,它会向该图像显示背景黑色.我尝试更改HTML中的背景颜色,但它不会改变颜色.或透明.有人可以帮忙吗?结果在这里上面的图像在ImageView中,下面是在WebView中.
public class MyBimapTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img1 = (ImageView) findViewById(R.id.ImageView01);
img1.setVisibility(View.INVISIBLE);
Drawable dra1 = img1.getDrawable();
Bitmap map1 = ((BitmapDrawable) dra1).getBitmap();
ImageView img2 = (ImageView) findViewById(R.id.ImageView02);
img2.setVisibility(View.INVISIBLE);
Drawable dra2 = img2.getDrawable();
Bitmap map2 = ((BitmapDrawable) dra2).getBitmap();
// ***
ByteArrayOutputStream baos = new ByteArrayOutputStream();
map1.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String abc = Base64.encodeBytes(b);
byte[] byt = null;
try { …Run Code Online (Sandbox Code Playgroud)