美文网首页
【Android程序开发】^爱情橡皮擦^小demo

【Android程序开发】^爱情橡皮擦^小demo

作者: 宁晓鸯 | 来源:发表于2019-08-26 01:07 被阅读0次

    6.爱情橡皮擦"小demo

    小声哔哔一下^^,为什么会叫"爱情橡皮擦"小demo呢,因为我找了两张情侣的图,运行效果:将覆盖在上面的情侣擦掉后,会出现他的另一半哟~~

    demo思路:使用透明色去替换原有图片的像素,立刻获取替换之后的图片,将图片显示在ImageView上

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:id="@+id/fl_main">
    
      //背景图片
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/c">
    
        </ImageView>
      //显示操作后的图片
        <ImageView
            android:id="@+id/iv_foreground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    
            >
        </ImageView>
        </FrameLayout>
    
    public class MainActivity extends AppCompatActivity {
    
    
        ImageView foreground;
        Bitmap orgBitmap;
        Bitmap copyBitmap;
        Canvas canvas;
        Paint paint;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 配置界面
            setContentView(R.layout.activity_main);
    
            // 找到容器视图里面的图片视图控件
            //findViewById
            foreground = findViewById(R.id.iv_foreground);
    
            // 将需要操作的图片读取出来 Bitmap
            // BitmapFactory 管理位图
            // decodeResource 从工程资源路径去生成一张位图
            // getResources获取工程资源
            // R.drawable.xiugai 访问资源路径下 drawable 里面的一个文件
            orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
    
            // 操作这张图片 用透明色替换
            // 不能操作原图 只能拷贝一份
            copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(), orgBitmap.getHeight(), orgBitmap.getConfig());
    
            // 创建一个Canvas 画布
            canvas = new Canvas(copyBitmap);
    
            // 创建一个画笔
            paint = new Paint();
    
            // 创建一个矩阵
            Matrix matrix = new Matrix();
            // 旋转图片
            // matrix.setRotate(90,240,400);
    
            // 平移
            // matrix.setTranslate(100,0);
    
            // 翻转 set作用一次 post作用多次
            // matrix.setScale(0.5f,1f);
            // matrix.postTranslate(orgBitmap.getWidth(),0);
    
            // 画一幅图
            canvas.drawBitmap(orgBitmap, matrix, paint);
    
            // 显示图片
            foreground.setImageBitmap(copyBitmap);
    
            // 给前景图片添加touch事件
            // 当有触摸事件发生 系统会监听并回调该事件
            foreground.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    // 获取当前事件
                    int action = motionEvent.getAction();
    
                    // 判断状态
                    if (action == MotionEvent.ACTION_MOVE) {
                        // 获取触摸点的坐标
                        int x = (int) motionEvent.getX();
                        int y = (int) motionEvent.getY();
                        for (int i = 0; i < 100; i++) {
                            for (int j = 0; j < 200; j++) {
                                copyBitmap.setPixel(x + i, y + j, Color.TRANSPARENT);
                            }
                        }
    
                        foreground.setImageBitmap(copyBitmap);
    
                    }
                    return true;
                }
            });
        }
    

    存在问题:

    1. ⼿画出屏幕 x,y超出图⽚尺⼨,越界就闪退
    2. 触摸点位置和划线位置不⼀致 ,图⽚尺⼨和屏幕尺⼨不匹配

    有一件事很抱歉,由于我的手机问题,我暂时无法将最终效果展示出来,等我解决了手机问题,我会立刻补上的


    image

    相关文章

      网友评论

          本文标题:【Android程序开发】^爱情橡皮擦^小demo

          本文链接:https://www.haomeiwen.com/subject/gnslectx.html