宅男神器——撕衣服

来源:互联网 发布:origin for mac破解版 编辑:程序博客网 时间:2024/04/26 12:54

给大家介绍一个简单的好玩的app应用程序--撕衣服

思路:

一、在布局文件中:

              在中间放两张图片,上面一张为原图(撕衣服之前)的拷贝,下面一张为撕衣服之后的图片

二、在MainActivity中:

              关心上面一张图片即撕衣服之前的控件,通过位图工厂获取它的位图,拷贝位图,然后创建画板和画笔,开始作画,然后将拷贝并画好的位图放到原图的位置上。

                        给原图的拷贝的控件设置屏幕触摸监听事件,匿名内部类的方法重写onTouch方法,最后return  true(事件结束被消费掉了)

 

在src的Java代码 com.mycode.takeoff.MainActivity

    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 {        private ImageView iv_pre;        private Bitmap alterBitmap;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            iv_pre = (ImageView) findViewById(R.id.iv_pre);            // 原图.            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pre);//原图的位图            alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),                    bitmap.getHeight(), bitmap.getConfig());  //原图位图的拷贝                       Canvas canvas = new Canvas(alterBitmap);       //创建画板            Paint paint = new Paint();       //创建画笔            canvas.drawBitmap(bitmap, new Matrix(), paint);   //开始作画            iv_pre.setImageBitmap(alterBitmap);       //上面一张图片为撕衣服之前拷贝后的图片            iv_pre.setOnTouchListener(new OnTouchListener() { //屏幕触摸监听事件                int x;                int y;                @Override                public boolean onTouch(View v, MotionEvent event) {                    switch (event.getAction()) {                    case MotionEvent.ACTION_DOWN:      //手指按上去的时候                        x = (int) event.getX();      //得到X方向的值                        y = (int) event.getY();      //得到Y方向的值                        for(int i=-3;i<4;i++){                            for(int j=-3;j<4;j++){                                if(Math.sqrt(i*i+j*j)<=3){   //设置点击触摸后的原图像素为矩形透明且矩形半径的值不大于3                                    try {                                        alterBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);                                    } catch (Exception e) {                                        e.printStackTrace();                                    }                                }                            }                        }                        iv_pre.setImageBitmap(alterBitmap);                        break;                    case MotionEvent.ACTION_MOVE:      //手指在屏幕上移动的时候                        x = (int) event.getX();                        y = (int) event.getY();                        for(int i=-3;i<4;i++){                                    for(int j=-3;j<4;j++){                                if(Math.sqrt(i*i+j*j)<=3){                                    try {                                        alterBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);                                    } catch (Exception e) {                                        e.printStackTrace();                                    }                                }                            }                        }                        iv_pre.setImageBitmap(alterBitmap);                        break;                    case MotionEvent.ACTION_UP:    //手指离开屏幕的时候                        break;                        }                    return true;// 事件结束被消费掉了                }            });        }    }

 

在res/layout/activity_main.xml的代码实现

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".MainActivity" >        <ImageView            android:id="@+id/iv_after"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:src="@drawable/after" />        <ImageView            android:id="@+id/iv_pre"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true" />    </RelativeLayout>
1 0
原创粉丝点击