原文链接:
http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/24/1530246.html
编辑整理:yftx
[b][size=large]获取位图[/size][/b]
[color=blue]三种方式获取bitmap,demo中都是从resource中获取图片。
方法一使用的inputStream流,所以比较适合从网络上面直接读取图片,显示图片。
方法二与方法三均是从resource中直接读取图片。
故方法一比较适合从网络加载图片。[/color]
[quote]
[color=blue]Tips[/color]
[b]获取位图的信息[/b]
要获取位图信息,比如位图大小、像素、density、透明度、颜色格式等。
获取得到Bitmap就迎刃而解了,这些信息在Bitmap的手册中,这里只是辅助说明以下2点:
[color=blue]* 在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、 RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题。
* Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充。|[/color][/quote]
[b]显示位图[/b]
[color=blue]转换为BitmapDrawable对象显示位图[/color]
[color=blue]使用Canvas类显示位图
这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示 [/color]
[b]位图缩放[/b]
[img]http://dl.iteye.com/upload/attachment/370491/149bf137-36f7-3e6a-a610-2a0c2d134611.png[/img]
[b]位图旋转[/b]
[img]http://dl.iteye.com/upload/attachment/370531/1f83dcb0-a65e-3ca9-934e-4e7209e4d49a.png[/img]
[b]图片水印的生成方法[/b]
[b]Canvas的save和restore[/b]
onDraw方法会传入一个Canvas对象,它是你用来绘制控件视觉界面的画布。
在onDraw方法里,我们经常会看到调用save和restore方法,它们到底是干什么用的呢?
❑ save:用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。
❑ restore:用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。
save和restore要配对使用(restore可以比save少,但不能多),如果restore调用次数比save多,会引发Error。save和restore之间,往往夹杂的是对Canvas的特殊操作。
例如:我们先想在画布上绘制一个右向的三角箭头,当然,我们可以直接绘制,另外,我们也可以先把画布旋转90°,画一个向上的箭头,然后再旋转回来(这种旋转操作对于画圆周上的标记非常有用)。然后,我们想在右下角有个20像素的圆,那么,onDraw中的核心代码是:
效果如图
[img]http://dl.iteye.com/upload/attachment/370544/5fc4ce5c-5f76-3443-958b-10725862ae30.png[/img]
[color=red]如果我们不调用save和restore的样子 [/color]
[img]http://dl.iteye.com/upload/attachment/370546/de83548c-00a8-3a18-880d-4eb08f08b8fc.png[/img]
http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/24/1530246.html
编辑整理:yftx
[b][size=large]获取位图[/size][/b]
[color=blue]三种方式获取bitmap,demo中都是从resource中获取图片。
方法一使用的inputStream流,所以比较适合从网络上面直接读取图片,显示图片。
方法二与方法三均是从resource中直接读取图片。
故方法一比较适合从网络加载图片。[/color]
package org.yftx.bitmap;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
public class BitmapDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = this.getResources();
/** 方法一:获取bitmap的方式,BitmapDrawable方式获取位图
* InputStream is=res.openRawResource(R.drawable.android);
* BitmapDrawable bmpDraw=new BitmapDrawable(is);
* Bitmap bmp=bmpDraw.getBitmap();
*/
/**方法二:获取bitmap的方式,BitmapDrawable方式获取位图
* BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.android);
* Bitmap bmp=bmpDraw.getBitmap();
*/
/**方法三:获取bitmap的方式,BitmapFactory方式获取位图
*
*/
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.android);
}
}
[quote]
[color=blue]Tips[/color]
[b]获取位图的信息[/b]
要获取位图信息,比如位图大小、像素、density、透明度、颜色格式等。
获取得到Bitmap就迎刃而解了,这些信息在Bitmap的手册中,这里只是辅助说明以下2点:
[color=blue]* 在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、 RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题。
* Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充。|[/color][/quote]
[b]显示位图[/b]
[color=blue]转换为BitmapDrawable对象显示位图[/color]
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.android);
/**
* 转换为BitmapDrawable对象显示位图
*
*/
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw = new BitmapDrawable(bmp);
// 显示位图
ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageDrawable(bmpDraw);
[color=blue]使用Canvas类显示位图
这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示 [/color]
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
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);
}
}
}
[b]位图缩放[/b]
/**
* 对位图进行缩放
*
*/
//对位图进行缩放
Matrix matrix=new Matrix();
//缩放的比例
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),//
bmp.getHeight(),matrix,true);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
BitmapDrawable bmpDraw2 = new BitmapDrawable(dstbmp);
imageView2.setImageDrawable(bmpDraw2);
[img]http://dl.iteye.com/upload/attachment/370491/149bf137-36f7-3e6a-a610-2a0c2d134611.png[/img]
[b]位图旋转[/b]
/**
* 位图旋转
*
*/
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
//设置旋转角度
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),//
bmp.getHeight(),matrix,true);
ImageView imageView2 = (ImageView)findViewById(R.id.imageView2);
BitmapDrawable bmpDraw2 = new BitmapDrawable(dstbmp);
imageView2.setImageDrawable(bmpDraw2);
[img]http://dl.iteye.com/upload/attachment/370531/1f83dcb0-a65e-3ca9-934e-4e7209e4d49a.png[/img]
[b]图片水印的生成方法[/b]
//生成水印的过程。其实分为三个环节:第一,载入原始图片;第二,载入水印图片;第三,保存新的图片。
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//创建一个新的和SRC长度宽度一样的位图
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
Canvas cv = new Canvas( newb );
//在 0,0坐标开始画入src
cv.drawBitmap( src, 0, 0, null );
//在src的右下角画入水印
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );
//保存
cv.save( Canvas.ALL_SAVE_FLAG );
//存储
cv.restore();
return newb;
}
[b]Canvas的save和restore[/b]
onDraw方法会传入一个Canvas对象,它是你用来绘制控件视觉界面的画布。
在onDraw方法里,我们经常会看到调用save和restore方法,它们到底是干什么用的呢?
❑ save:用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。
❑ restore:用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。
save和restore要配对使用(restore可以比save少,但不能多),如果restore调用次数比save多,会引发Error。save和restore之间,往往夹杂的是对Canvas的特殊操作。
例如:我们先想在画布上绘制一个右向的三角箭头,当然,我们可以直接绘制,另外,我们也可以先把画布旋转90°,画一个向上的箭头,然后再旋转回来(这种旋转操作对于画圆周上的标记非常有用)。然后,我们想在右下角有个20像素的圆,那么,onDraw中的核心代码是:
int px = getMeasuredWidth();
int py = getMeasuredWidth();
// Draw background
canvas.drawRect(0, 0, px, py, backgroundPaint);
canvas.save();
canvas.rotate(90, px/2, py/2);
// Draw up arrow
canvas.drawLine(px / 2, 0, 0, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px / 2, py, linePaint);
canvas.restore();
// Draw circle
canvas.drawCircle(px - 10, py - 10, 10, linePaint);
效果如图
[img]http://dl.iteye.com/upload/attachment/370544/5fc4ce5c-5f76-3443-958b-10725862ae30.png[/img]
[color=red]如果我们不调用save和restore的样子 [/color]
[img]http://dl.iteye.com/upload/attachment/370546/de83548c-00a8-3a18-880d-4eb08f08b8fc.png[/img]