撕衣服的简易实现

 

* 原理:两张图片叠加显示,手指移动的地方编程透明的
* 编写步骤:

1. 创建上面图的副本
2. 设置上面图片的imageview触摸监听
3. 把上面图片的副本编成透明的
4. 给上面的imageview设置修改的图片

package com.example.syf;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity {

   /**
    *
      1. 创建上面图的副本
      2. 设置上面图片的imageview触摸监听
      3. 把上面图片的副本编程透明的
      4. 给上面的imageview设置修改的图片
    */
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final ImageView iv = (ImageView) findViewById(R.id.iv);
      //创建上面图的副本
      Bitmap srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.waiyi);
      Paint paint = new Paint();
      final Bitmap copyBmp = Bitmap.createBitmap(srcBmp.getWidth(),srcBmp.getHeight(),srcBmp.getConfig());
      Canvas canvas = new Canvas(copyBmp);
      canvas.drawBitmap(srcBmp, new Matrix(), paint);
      //设置上面图片的imageview触摸监听
      iv.setOnTouchListener(new OnTouchListener() {
         @Override
         public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                //手指移动时,把上面图片的副本编成透明的
                for (int i = -5; i < 5; i++) {
                   for (int j = -5; j < 5; j++) {
                      try {
                         copyBmp.setPixel((int)event.getX()+i, (int)event.getY()+j, Color.TRANSPARENT);
                      } catch (Exception e) {
                      }
                   }
                }
            }
            // 给上面的imageview设置修改的图片
            iv.setImageBitmap(copyBmp);
            return true;
         }
      }); 

   }

}

 

posted on 2017-02-20 20:58  LoaderMan  阅读(351)  评论(0编辑  收藏  举报

导航