Parameters
bitmap Bitmap: The bitmap to draw using the mesh
meshWidth int: The number of columns in the mesh. Nothing is drawn if this is 0
meshHeight int: The number of rows in the mesh. Nothing is drawn if this is 0
verts float: Array of x,y pairs, specifying where the mesh should be drawn. There must be at least (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values in the array
vertOffset int: Number of verts elements to skip before drawing
colors int: May be null. Specifies a color at each vertex, which is interpolated across the cell, and whose values are multiplied by the corresponding bitmap colors. If not null, there must be at least (meshWidth+1) * (meshHeight+1) + colorOffset values in the array.
colorOffset int: Number of color elements to skip before drawing
paint Paint: May be null. The paint used to draw the bitmap
public class MainActivity extends Activity {
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(MainActivity.this, R.drawable.jinta));
}
private class MyView extends View{
//將圖片在橫向,縱向劃分為20格
private final int WIDTH=20,HEIGHT=20;
//包含441個頂點
private final int COUNT=(WIDTH+1)*(HEIGHT+1);
//保存原坐標
private final float[] verts=new float[COUNT*2];
//保存扭曲后坐標次屠,對圖片扭曲關鍵為修改該數(shù)組的值
private final float[] orig=new float[COUNT*2];
public MyView(Context context,int DrawableId) {
super(context);
setFocusable(true);
//加載圖片資源
bitmap=BitmapFactory.decodeResource(getResources(), DrawableId);
//獲取高弯菊,寬
float bitmapWidth=bitmap.getWidth(),
bitmapHeight=bitmap.getHeight();
int index=0;
//對原坐標和扭曲后坐標數(shù)組進行初始化操作
for(int y=0;y<=HEIGHT;y++){
float fy=bitmapHeight*y/HEIGHT;
for(int x=0;x<=WIDTH;x++){
float fx=bitmapWidth*x/WIDTH;
orig[index*2+0]=verts[index*2+0]=fx;
orig[index*2+1]=verts[index*2+1]=fy;
index+=1;
}
}
//背景色
setBackgroundColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmapMesh(bitmap,WIDTH,HEIGHT,verts,0,null,0,null);
}
//cx,cy為touch事件發(fā)生坐標
private void wrap(float cx,float cy){
for(int i=0;i<COUNT*2;i+=2){
//dx,dy為當前點距離事件發(fā)生點的坐標差
float dx=cx-orig[i+0];
float dy=cy-orig[i+1];
float dd=dx*dx+dy*dy;
//d為當前點與事件發(fā)生點的距離
float d=(float)Math.sqrt(dd);
//pull為扭曲度千贯,距離越大扭曲程度越小
float pull=80000/((float)(dd*d));
if(pull>=1){
//距離近,直接扭曲至事件發(fā)生點
verts[i+0]=cx;
verts[i+1]=cy;
}else{
//距離稍遠,向事件發(fā)生偏移
verts[i+0]=orig[i+0]+dx*pull;
verts[i+1]=orig[i+1]+dy*pull;
}
}
//通知組件調用OnDraw重繪
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//觸摸事件發(fā)生時調用扭曲方法
wrap(event.getX(),event.getY());
return true;
}
}
}