Android实现能够揉动的图片

public class Demo01 extends Activity{
	private Bitmap bitmap = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this, R.drawable.background));
	}
	
	private class MyView extends View{
		private final int  WIDTH = 20;
		private final int  HEIGHT = 20;
		private final int COUNT = (WIDTH+1)*(HEIGHT+1);
		private final float[] verts = new float[COUNT*2];
		private final float[]  orgs = new float[COUNT*2];
		public MyView(Context context,int drawableId) {
			super(context);
			setFocusable(true);
			bitmap = BitmapFactory.decodeResource(getResources(), drawableId);
			float bitmapWidth = bitmap.getWidth();
			float bitmapHeight = bitmap.getHeight();
			int index = 0;
			for(int y=0;y<=HEIGHT;y++){
				float fy = bitmapHeight / WIDTH * y;
				for(int x = 0;x<=WIDTH;x++){
					float fx = bitmapWidth / HEIGHT * x;
					verts[index*2 + 0 ] = orgs[index*2 + 0] = fx;
					verts[index*2 + 1 ] = orgs[index*2 + 1] = fy;
					index ++;
				}
			}
			setBackgroundColor(Color.WHITE);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts, 0, null, 0, null);
		}
		
		private void warp(float fx , float fy){
			for(int i=0;i<COUNT*2;i+=2){
				float dx = fx - orgs[i+0];
				float dy = fy - orgs[i+1];
				float dd = dx*dx + dy*dy;
				float d = (float) Math.sqrt(dd);
				float pull = 80000 / (float)(dd*d);
				if(pull > 1){
					verts[i+0] = fx;
					verts[i+1] = fy;
				}else{
					verts[i+0] = orgs[i+0] + dx*pull;
					verts[i+1] = orgs[i+1] + dy*pull;
				}
			}
			invalidate();
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			warp(event.getX(),event.getY());
			return true;
		}
	}
}

原文地址:https://www.cnblogs.com/lxjshuju/p/7010109.html