Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現可以揉動的圖片

Android實現可以揉動的圖片

編輯:關於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 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;
		}
	}
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved