更多细节参照:http://blog.csdn.net/flash129/article/details/8234599
Bitmap
Bitmp代表一张位图,BitmapDrawable里封装的图片就是一张Bitmap对象
简单代码示例
package com.test.mypathview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyBitMap extends View{
private Bitmap mBitmap;
private Paint mPaint;
public MyBitMap(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.d);
Log.d("aa", mBitmap.getWidth()+"高"+mBitmap.getHeight());
}
public MyBitMap(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
}
对图片的各种操作
package com.test.mypathview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyBitMap extends View{
private Bitmap mBitmap;
private Paint mPaint;
private Matrix mMatrix;
private int mHeight;
private int mWidth;
private int mBitmapWidth;
private int mBitmapHeight;
public MyBitMap(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mMatrix=new Matrix();
mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.d);
Log.d("aa", mBitmap.getWidth()+"高"+mBitmap.getHeight());
mBitmapWidth=mBitmap.getWidth();
mBitmapHeight=mBitmap.getHeight();
}
public MyBitMap(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth=getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
mHeight=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawBitmap(mBitmap, 0,0, mPaint);
//放大
mMatrix.reset();
mMatrix.postScale(2, 2);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
mMatrix.reset();
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//平移
mMatrix.reset();
mMatrix.postTranslate(0, 2*mBitmapHeight);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//旋转平移
mMatrix.reset();
mMatrix.preRotate(180);
mMatrix.postTranslate(mBitmapWidth*2, mBitmapHeight*3);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//拉伸,面积不变
mMatrix.reset();
mMatrix.setSkew(0, 1);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//沿x轴翻转
mMatrix.reset();
float matrix_values[] = {1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f};
mMatrix.setValues(matrix_values);
mMatrix.postTranslate(0, mBitmapHeight*2);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//沿y轴翻转
mMatrix.reset();
float matrix_values2[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};
mMatrix.setValues(matrix_values2);
mMatrix.postTranslate(mBitmapWidth*2, 0);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//测试用
mMatrix.reset();
float matrix_values3[] = {0f, 0.5f,0.5f, -1f, 0f, 0.5f, 0.5f, -1f, 0f};
mMatrix.setValues(matrix_values2);
mMatrix.postTranslate(mBitmapWidth*3, mBitmapHeight*4);
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
}
}
效果图
参考:http://blog.csdn.net/t12x3456/article/details/10432935
更高级操作
package com.test.mypathview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
public class MyBitmip2 extends View {
private int height;
private int width;
private Bitmap mBitmap;
private Canvas mBitmapCanvas;
private Paint mPaintCircle;
private Paint mPaintRec;
public MyBitmip2(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintCircle = new Paint();
mPaintCircle.setColor(Color.RED);
mPaintRec = new Paint();
mPaintRec.setColor(Color.YELLOW);
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
mPaintRec.setXfermode(mode);
}
public MyBitmip2(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mBitmapCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE);
mBitmapCanvas.drawCircle(width/2, height/2, width/2, mPaintCircle);
mBitmapCanvas.drawRect(0, 0, width/2, width/2, mPaintRec);
canvas.drawBitmap(mBitmap, 0,0, null);
}
}