Ars*_*war 11 html android android-layout android-webview
我正在使用一个代码,通过使用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 {
        byt = Base64.decode(abc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    map1 = BitmapFactory.decodeByteArray(byt, 0, byt.length);
    // ***
    Bitmap map = combineImages(map1, map2);
    ByteArrayOutputStream bbb = new ByteArrayOutputStream();
    map.compress(Bitmap.CompressFormat.JPEG, 100, bbb);
    byte[] bit = bbb.toByteArray();
    String imgToString = Base64.encodeBytes(bit);
    String imgTag = "<img src='data:image/jpg;base64," + imgToString
            + "' align='left' bgcolor='ff0000'/>";
    WebView webView = (WebView) findViewById(R.id.storyView);
    webView.loadData(imgTag, "text/html", "utf-8");
    Drawable end = new BitmapDrawable(map);
    ImageView img3 = (ImageView) findViewById(R.id.ImageView03);
    img3.setImageBitmap(map);
}
public Bitmap combineImages(Bitmap c, Bitmap s) { 
    Bitmap cs = null;
    int width, height = 0;
    width = c.getWidth() + (s.getWidth() / 2);
    height = c.getHeight() + (s.getHeight() / 2);
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(c, 0f, 0f, null);
    comboImage.drawBitmap(s, c.getWidth() - (s.getWidth() / 2), c
            .getHeight()
            - (s.getHeight() / 2), null);
    return cs;
}
}
Mus*_*sis 25
该JPEG格式不支持Alpha透明度,这就是为什么当你转换你的原始图像的透明背景变成了黑色JPEG.
请改用PNG格式:
 map1.compress(Bitmap.CompressFormat.PNG, 100, baos); 
和
String imgTag = "<img src='data:image/png;base64," + imgToString               
    + "' align='left' bgcolor='ff0000'/>";   
| 归档时间: | 
 | 
| 查看次数: | 8504 次 | 
| 最近记录: |