Android技术精髓-Bitmap详解
Bitmap (android.graphics.Bitmap)
Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。
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());
}
}
}
}
<?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 开发人员自己补充了。
显示位图可以使用核心类 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 中显示
public classMainActivity extends Activity {
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);
( 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);
同样,位图的旋转也可以借助 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详解的更多相关文章
- Android图片缓存之Bitmap详解
前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...
- 给 Android 开发者的 RxJava 详解
我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Flipboard 的 Android 项目也在使用 RxJava ,并且使用的场景越来越多 .而最近这几个 ...
- android ------- 开发者的 RxJava 详解
在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github: https://github.com/ReactiveX/RxJava https://github. ...
- 转:给 Android 开发者的 RxJava 详解
转自: http://gank.io/post/560e15be2dca930e00da1083 评注:多图解析,但是我还是未看懂. 前言 我从去年开始使用 RxJava ,到现在一年多了.今年加入 ...
- 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING
<Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th 2014 Email:skyseraph00@163.com 更多精彩请直接 ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之InstanceState详解(转)---利用其保存Activity状态
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android JNI作用及其详解
Android JNI作用及其详解 Java Native Interface (JNI)标准是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 J ...
- android Camera2 API使用详解
原文:android Camera2 API使用详解 由于最近需要使用相机拍照等功能,鉴于老旧的相机API问题多多,而且新的设备都是基于安卓5.0以上的,于是本人决定研究一下安卓5.0新引入的Came ...
随机推荐
- window下配置SSH连接GitHub、GitHub配置ssh key(转)
转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...
- 基于jQuery查找dom的多种方式性能问题
这个问题的产生由于我们前端组每个人的编码习惯的差异,最主要的还是因为代码的维护性问题.在此基础上,我对jQuery源码(1.11.3)查找dom节点相关的内容进行了仔细的查阅,虽然并不能理解的很深入 ...
- pojo和JavaBean的区别
javabean可以处理业务,pojo不可以. pojo就是get 和set 例如: Student{ id; name; get();... set();...} javabean可以实现业务逻辑 ...
- css实现三角的一些方法
css实现三角没有想象中的那么难,只要明白border的各种属性的意思就很好明白css三角是如何实现的. 一下是几个很简单的例子: css三角形状的制作: css样式: .trian ...
- refreshcontrol 实现下拉刷新的功能
该组件实现下拉刷新的功能.不过该组件是用在ScrollView的内部的,为ScrollView添加一个下拉刷新的功能.当ScrollView的垂直方向的偏移量scrollY:0的时候,手指往下拖拽Sc ...
- 轻松解决Win8.1连接受限或无法连接WiFi问题
在无线网络连接设置窗口中,找到当前连接的无线网络“WLAN状态”,右击查看“状态”. 在“WLAN状态”窗口中找到“无线属性”. 进入“无线网络属性”窗口,切换到“安全”页面,点击“高级设置”.最重要 ...
- 应用SecureCRT(发送接收文件)
使用 SecureCRT 和 cz. sz,可以从 Linux 服务器上下载/上传文件. Linux 上要安装 lszrz 包 (1)编译安装root 账号登陆后,依次执行以下命令 cd /tmp w ...
- ubuntu 解压rar
Ubuntu下解压rar文件的方法 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤 ...
- 如何把iOS代码编译为Android应用
新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...
- JAVA线程示范之一种
线程有两种方法生成,这是其中的一种.. MyRunnable.java public class MyRunnable implements Runnable { public void run() ...