是男人就下100层【第二层】——帮美女更衣(2)

前一篇《是男人就下100层【第二层】——帮美女更衣(1)》介绍了ImageSwitcher组件的使用,并完成了展示,这一篇我们来完成剩下的部分吧。

在点击图片的时候跳到另一个Activity并将该图片的序号传过去。

is.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v){
				Intent intent = new Intent();
				intent.putExtra("imagePosition", gallery.getSelectedItemPosition());
				intent.setClass(MainActivity.this, RemoveClothActivity.class);
				startActivity(intent);
			}
		});
创建一个View对象,如下:

	class MyView extends View {
		private Bitmap mBitmap;
		private Canvas mCanvas;
		private Paint mPaint;
		private Path mPath;
		private float mX, mY;
		private static final float TOUCH_TOLERANCE = 4;

		public MyView(Context context) {
			super(context);
			setFocusable(true);
			//获取屏幕宽高
			setScreenWH();
			//设置背景图片
			setBackGround();
			
			int drawableId = 0;
			try {
				drawableId = R.drawable.class.getDeclaredField(
						"pre" + imagePosition).getInt(this);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
			//获取图片资源并全屏显示
			Bitmap bm = scaleBitmapFillScreen(BitmapFactory.decodeResource(
					getResources(), drawableId));
			seticon1Bitmap(bm);

		}

		private void setScreenWH() {
			//获取屏幕信息
			DisplayMetrics dm = new DisplayMetrics();
			dm = this.getResources().getDisplayMetrics();
			//获取屏幕宽度
			int screenWidth = dm.widthPixels;
			//获取屏幕高度
			int screenHeight = dm.heightPixels;

			SCREEN_W = screenWidth;
			SCREEN_H = screenHeight;
		}

		private Bitmap createBitmapFromSRC() {
			return BitmapFactory.decodeResource(getResources(),
					R.drawable.icon1);
		}

		private Bitmap createBitmapFromARGB(int colorARGB, int width, int height) {
			int[] argb = new int[width * height];

			for (int i = 0; i < argb.length; i++) {

				argb[i] = colorARGB;

			}
			return Bitmap.createBitmap(argb, width, height, Config.ARGB_8888);
		}


		private Bitmap setBitmapAlpha(Bitmap bm, int alpha) {
			int[] argb = new int[bm.getWidth() * bm.getHeight()];
			bm.getPixels(argb, 0, bm.getWidth(), 0, 0, bm.getWidth(),
					bm.getHeight());

			for (int i = 0; i < argb.length; i++) {

				argb[i] = ((alpha << 24) | (argb[i] & 0x00FFFFFF));
			}
			return Bitmap.createBitmap(argb, bm.getWidth(), bm.getHeight(),
					Config.ARGB_8888);
		}


		private Bitmap scaleBitmapFillScreen(Bitmap bm) {
			return Bitmap.createScaledBitmap(bm, SCREEN_W, SCREEN_H, true);
		}

		private void setBackGround() {
			int drawableId = 0;
			try {
				drawableId = R.drawable.class.getDeclaredField(
						"after" + imagePosition).getInt(this);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
			setBackgroundResource(drawableId);
		}


		private void seticon1Bitmap(Bitmap bm) {
			// 设置画笔
			mPaint = new Paint();
			//设置透明度为0
			mPaint.setAlpha(0);
			//设置两张图片相交时的模式
			//详细请参考:http://trylovecatch.iteye.com/blog/1189452
			mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
			
			mPaint.setAntiAlias(true);

			mPaint.setDither(true);
			mPaint.setStyle(Paint.Style.STROKE);
			//设置结合处为圆弧
			mPaint.setStrokeJoin(Paint.Join.ROUND);
			//设置画笔为圆弧状
			mPaint.setStrokeCap(Paint.Cap.ROUND);
			//画笔宽度
			mPaint.setStrokeWidth(20);

			
			mPath = new Path();
			
			//创建一张图片
			mBitmap = Bitmap.createBitmap(SCREEN_W, SCREEN_H, Config.ARGB_8888);
			mCanvas = new Canvas();
			//设置为画布背景
			mCanvas.setBitmap(mBitmap);
			//绘制图片到画布
			mCanvas.drawBitmap(bm, 0, 0, null);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawBitmap(mBitmap, 0, 0, null);
			mCanvas.drawPath(mPath, mPaint);
			super.onDraw(canvas);
		}

		private void touch_start(float x, float y) {
			mPath.reset();
			mPath.moveTo(x, y);
			mX = x;
			mY = y;
		}

		private void touch_move(float x, float y) {
			float dx = Math.abs(x - mX);
			float dy = Math.abs(y - mY);
			if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
				mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
				mX = x;
				mY = y;
			}
		}

		private void touch_up() {
			mPath.lineTo(mX, mY);
			// 开始绘制
			mCanvas.drawPath(mPath, mPaint);
			// 重新开始绘制路线
			mPath.reset();
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			float x = event.getX();
			float y = event.getY();

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);
				invalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				touch_move(x, y);
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				touch_up();
				invalidate();
				playMusic();
				break;
			}
			return true;
		}
	}

具体请看代码注释

基本原理请看:http://blog.csdn.net/dawanganban/article/details/17439667


源代码下载:http://download.csdn.net/detail/lxq_xsyu/7014889






原文地址:https://www.cnblogs.com/lanzhi/p/6469140.html