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://blog.csdn.net/qq_22605739/article/details/47906081
文章标签: android 图片 位图
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