首页 > 技术文章 > 撕衣服

324sige 2016-07-28 15:48 原文

 

 

方法

1、准备两张图片,重叠放置

2、当手在屏幕移动时,获取坐标

3、把第一张图片的相应位置设为透明

 注意:

getRawX:相对于屏幕的X轴坐标
getX:相对于控件的X轴坐标

package com.example.mysecond;

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.widget.ImageView;

public class MainActivity extends Activity {

protected ImageView imageView;
protected Bitmap bmcopy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView= (ImageView) findViewById(R.id.top);

//BitmapFactory.decodeResource(resource,id)这个带两个参数的方法:
//第一个参数是包含你要加载的位图资源文件的对象(一般写成 getResources()就ok了)
//第二个时你需要加载的位图资源的Id。
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(),R.mipmap.b);
//Returns a mutable bitmap with the specified width and height. Its
// initial density is as per {@link #getDensity}.
//返回一个可变的位图用指定的宽度和高度。初始密度是按照其
bmcopy=Bitmap.createBitmap(bmSrc.getWidth(),bmSrc.getHeight(),bmSrc.getConfig());
//画笔
Paint paint=new Paint();
//画板,把可变的位图作为操作对象
Canvas canvas=new Canvas(bmcopy);
//在可变的位图上画图
canvas.drawBitmap(bmSrc,new Matrix(),paint);

/**
* 1、获取到点击的位置
* 2、内存中有一张图片的备份,把点击的位置周围,全部设为透明
* 3、把内存中的图片粘贴到视图上
*/
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
{
int x= (int) event.getX();
int y= (int) event.getY();

for(int i=-20;i<=20;i++){
for(int j=-20;j<=20;j++){
//不能有等于号,如果宽度像素为一百,则图片的像素是0--99
if(x+i>0 && x+i<bmcopy.getWidth() && y+j>0 && y+j<bmcopy.getHeight()){
bmcopy.setPixel(x+i,y+j, Color.TRANSPARENT);
}
}
}
imageView.setImageBitmap(bmcopy);
break;
}
}
return true;
}
});
}

}

  

 

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.mysecond.MainActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bottom"
        android:src="@mipmap/a"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/top"
        android:src="@mipmap/b"/>

</RelativeLayout>

  

推荐阅读