天天看点

撕衣服小游戏代码

public class MainActivity extends Activity {

private ImageView iv;

// 可以修改的位图

private Bitmap alertBitmap;

private Canvas canvas;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

iv = (ImageView) findViewById(R.id.iv);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.pre);

// 创建一个空白的原图的拷贝

alertBitmap = Bitmap.createBitmap(bitmap.getWidth(),

bitmap.getHeight(), bitmap.getConfig());

canvas = new Canvas(alertBitmap);

Paint paint = new Paint();

paint.setColor(Color.BLACK);

canvas.drawBitmap(bitmap, new Matrix(), paint);

iv.setImageBitmap(alertBitmap);

iv.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:// 手指按下屏幕

System.out.println("action down");

break;

case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动

int x = (int) event.getX();

int y = (int) event.getY();

System.out.println("设置("+x+","+y+")透明颜色");

for(int i=-4;i<5;i++){

for(int j=-4;j<5;j++){

try{

alertBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);

}catch (Exception e) {

// TODO: handle exception   //此处如果不try catch的话,会发生异常,因为当手指滑动到图片外面的时候,x,y的值有可能小于0,这个时候                                                                                                                       //就会发生异常,try catch之后就没事了

}

}

}

iv.setImageBitmap(alertBitmap);

break;

case MotionEvent.ACTION_UP:// 手指离开屏幕

MediaPlayer.create(getApplicationContext(), R.raw.higirl).start();

break;

}

return true;//可以重复循环的处理事件,如果为false,安卓就会认为一次动作始终没有结束,仍然在继续进行,就不会有后面的处理了。

}

});

}

}

上面的代码撕衣服的时候 画笔是正方形的,变成圆形的更好,,我的写法是这样的:

case MotionEvent.ACTION_MOVE:

int x=(int) event.getX();

int y=(int) event.getY();

for(int i=-5;i<6;i++)

for(int j=-5;j<6;j++){

try {

if((i-x)*(i-x)+(j-y)*(j-y)<5){

copyBitmap.setPixel(x, y, Color.TRANSPARENT);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

后来发现,这个写法是错误的,逻辑错误,应该写成

case MotionEvent.ACTION_MOVE:

int x=(int) event.getX();

int y=(int) event.getY();

for(int i=-5;i<6;i++)

for(int j=-5;j<6;j++){

try {

//if((x*x+y*y)<20)

{

copyBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

iv.setImageBitmap(copyBitmap);   //千万不要忘记这一步,否则看不到变化

break;

可是这样写还是看不到变化,不知道什么原因。

布局文件:

<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:layout_width="wrap_content"

        android:layout_height="wrap_content"

       android:src="@drawable/after"

       android:layout_centerHorizontal="true"

       android:layout_centerVertical="true"/>

    <ImageView 

        android:id="@+id/iv_pre"

       android:layout_width="wrap_content"

        android:layout_height="wrap_content"

          android:layout_centerHorizontal="true"

       android:layout_centerVertical="true"

        />

</RelativeLayout>

imageView里面的wrap_content不能写成fill_parent,否则图片填充整个屏幕之后,图片被放大,相当于图片的尺寸改变了,这个方法就行不通了。

转载于:https://www.cnblogs.com/berylqliu/p/6261542.html

继续阅读