Bitmap (android.graphics.Bitmap)

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。

Bitmap类:

public boolean  compress   ( Bitmap.CompressFormat  format, int quality, OutputStream  stream)     压缩:

将位图的压缩到指定的OutputStream。如果返回true,位图可以通过传递一个相应的InputStream BitmapFactory.decodeStream(重建)。

format: 压缩图像的格式

quality: 0-100。 0含义压缩为小尺寸,100压缩的意思为最大质量。(PNG是无损的,会忽略品质设定 )

stream: OutputStream中写入压缩数据。

return: 是否成功压缩到指定的流。

 
 

public void recycle()—— 回收位图占用的内存空间,把位图标记为 Dead

public final boolean isRecycled() —— 判断位图内存是否已释放

public final int getWidth()—— 获取位图的宽度

public final int getHeight()—— 获取位图的高度

public final boolean isMutable()—— 图片是否可修改

public int getScaledWidth(Canvas canvas)—— 获取指定密度转换后的图像的宽度

public int getScaledHeight(Canvas canvas)—— 获取指定密度转换后的图像的高度

public boolean compress(CompressFormat format, int quality, OutputStreamstream)—— 按指定的图片格式以及画质,将图片转换为输出流。

format : Bitmap.CompressFormat.PNG 或 Bitmap.CompressFormat.JPEG

quality :画质, 0-100.0 表示最低画质压缩, 100 以最高画质压缩。对于 PNG 等无损格式的图片,会忽略此项设置。

常用的静态方法:

public staticBitmap createBitmap(Bitmap src)  ——以 src 为原图生成不可变得新图像

public staticBitmap createScaledBitmap(Bitmap src, int dstWidth,

int dstHeight, boolean filter) ——以 src 为原图,创建新的图像,指定新图像的高宽以及是否变。

public staticBitmap createBitmap(int width, int height, Config config) ——创建指定格式、大小的位图

public staticBitmap createBitmap(Bitmap source, int x, int y, int width, int height) 以source 为原图,创建新的图片,指定起始坐标以及新图像的高宽。

public staticBitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrixm, boolean filter)

BitmapFactory 工厂类:

Option 参数类:

public boolean inJustDecodeBounds ——如果设置为 true ,不获取图片,不分配内存,但会返回图片的高宽度信息。

public int inSampleSize ——图片缩放的倍数。如果设为 4 ,则宽和高都为原来的1/4 ,则图是原来的 1/16 。

public int outWidth ——获取图片的宽度值

public int outHeight ——获取图片的高度值

public int inDensity——用于位图的像素压缩比

public int inTargetDensity ——用于目标位图的像素压缩比(要生成的位图)

public boolean inScaled ——设置为 true 时进行图片压缩,从 inDensity 到inTargetDensity 。

读取一个文件路径得到一个位图。如果指定文件为空或者不能解码成文件,则返回NULL 。

public staticBitmap decodeFile(String pathName, Options opts)

public staticBitmap decodeFile(String pathName)

读取一个资源文件得到一个位图。如果位图数据不能被解码,或者 opts 参数只请求大小信息时,则返回 NuLL 。(即当 Options.inJustDecodeBounds=true, 只请求图片的大小信息。)

public staticBitmap decodeResource(Resources res, int id)

public staticBitmap decodeResource(Resources res, int id, Options opts)

从输入流中解码位图

public static Bitmap decodeStream(InputStreamis)

从字节数组中解码生成不可变的位图

public staticBitmap decodeByteArray(byte[] data, int offset, int length)

BitmapDrawable 类:

继承于 Drawable ,你可以从文件路径、输入流、 XML 文件以及 Bitmap 中创建。

常用的构造函数:

Resourcesres=getResources();// 获取资源

publicBitmapDrawable(Resources res) ——创建一个空的 drawable 。( Response用来指定初始时所用的像素密度)替代 public BitmapDrawable() 方法(此方法不处理像素密度)

publicBitmapDrawable(Resources res, Bitmap bitmap)——从位图创建绘制

publicBitmapDrawable(Resources res, String filepath)——通过打开agiven文件路径和位图解码创建绘制

publicBitmapDrawable(Resources res, java.io.InputStream is)——创建一个可绘制从给定的输入流bydecoding位图。

 

使用BitmapFactory解码资源

 
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView; public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView = (ImageView)findViewById(R.id.image3); imgView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon) );
}
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"> <ImageView android:id="@+id/image1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon"
/> <ImageView android:id="@+id/image2"
android:layout_width="125dip" android:layout_height="25dip"
android:src="#555555"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"> <ImageView android:id="@+id/image3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/> <ImageView android:id="@+id/image4"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon"
android:scaleType="centerInside"
android:maxWidth="35dip" android:maxHeight="50dip"
/> </LinearLayout>
</LinearLayout>
捕获和保存位图 
import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View; public class Test extends Activity {
Uri myPicture = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void captureImage(View view)
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0 && resultCode==Activity.RESULT_OK)
{
Bitmap myBitmap = (Bitmap) data.getExtras().get("data");
}
}
}
 

