您的位置:首页 > 本地本地

android中bitmap的用法

admin2024-07-27人已围观

一、android中bitmap的用法

参见代码:

/////////////////////////////////////////////////////////////////////////;

// Save canvas to file.

// Get the width and height of screen.

DisplayMetrics display = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(display);

int width = display.widthPixels;

int height = display.heightPixels;

// Create bitmap.

Bitmap bt = Bitmap.createBitmap(width, height, Config.ARGB_8888);

// Create canvas.

Canvas canvas = new Canvas();

canvas.setBitmap(bt);

Paint paint = new Paint();

// Draw a oval.

int left = width>>2;

int right = left*3;

int top = height>>2;

int bottom = top*3;

paint.setStyle(Style.STROKE);

canvas.drawOval(new RectF(left,top,right,bottom), paint);

// Draw text.

paint.setTextAlign(Align.CENTER);

paint.setColor(Color.RED);

canvas.drawText(Hi,man!, width>>1, height>>1,paint);

// Save canvas.

canvas.save(Canvas.ALL_SAVE_FLAG);

canvas.restore();

//Save canvas to file.

File file = new File(getFilesDir(), hiMan.png);

FileOutputStream fos = null;

try {

fos = new FileOutputStream(file);

bt.compress(Bitmap.CompressFormat.PNG, 50, fos);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

二、android中怎么将bitmap转为file

android中怎么把imageview转化成bitmap啊 [问题点数:30分,结帖人gengxinzhou112]...Decode a file path into a bitmap. static Bitmap decodeFile(St

三、android 读取Bitmap的几种方式

想读取本地项目里的资源图片,但又不能用到R文件。查了很多资料终于找到了。

现总结以下几种读取Bitmap的方法。

1.以文件流的方式,假设在sdcard下有

test.png图片

FileInputStream fis = new

FileInputStream(/sdcard/test.png);

Bitmap bitmap=BitmapFactory.decodeStream(fis);

2. 以R文件的方式,假设

res/drawable下有 test.jpg文件Bitmapbitmap =BitmapFactory.decodeResource(this.getContext().getResources(),

R.drawable.test);

3.以ResourceStream的方式,但不用到R文件。

Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(“/res/drawable/test.png”));

图片名就可以读取到 Bitmap啦。

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 2;

//图片宽高都为原来的二分之一,即图片为原来的四分一

//以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。

很赞哦! ()

上一篇:安卓手机用什么播放器可以播放f4v的本地软件?'>谈谈自媒体、新媒体和融媒体

下一篇:返回列表'>返回列表

随机图文