1.url转bitmap
public void returnBitMap(final String url, final CommonCallback<Bitmap> callback) {
new Thread(new Runnable() {
@Override
public void run() {
URL imageurl = null;
try {
imageurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
if (callback!=null){
callback.callback(null);
}
}
try {
HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//----圆形bitmap----
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int left = 0, top = 0, right = width, bottom = height;
float roundPx = height/2;
if (width > height) {
left = (width - height)/2;
top = 0;
right = left + height;
bottom = height;
} else if (height > width) {
left = 0;
top = (height - width)/2;
right = width;
bottom = top + width;
roundPx = width/2;
}
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//----圆形bitmap----
if (callback!=null){
callback.callback(output);
}
} catch (IOException e) {
e.printStackTrace();
if (callback!=null){
callback.callback(null);
}
}
}
}).start();
}
2.id转drawable
Drawable drawable = getResources().getDrawable(R.mipmap.xxx);
3.id转bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.xxx);
3.drawable转bitmap
(1). BitmapDrawable, FastBitmapDrawable 调用getBitmap
(2). Canvas绘图bitmap:
Canvas canvas = new Canvas(bitmap)
drawable.draw(canvas)
4. bitmap转drawable
Drawable drawable = new FastBitmapDrawable(bitmap);
5.url转drawable
InputStream is2 = null;
try {
is2 = (InputStream) new URL(url).getContent();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(is2, "src");
try {
is2.close();
} catch (IOException e) {
e.printStackTrace();
}
588

被折叠的 条评论
为什么被折叠?



