黑马程序员技术交流社区

标题: 分享一段 Android图片处理代码 [打印本页]

作者: 张振宁    时间: 2011-12-22 16:26
标题: 分享一段 Android图片处理代码
/** 放大缩小
     * @param bgimage
     * @param newWidth
     * @param newHeight
     * @return
     */
    public Bitmap ZoomImage(Bitmap src, Float newWidth, Float newHeight) {
                int width = src.getWidth();
                int height = src.getHeight();
                Matrix matrix = new Matrix();
                float scaleWidth  = newWidth  / width;
                float scaleHeight = newHeight / height;
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap bitmap = Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
                return bitmap;
    }

/**        旋转图片返回传入点旋转后的位置
     * @param         图片大小/中点/旋转角度
     * @return        旋转后中点位置
     */
    public Vector2D rota(Vector2D bitmapSize, Vector2D midPoint, float angle){
        float w = bitmapSize.x;
        float h = bitmapSize.y;
        angle = (float)Math.toRadians(angle);
            Vector<Vector2D> vec = new Vector<Vector2D>();
            //创建矩形的四个顶点
            Vector2D point1 = new Vector2D(-midPoint.x,-midPoint.y);
            Vector2D point2 = new Vector2D(-midPoint.x + w,-midPoint.y);
            Vector2D point3 = new Vector2D(-midPoint.x + w,-midPoint.y + h);
            Vector2D point4 = new Vector2D(-midPoint.x,-midPoint.y + h);
            //旋转后顶点位置
        point1.setAngle(point1.getAngle()+angle);
        point2.setAngle(point2.getAngle()+angle);
        point3.setAngle(point3.getAngle()+angle);
        point4.setAngle(point4.getAngle()+angle);
        //取出左上顶点
        vec.addElement(point1);
        vec.addElement(point2);
        vec.addElement(point3);
        vec.addElement(point4);
        Vector2D newOrigin = new Vector2D(0,0);
            for (int i = 0; i < vec.size(); i++) {
              Vector2D tmp = vec.elementAt(i);
              if(newOrigin.getX()>tmp.getX()){
                newOrigin.setX(tmp.getX());
             }
             if(newOrigin.getY()>tmp.getY()){
                newOrigin.setY(tmp.getY());
            }
        }
        return newOrigin;
}
//单张图片旋转
public Bitmap draw(){
        float angle = (float)Math.toDegrees(velocity.getAngle())+90;
        Matrix ma = new Matrix();
        ma.setRotate(angle);
        mid = GameView.img.rota(new Vector2D(imgSrc.getWidth(),imgSrc.getHeight()), new Vector2D(imgSrc.getWidth()/2,imgSrc.getHeight()/2), angle);
        return Bitmap.createBitmap(imgSrc,0,0,imgSrc.getWidth(),imgSrc.getHeight(),ma,true);
}
Vector2D   向量类
public class Vector2D {
        public float x;
        public float y;
        //-------------------------------------------------
        public Vector2D() {
                super();
        }
        public Vector2D(float x, float y) {
                this.x = x;
                this.y = y;
        }
        //-------------------------------------------------
        public Vector2D clone() {
                return new Vector2D(x, y);
        }
        //归零
        public Vector2D zero(){
                x = 0;
                y = 0;
                return this;
        }
        //是否为0
        public boolean isZero(){
                return x == 0 && y == 0;
        }
        /** 取出两点间距离*/
        public float getLength(){
                return (float)Math.sqrt(getLengthSQ());
        }
        /** 取出X Y的平方和*/
        public float getLengthSQ(){
                return x * x + y * y;
        }
        /**        设置长度*/
        public void setLength(float value){
                float a = getAngle();
                x = (float)Math.cos(a) * value;
                y = (float)Math.sin(a) * value;
        }
        /** 设置角度*/
        public void setAngle(float value){
                float len = getLength();
                x = (float)Math.cos(value) * len;
                y = (float)Math.sin(value) * len;
        }
        public float getAngle(){
                return (float)Math.atan2(y, x);
        }
        /** 规范化*/
        public Vector2D normalize(){
                if (getLength() == 0) {
                        x = 1;
                        return this;
                }
                float len = getLength();
                x /=  len;
                y /=  len;
                return this;
        }
        /** 取最小值*/
        public Vector2D truncate(float max){
                setLength(Math.min(max,getLength()));
                return this;
        }
        /** 取负值*/
        public Vector2D reverse(){
                x =  -  x;
                y =  -  y;
                return this;
        }
        public boolean isNormalized(){
                return getLength()==1.0;
        }
       
        public float dotProd(Vector2D v2){
                return x * v2.getX() + y * v2.getY();
        }
       
        public float crossProd(Vector2D v2) {
                return x * v2.getY() - y * v2.getX();
        }
        public static float angleBetween(Vector2D v1, Vector2D v2) {
                if (! v1.isNormalized()) {
                        v1 = v1.clone().normalize();
                }
                if (! v2.isNormalized()) {
                        v2 = v2.clone().normalize();
                }
                return (float)Math.acos(v1.dotProd(v2));
        }
       
        public int sign(Vector2D v2) {
                return getPerp().dotProd(v2) < 0 ? -1 : 1;
        }
       
        public Vector2D getPerp() {
                return new Vector2D(-y, x);
        }
       
        public float dist(Vector2D v2) {
                return (float)Math.sqrt(distSQ(v2));
        }
       
        public float distSQ(Vector2D v2) {
                float dx = v2.getX() - x;
                float dy = v2.getY() - y;
                return dx * dx + dy * dy;
        }
       
        public Vector2D add(Vector2D v2) {
                return new Vector2D(x + v2.getX(), y + v2.getY());
        }
                       
        public Vector2D subtract(Vector2D v2){
                return new Vector2D(x - v2.getX(), y - v2.getY());
        }
       
        public Vector2D multiply(float value) {
                return new Vector2D(x * value, y * value);
        }
        /** 分份*/
        public Vector2D divide(float value) {
                return new Vector2D(x / value, y / value);
        }
        /** 查看两点坐标是否相同*/
        public boolean equals(Vector2D v2) {
                return x == v2.getX() && y == v2.getY();
        }

        public String toString() {
                return "[Vector2D (x:" + x + ", y:" + y + ")]";
        }

        //-------------------------------------------------
        public float getX() {
                return x;
        }
        public void setX(float x) {
                this.x = x;
        }
        public float getY() {
                return y;
        }
        public void setY(float y) {
                this.y = y;
        }
}
作者: 房宝彬    时间: 2011-12-22 17:15
我只能说,我看不懂{:soso_e127:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2