The previous article titled "men are the next layer" (Layer 2] -- helping beautiful women change clothes (1) "introduced the use of the ImageSwitcher component and completed the presentation, let's complete the rest of this article.
When you click an image, you can jump to another Activity and upload the serial number of the image.
is.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){Intent intent = new Intent();intent.putExtra("imagePosition", gallery.getSelectedItemPosition());intent.setClass(MainActivity.this, RemoveClothActivity.class);startActivity(intent);}});
Create a View object as follows:
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); // get the screen width and height setScreenWH (); // set the background image 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 ();} // obtain image resources and display Bitmap bm = scaleBitmapFillScreen (BitmapFactory. decodeResource (getResources (), drawableId); seticon1Bitmap (bm);} private void setScreenWH () {// obtain the screen information DisplayMetrics dm = new DisplayMetrics (); dm = this. getResources (). getDisplayMetrics (); // get the screen width int screenWidth = dm. widthPixels; // get the screen height 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) {// set the Paint brush mPaint = new Paint (); // set the transparency to 0mPaint. setAlpha (0); // set the pattern when two images intersect // For details, see: http://trylovecatch.iteye.com/blog/1189452mPaint.setXfermode (new porterduxfermode (PorterDuff. mode. DST_IN); mPaint. setAntiAlias (true); mPaint. setDither (true); mPaint. setStyle (Paint. style. STROKE); // set the combination to an arc mPaint. setStrokeJoin (Paint. join. ROUND); // set the paint brush to an arc-shaped mPaint. setStrokeCap (Paint. cap. ROUND); // The width of the paint brush mPaint. setStrokeWidth (20); mPath = new Path (); // create an image mBitmap = Bitmap. createBitmap (SCREEN_W, SCREEN_H, Config. ARGB_8888); mCanvas = new Canvas (); // set it to the Canvas background mCanvas. setBitmap (mBitmap); // draw an image to the canvas mCanvas. drawBitmap (bm, 0, 0, null);} @ Overrideprotected 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); // start to draw mCanvas. drawPath (mPath, mPaint); // redraw the route mPath. reset () ;}@ Overridepublic 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 ;}}
For more information, see code comments.
See the basic principles of http://blog.csdn.net/dawanganban/article/details/17439667
Source code download: http://download.csdn.net/detail/lxq_xsyu/7014889