扒美女衣服原版来自日本妄撮游戏,如今介绍一下它的实现原理。
1、两张美女图片,一张穿着衣服,一张没穿衣服。
2、採用FrameLayout将穿衣服的图片放在上面,没穿衣服的放在以下
3、当触摸图片时,将触摸位置处的图片设为透明。
这样就能够看到以下没穿衣服的图片,哈哈,是不是非常easy。
以下来看详细代码:
Layout布局两张图片叠加在一起
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_after" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_before" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
主程序代码:
public class bayifu extends Activity { private ImageView iv_after; private ImageView iv_before; private Bitmap alterBitmap; private Canvas canvas; private Paint paint; private Bitmap after; private Bitmap before; @Override protected void onCreate(Bundle savedInstanceState) { int[] imageIds1 = new int[] { R.drawable.pre1, R.drawable.pre2, R.drawable.pre3, R.drawable.pre4, R.drawable.pre5, R.drawable.pre6, R.drawable.pre7, R.drawable.pre8, R.drawable.pre9, R.drawable.pre10, R.drawable.pre11, R.drawable.pre12, R.drawable.pre13, R.drawable.pre14, R.drawable.pre15, R.drawable.pre16, R.drawable.pre17, R.drawable.pre18, R.drawable.pre19, R.drawable.pre20, R.drawable.pre21 }; int[] imageIds2 = new int[] { R.drawable.after1, R.drawable.after2, R.drawable.after3, R.drawable.after4, R.drawable.after5, R.drawable.after6, R.drawable.after7, R.drawable.after8, R.drawable.after9, R.drawable.after10, R.drawable.after11, R.drawable.after12, R.drawable.after13, R.drawable.after14, R.drawable.after15, R.drawable.after16, R.drawable.after17, R.drawable.after18, R.drawable.after19, R.drawable.after20, R.drawable.after21 }; // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.tuoyifulayout); Intent ic = getIntent(); Bundle bd = ic.getExtras(); String is = bd.getString("num"); int position = Integer.parseInt(is); BitmapFactory.Options opts = new Options(); opts.inSampleSize = 1; iv_after = (ImageView) findViewById(R.id.iv_after); iv_before = (ImageView) findViewById(R.id.iv_before); after = BitmapFactory.decodeResource(getResources(), imageIds2[position], opts); before = BitmapFactory.decodeResource(getResources(), imageIds1[position], opts); // 能够改动的空白的bitmap alterBitmap = Bitmap.createBitmap(before.getWidth(), before.getHeight(), before.getConfig()); canvas = new Canvas(alterBitmap); paint = new Paint(); paint.setStrokeCap(Cap.ROUND); paint.setStrokeJoin(Join.ROUND); paint.setStrokeWidth(5); paint.setColor(Color.BLACK); paint.setAntiAlias(true); canvas.drawBitmap(before, new Matrix(), paint); //在ImageView中设置了重叠的两张图片 iv_after.setImageBitmap(after); iv_before.setImageBitmap(before); iv_before.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int newX = (int) event.getX(); int newY = (int) event.getY(); //将触摸区域。图片的像素设为透明 for (int i = -10; i < 10; i++) { for (int j = -10; j < 10; j++) { if (i + newX >= 0 && j + newY >= 0 && i + newX <= before.getWidth() && j + newY <= before.getHeight()) alterBitmap.setPixel(i + newX, j + newY, Color.TRANSPARENT); } } iv_before.setImageBitmap(alterBitmap); break; } return true; } }); } }
3、效果图