是男人就下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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值