Bitmap
保存原始版本-这正是我所需要的,所以这里有一个解决方案:
int w = 72;
int h = 48;
float srcPoints[] = {108, 201,
532, 26,
554, 301,
55, 372};
float dstPoints[] = {0, 0,
w, 0,
w, h,
0, h};
Matrix m = new Matrix();
m.setPolyToPoly(srcPoints, 0, dstPoints, 0, dstPoints.length >> 1);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.macedonia);
Bitmap dstBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(dstBitmap);
canvas.clipRect(0, 0, w, h);
canvas.drawBitmap(srcBitmap, m, null);
但如果您不想使用额外内存,请使用其他方法:
class BD extends BitmapDrawable {
Matrix matrix = new Matrix();
int w = 72;
int h = 48;
RectF clip = new RectF(0, 0, w, h);
public BD(Resources res, int resId) {
super(res, BitmapFactory.decodeResource(res, resId));
float src[] = {108,201,532,26,554,301,55,372};
float dst[] = {0,0,w,0,w,h,0,h};
matrix.setPolyToPoly(src, 0, dst, 0, src.length / 2);
}
@Override public int getIntrinsicWidth() {return w;}
@Override public int getIntrinsicHeight() {return h;}
@Override
public void draw(Canvas canvas) {
canvas.clipRect(clip);
canvas.drawBitmap(getBitmap(), matrix, null);
}
}