您的位置:首页 > 移动开发 > Android开发

android 图片操作之BitmapMesh

2016-01-03 14:11 513 查看
[code]
/**
 * 通过 坐标矩阵的变换来操作图片
 */
public class MeshView extends View {

    private static int WIDTH=200;
    private static int HEIGHT=200;

    //分割的焦点个数
    private int COUNT=(WIDTH+1)*(HEIGHT+1);
    //保存原坐标
    private float[] orig=new float[COUNT*2];
    //原本的图片
    private Bitmap origBitmap;

    public MeshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MeshView(Context context, AttributeSet attrs) {
        this(context, attrs,0);

    }

    public MeshView(Context context) {
        this(context,null);
    }

    public void init(){

        origBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.test3);
        int width=origBitmap.getWidth();
        int height=origBitmap.getHeight();
        int index=0;

        for(int i=0;i<WIDTH+1;i++){
            float fx=width*i/WIDTH;
            for(int j=0;j<HEIGHT+1;j++){
                float fy=height*j/HEIGHT;
                //保存坐标的横坐标
                orig[index*2+0]=fx;
                //保存坐标的纵坐标
                orig[index*2+1]=fy;
                index++;
            }
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {

        //获取变换后的数组
        float[] verts=sinPixels(orig);
        //通过变换后的数组来操作图片
        canvas.drawBitmapMesh(origBitmap, WIDTH, HEIGHT, verts,0,null, 0, null);
    }

    public float[] sinPixels(float[] orgs){

        float[] verts=new float[COUNT*2];

               //坐标变换
                for(int i=0;i<WIDTH+1;i++){
                    for(int j=0;j<HEIGHT+1;j++){
                        //横坐标
                        verts[(i*(HEIGHT+1)+j)*2]=orgs[(i*(HEIGHT+1)+j)*2];
                        //纵坐标的偏移
                        float offy=(float) Math.sin((float)i/HEIGHT*2*Math.PI);
                        //纵坐标
                        verts[(i*(HEIGHT+1)+j)*2+1]=offy*50+orgs[(i*(HEIGHT+1)+j)*2+1];
                    }
                }

        return verts;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: