在android中显示图像也有好多种方法,本例中使用BitmapFactory.decodeResource获得资源图像文件,然后用Canvas画布显示位图。由于通过自定义View代替布局文件,故而不需要使用布局文件。

  

一、程序文件

  1、新建BitmapView.java文件。

  在“src/com.genwoxue.bitmap”位置,新建BitmapView.java文件。

  

Android Bitmap生成图片并生产URI android创建bitmap图片_Bitmap

  BitmapView.java文件源代码如下:

package com.genwoxue.bitmap;

import android.content.Context;  
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import android.view.View;


public class BitmapView extends View{
	
	Paint paint=null;
	Resources src=null;
	Bitmap bmp=null;
	
	public BitmapView(Context context) {   
		super(context);   
		
		/*----------------------------------------------
		 * 建议尽量把初始化画笔、资源、位图不放在onDraw方法中,这是因为:
		 * onDraw经常会运行 到的,不要在里面new对象,越少越好,不然很浪费内存
		 *--------------------------------------------*/
		
		//获取画笔paint,初始化画笔
		paint=new Paint();
		//获取资源src
		src=getResources();
		// 获取位图
	    bmp=BitmapFactory.decodeResource(src, R.drawable.ic_launcher);
	}  
	
	@Override
	protected void onDraw(Canvas canvas){
		
		String info=null;
		int posX=0;
		int posY=0;
		
		//设置画布背景为灰色
		canvas.drawColor(Color.GRAY);
		//显示位图
		canvas.drawBitmap(bmp,220,220,paint);
		
		//设置画笔为白色
		paint.setColor(Color.WHITE);
		//字体大小为18
		paint.setTextSize(18);
		info="图片高度:"+bmp.getHeight()+",图片宽度:"+bmp.getWidth();
		posX=135;
		posY=bmp.getWidth()+260;
		
		//在posX与posY处显示图片高度与宽度信息
		canvas.drawText(info, posX,posY, paint);
	}

}

 

 2、打开MainActivity.java主文件

  打开“src/com.genwoxue.bitmap/MainActivity.java”文件。

  MainActivity.java文件源代码如下:

package com.genwoxue.bitmap;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//实例化GraphicsView
		BitmapView gv = new BitmapView(this); 
		
		/* 在以前我们通过setContentView(R.layout.activity_main)显示布局文件
		 * 本例中使用GraphicsView对象gv代替以前布局文件
		 */
		setContentView(gv);
	}

}

二、运行结果

 

  

Android Bitmap生成图片并生产URI android创建bitmap图片_android_02

 

附:android Bitmap用法总结





1、Drawable → Bitmap

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap

.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

// canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

 

2、从资源中获取Bitmap

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

 

3、Bitmap → byte[]

private byte[] Bitmap2Bytes(Bitmap bm){

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

 

4、byte[] → Bitmap

private Bitmap Bytes2Bimap(byte[] b){

if(b.length!=0){

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

else {

return null;

}

}

 

5、保存bitmap

static boolean saveBitmap2file(Bitmap bmp,String filename){

CompressFormat format= Bitmap.CompressFormat.JPEG;

int quality = 100;

OutputStream stream = null;

try {

stream = new FileOutputStream("/sdcard/" + filename);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return bmp.compress(format, quality, stream);

}

 

6、将图片按自己的要求缩放

// 图片源

Bitmap bm = BitmapFactory.decodeStream(getResources()

.openRawResource(R.drawable.dog));

// 获得图片的宽高

int width = bm.getWidth();

int height = bm.getHeight();

// 设置想要的大小

int newWidth = 320;

int newHeight = 480;

// 计算缩放比例

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// 取得想要缩放的matrix参数

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

// 得到新的图片

Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,

true);

// 放在画布上

canvas.drawBitmap(newbm, 0, 0, paint);

 

7、bitmap的用法小结

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

option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出

Bitmap bm = BitmapFactory.decodeFile("",option);//文件流

URL url = new URL("");

InputStream is = url.openStream();

Bitmap bm = BitmapFactory.decodeStream(is);

android:scaleType:

android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /

android:scaleType值的意义区别:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分

显示

CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长

(宽)

CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片

长/宽等于或小于View的长/宽

FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示

MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

//放大缩小图片

public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidht = ((float)w / width);

float scaleHeight = ((float)h / height);

matrix.postScale(scaleWidht, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,

true);

return newbmp;

}

//将Drawable转化为Bitmap

public static Bitmap drawableToBitmap(Drawable drawable){

int width = drawable.getIntrinsicWidth();

int height = drawable.getIntrinsicHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height,

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0,0,width,height);

drawable.draw(canvas);

return bitmap;

}

//获得圆角图片的方法

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap

.getHeight(), Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

final 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(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

}

//获得带倒影的图片方法

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){

final int reflectionGap = 4;

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap,

0, height/2, width, height/2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),

Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(bitmap, 0, 0, null);

Paint deafalutPaint = new Paint();

canvas.drawRect(0, height,width,height + reflectionGap,

deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();

LinearGradient shader = new LinearGradient(0,

bitmap.getHeight(), 0, bitmapWithReflection.getHeight()

+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()

+ reflectionGap, paint);

return bitmapWithReflection;

}

}