位图大小 

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.widget.ImageView; public class Test extends Activity {
final static int CAMERA_RESULT = 0;
ImageView imv;
String imageFilePath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/a.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_RESULT);
} protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
imv = (ImageView) findViewById(R.id.ReturnedImageView);
Display currentDisplay = getWindowManager().getDefaultDisplay();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,
bmpFactoryOptions); bmpFactoryOptions.inSampleSize = 2;
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
imv.setImageBitmap(bmp);
}
}
}

//layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/ReturnedImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</LinearLayout>

在画布(Canvas)上绘制位图

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class Test extends Activity implements OnClickListener {
ImageView chosenImageView;
Button choosePicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton); choosePicture.setOnClickListener(this);
} public void onClick(View v) {
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);
} protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) {
Uri imageFileUri = intent.getData();
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inSampleSize = 2; bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
canvas.drawBitmap(bmp, 0, 0, paint);
ImageView alteredImageView = (ImageView) this
.findViewById(R.id.AlteredImageView);
alteredImageView.setImageBitmap(alteredBitmap);
chosenImageView.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
Log.v("ERROR", e.toString());
}
}
}
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose Picture" android:id="@+id/ChoosePictureButton"/> <ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ChosenImageView"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AlteredImageView"></ImageView>
</LinearLayout>
 
 

Bitmap.createBitmap

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class Test extends Activity implements OnClickListener {
Button choosePicture1, choosePicture2;
ImageView compositeImageView;
Bitmap bmp1, bmp2;
Canvas canvas;
Paint paint; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); compositeImageView = (ImageView) this
.findViewById(R.id.CompositeImageView); choosePicture1 = (Button) this.findViewById(R.id.ChoosePictureButton1);
choosePicture2 = (Button) this.findViewById(R.id.ChoosePictureButton2); choosePicture1.setOnClickListener(this);
choosePicture2.setOnClickListener(this);
} public void onClick(View v) {
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 1);
} protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) {
Uri imageFileUri = intent.getData();
bmp1 = loadBitmap(imageFileUri);
Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),
bmp1.getHeight(), bmp1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bmp1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(bmp2, 0, 0, paint);
compositeImageView.setImageBitmap(drawingBitmap);
}
} private Bitmap loadBitmap(Uri imageFileUri) {
Display currentDisplay = getWindowManager().getDefaultDisplay(); float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight(); Bitmap returnBmp = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_4444);
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
returnBmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inSampleSize = 2;
bmpFactoryOptions.inJustDecodeBounds = false;
returnBmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri), null, bmpFactoryOptions);
} catch (Exception e) {
Log.v("ERROR", e.toString());
}
return returnBmp;
}
}

在画布上绘制位图(使用矩阵)

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class Test extends Activity implements OnClickListener { ImageView chosenImageView;
Button choosePicture; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton); choosePicture.setOnClickListener(this);
} public void onClick(View v) {
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);
} protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) {
Uri imageFileUri = intent.getData(); Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight() / 2 - 100;
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions); bmpFactoryOptions.inSampleSize = 2; bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions); Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
canvas.drawBitmap(bmp, 0, 0, paint);
Matrix matrix = new Matrix();
matrix.setValues(new float[] { 1, .5f, 0, 0, 1, 0, 0, 0, 1 });
canvas.drawBitmap(bmp, matrix, paint); ImageView alteredImageView = (ImageView) this.findViewById(R.id.AlteredImageView);
alteredImageView.setImageBitmap(alteredBitmap); chosenImageView.setImageBitmap(bmp); } catch (Exception e) {
Log.v("ERROR", e.toString());
}
}
}
}
//main.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose Picture" android:id="@+id/ChoosePictureButton"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ChosenImageView"></ImageView>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/AlteredImageView"></ImageView>
</LinearLayout>

创建一个位图来画 

 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ImageView; public class Test extends Activity {
ImageView drawingImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap); Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setTextSize(20);
paint.setTypeface(Typeface.DEFAULT);
Path p = new Path();
p.moveTo(20, 20);
p.lineTo(100, 150);
p.lineTo(200, 220);
canvas.drawTextOnPath("this is a test", p, 0, 0, paint);
}
}
 
 
 

加载位图和画

