【问题标题】:Render Bitmap for a given Rect为给定的 Rect 渲染位图
【发布时间】:2014-04-29 10:51:30
【问题描述】:

我正在试图弄清楚如何构建一个与给定Rect 大小相同的Bitmap。我可以独立生成位图(如下所示),但我对 Android SDK 不够熟悉,不知道要查看哪些库以及这两个类如何关联。

矩形右/上/左/下属性如何与位图的大小相关?这些是像素吗?

public Bitmap renderFragment(Rect rect){
    // step 1: collect underpants
    Bitmap fullBitmap = getBitmap();

    // step 2: ...
    Bitmap fragment = /* do some magic */

    // step 3: profit
    return fragment;
}

private Bitmap getBitmap()
{
    Bitmap bitmap;
    String imageUrl = "http://developer.android.com/design/media/creative_vision_main.png";
    bitmap = getBitmapFromURL(imageUrl);
    return bitmap;
}


public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

【问题讨论】:

    标签: android bitmap rendering rect


    【解决方案1】:

    如果您有原始位图,请使用原始位图创建一个新位图。首先你应该创建位图的边界。

       Bitmap.createBitmap(Bitmap orginalBitmap, int x, int y, int width, int height) // This is part of orginal bitmap
       Bitmap.createScaledBitmap(Bitmap orginalBitmap, int destWidth, int destHeight, boolean filter ) // Create new bitmap 
    

    小心内存不足...

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多