撕衣服小游戏代码

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,否则图片填充整个屏幕之后,图片被放大,相当于图片的尺寸改变了,这个方法就行不通了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的面向对象衣服管理系统的示例代码: ``` // 衣服类 public class Cloth { private String name; private String style; private String color; private String size; public Cloth(String name, String style, String color, String size) { this.name = name; this.style = style; this.color = color; this.size = size; } public String getName() { return name; } public String getStyle() { return style; } public String getColor() { return color; } public String getSize() { return size; } @Override public String toString() { return "Cloth{" + "name='" + name + '\'' + ", style='" + style + '\'' + ", color='" + color + '\'' + ", size='" + size + '\'' + '}'; } } // 衣柜类 public class Closet { private List<Cloth> clothes; public Closet() { clothes = new ArrayList<>(); } public void addCloth(Cloth cloth) { clothes.add(cloth); System.out.println("成功添加衣服:" + cloth.getName()); } public void removeCloth(String name) { for (int i = 0; i < clothes.size(); i++) { if (clothes.get(i).getName().equals(name)) { clothes.remove(i); System.out.println("成功移除衣服:" + name); return; } } System.out.println("未找到衣服:" + name); } public void listClothes() { if (clothes.size() == 0) { System.out.println("衣柜为空"); } else { System.out.println("衣柜中的衣服:"); for (Cloth cloth : clothes) { System.out.println(cloth); } } } public void findCloth(String name) { for (Cloth cloth : clothes) { if (cloth.getName().equals(name)) { System.out.println("找到衣服:" + cloth); return; } } System.out.println("未找到衣服:" + name); } } // 主程序 public class Main { public static void main(String[] args) { Closet closet = new Closet(); Cloth cloth1 = new Cloth("T恤", "休闲", "白色", "L"); Cloth cloth2 = new Cloth("衬衫", "正装", "蓝色", "M"); Cloth cloth3 = new Cloth("牛仔裤", "休闲", "黑色", "28"); closet.addCloth(cloth1); closet.addCloth(cloth2); closet.addCloth(cloth3); closet.listClothes(); closet.removeCloth("T恤"); closet.findCloth("衬衫"); closet.findCloth("短裤"); } } ``` 在这个示例中,我们定义了 `Cloth` 和 `Closet` 两个类,分别表示衣服和衣柜。`Cloth` 类包含了衣服的属性和构造方法,`Closet` 类包含了衣柜中衣服的列表和对这些衣服进行管理的方法。在 `Main` 类中,我们实例化了 `Closet` 对象,并添加了三件衣服到衣柜中,然后调用了一些方法对衣柜中的衣服进行操作。 需要注意的是,这个示例只是一个简单的实现,实际的衣服管理系统可能需要更多的属性和方法来满足实际需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值