import java.io.IOException;
import java.io.InputStream; import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager; public class Test extends Activity {
class RenderView extends View {
Bitmap bitmap1;
Bitmap bitmap2;
Rect dst = new Rect(); public RenderView(Context context) {
super(context);
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("a.png");
bitmap1 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("Text",""+bitmap1.getConfig());
inputStream = assetManager.open("b.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bitmap2 = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
Log.d("BitmapText","" + bitmap2.getConfig());
} catch (IOException e) { }
} protected void onDraw(Canvas canvas) {
dst.set(50, 50, 350, 350);
canvas.drawBitmap(bitmap1, null, dst, null);
canvas.drawBitmap(bitmap2, 100, 100, null);
invalidate();
}
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
}
 
 
 

总结一下:

1.  从资源中获取位图

可以使用 BitmapDrawable 或者 BitmapFactory 来获取资源中的位图。

当然,首先需要获取资源:  Resources res=getResources();

使用 BitmapDrawable 获取位图

1.  使用 BitmapDrawable (InputStream is) 构造一个 BitmapDrawable ;

2.  使用 BitmapDrawable 类的 getBitmap() 获取得到位图;

通过 Resource 的函数: InputStream openRawResource(int id) 获取得到资源文件的数据流后,也可以通 2 种方法来获取 Bitmap ,如下:

使用 BitmapDrawable

( A Drawable that wraps a bitmap and can be tiled,stretched, or aligned. )

使用 BitmapDrawable (InputStream is) 构造一个 BitmapDrawable ;

使用 BitmapDrawable 类的 getBitmap() 获取得到位图;

BitmapDrawable 也提供了显示位图等操作。

InputStreamis=res.openRawResource(R.drawable.pic180); //  读取资源文件获取输入流

BitmapDrawablebmpDraw=new BitmapDrawable(is);

Bitmapbmp=bmpDraw.getBitmap();

或者:

BitmapDrawablebmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);

Bitmapbmp=bmpDraw.getBitmap();

使用 BitmapFactory

( Creates Bitmap objects from various sources, includingfiles, streams, and byte-arrays. )

使用 BitmapFactory 类 decodeStream(InputStream is) 解码位图资源,获取位图。

使用 BitmapFactory 类 Bitmap bmp=BitmapFactory.decodeResource(res,R.drawable.pic180);  方法解码位图资源。

BitmapFactory 的所有函数都是 static ,这个辅助类可以通过资源 ID 、路径、文件、数据流等方式来获取位图。

以上方法在编程的时候可以自由选择,在 Android SDK 中说明可以支持的图片格式如下: png (preferred), jpg (acceptable), gif(discouraged) ,虽然 bmp 格式没有明确说明,但是在 Android SDK Support Media Format 中是明确说明了。

2.  获取位图的信息

要获取位图信息,比如位图大小、是否包含透明度、颜色格式等,获取得到 Bitmap就迎刃而解了,这些信息在 Bitmap 的函数中可以轻松获取到。 Android SDK 中对Bitmap 有详细说明,阅读起来也比较容易,不在此详细说明,这里只是辅助说明以下2 点:

在 Bitmap 中对 RGB 颜色格式使用 Bitmap.Config 定义,仅包括 ALPHA_8 、ARGB_4444 、 ARGB_8888 、 RGB_565 ,缺少了一些其他的,比如说 RGB_555 ,在开发中可能需要注意这个小问题;

Bitmap 还提供了 compress() 接口来压缩图片,不过 AndroidSAK 只支持 PNG 、JPG 格式的压缩;其他格式的需要 Android 开发人员自己补充了。

3.  显示位图

显示位图可以使用核心类 Canvas ,通过 Canvas 类的 drawBirmap() 显示位图,或者借助于 BitmapDrawable 来将 Bitmap 绘制到 Canvas 。当然,也可以通过BitmapDrawable 将位图显示到 View 中。

转换为 BitmapDrawable 对象显示位图

// 获取位图

Bitmapbmp=BitmapFactory.decodeResource(res, R.drawable.pic180);

//  转换为 BitmapDrawable 对象

BitmapDrawable bmpDraw=newBitmapDrawable(bmp);

//  显示位图

ImageView iv2 =(ImageView)findViewById(R.id.ImageView02);

iv2.setImageDrawable(bmpDraw);

使用 Canvas 类显示位图

这儿采用一个继承自 View 的子类 Panel ,在子类的 OnDraw 中显示

Java 代码

public classMainActivity extends Activity {

     
    @Override

public void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new Panel(this));

    }   
      

class Panel extends View{

public Panel(Context context) {

super(context);

}

public void onDraw(Canvas canvas){

Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.pic180);

canvas.drawColor(Color.BLACK);

canvas.drawBitmap(bmp, 10, 10,null);

        }   
    }   
}  
4.  位图缩放

( 1 )将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样: drawBitmap(Bitmap bitmap, Rect src, Rect dst,Paint paint) 。

( 2 )在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y,int width, int height, Matrix m, boolean filter)

