AndroidStudio常用的方法归纳(陆续更新)

图片缩放

//调节图片大小
public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h) {
   int src_w = bitmap.getWidth();
   int src_h = bitmap.getHeight();
   float scale_w = ((float) dst_w) / src_w;
   float scale_h = ((float) dst_h) / src_h;
   Matrix matrix = new Matrix();
   matrix.postScale(scale_w, scale_h);
   Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,
           true);
   return dstbmp;
}

获取网络图片

//获取网络图片
public static Bitmap getBitmap(String path) throws IOException {

    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setConnectTimeout(8000);
    conn.setRequestMethod("GET");
    if(conn.getResponseCode() == 200){
        InputStream inputStream = conn.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        return bitmap;
    }
    return null;
}
发布了4 篇原创文章 · 获赞 0 · 访问量 100

猜你喜欢

转载自blog.csdn.net/qq_45212775/article/details/104066190
今日推荐