( 3 )借助 Canvas 的 scale(float sx, float sy)  ( Preconcat the current matrix with thespecified scale. ),不过要注意此时整个画布都缩放了。

( 4 )借助 Matrix :

Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.pic180);

Matrix matrix=new Matrix();

matrix.postScale(0.2f,0.2f);

Bitmapdstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),

bmp.getHeight(),matrix,true);

canvas.drawColor(Color.BLACK);

canvas.drawBitmap(dstbmp,10, 10, null);

5.  位图旋转

同样,位图的旋转也可以借助 Matrix 或者 Canvas 来实现。 Matrix 在线性代数中都学习过, Android SDK 提供了 Matrix 类,可以通过各种接口来设置矩阵。结合上面的例子程序,将位图缩放例子程序在显示位图的时候前,增加位图旋转功能,修改代码如下:

Matrix matrix = new Matrix();

//matrix.postScale(0.5f,0.5f);

matrix.setRotate(90,120,130);

canvas.drawBitmap(mbmpTest,matrix, mPaint);

除了这种方法之外,我们也可以在使用 Bitmap 提供的函数如下:

public staticBitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrixm, boolean filter) ,在原有位图旋转的基础上,创建新位图。

Android技术精髓-Bitmap详解的更多相关文章

  1. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

  2. 给 Android 开发者的 RxJava 详解

    我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Flipboard 的 Android 项目也在使用 RxJava ,并且使用的场景越来越多 .而最近这几个 ...

  3. android ------- 开发者的 RxJava 详解

    在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github: https://github.com/ReactiveX/RxJava https://github. ...

  4. 转:给 Android 开发者的 RxJava 详解

    转自:  http://gank.io/post/560e15be2dca930e00da1083 评注:多图解析,但是我还是未看懂. 前言 我从去年开始使用 RxJava ,到现在一年多了.今年加入 ...

  5. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  6. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  7. Android开发之InstanceState详解(转)---利用其保存Activity状态

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  8. Android JNI作用及其详解

    Android JNI作用及其详解 Java Native Interface (JNI)标准是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 J ...

  9. android Camera2 API使用详解

    原文:android Camera2 API使用详解 由于最近需要使用相机拍照等功能,鉴于老旧的相机API问题多多,而且新的设备都是基于安卓5.0以上的,于是本人决定研究一下安卓5.0新引入的Came ...

随机推荐

  1. 关于unitils联合dbunit的测试

    unitils据说测试的能力很强大,可测试dao,service,web层,其实对数据库的测试我更关心,看到有人展示了测试的方法,数据直接写在xls表中,很直观,然后就依照他们的方法进行试验,花费的时 ...

  2. 绑定下拉框时避免触发SelectedIndexChanged事件

    在从数据库读取数据集绑定到下拉框时会立即触发其SelectedIndexChanged事件造成异常,可对其SelectedIndexChanged事件采取先解除后附加的方法解决. cmbXl_gt.V ...

  3. 常见的mongodb可视化工具

    一.MongoVue   1.MongoVUE是一款比较好用的MongoDB客户端工具,可以为大家提供一个高度.简洁可用的MongoDB管理界面. 2.通过MongoVUE,用户可以用树形.表格及bj ...

  4. Java线程运行轨迹-代码追踪与定位

    今天在写程序时,想到一个问题,当我的程序出异常的时候,控制台输出信息中,明确指出了出现异常的位置,并详细列举了函数的调用层次关系,它是怎么做到的. 竟然想到了这个问题,就去查看了源代码,不过没点几下, ...

  5. js 代码记录

    window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...

  6. 谷歌制图服务(Google Chart)接口生成二维码

    Google公布了制图服务(Google Chart)的接口,这项服务用起来相当简单,只使用浏览器就可以用来为统计数据自动生成图片. 目前谷歌制图服务提供折线图.条状图.饼图.Venn图.散点图.二维 ...

  7. QBImagePickerController 用法

    // // ViewController.m // QBImagePickerControllerDemo // // Created by Tanaka Katsuma on 2013/12/30. ...

  8. 【译】UI设计基础(UI Design Basics)--启动与停止(Starting and Stopping)(五)

    2.4  启动与停止(Starting and Stopping) 2.4.1  立即启动(Start Instantly) 通常来讲,用户不会花超过两分钟的时候去评价一个新的应用.在这段有限的时间里 ...

  9. BZOJ 1014 火星人prefix

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  10. iOS环形控制器、环形按钮

    这两天接手了一个外包的UI,有一个环形的控制器,需求改啊改的:“安卓已经实现了……”,最讨厌这句了,最后做了一版,对方终于满意了,删掉其他的繁琐部分,留下控制器部分,大家看看,有更好的想法欢迎分享. ...