package com.gyarmy.setcloth; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class MainActivity extends Activity implements OnTouchListener { private ImageView iv_pre; private ImageView iv_after; private Bitmap copyBitmap; Paint paint ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_after = (ImageView) findViewById(R.id.iv_after); iv_pre = (ImageView) findViewById(R.id.iv_pre); iv_after.setImageResource(R.drawable.after14); iv_pre.setImageResource(R.drawable.pre14); iv_pre.setOnTouchListener(this); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pre14); copyBitmap = bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(copyBitmap); paint = new Paint(); //paint.setStrokeWidth(10); Matrix matrix = new Matrix(); //绘制图片 canvas.drawBitmap(bitmap, matrix, paint); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("按下............."); int downX = (int) (event.getX()+0.5f); int downY = (int) (event.getY()+0.5f); clearPicture(downX,downY); break; case MotionEvent.ACTION_MOVE: //设置透明色 int moveX = (int) (event.getX()+0.5f); int moveY = (int) (event.getY()+0.5f); clearPicture(moveX,moveY); break; case MotionEvent.ACTION_UP: System.out.println("UP UP UP............."); break; default: break; } return true; } public void clearPicture(int x,int y){ //设置新图片 for(int i=-10;i<=10;i++) { for(int j=-10;j<=10;j++){ //要求圆形,并且不报错 try { if((i*i+j*j)<=100){ copyBitmap.setPixel(x+i, y+j, Color.TRANSPARENT); } } catch (Exception e) { // TODO: handle exception } } } iv_pre.setImageBitmap(copyBitmap); } }
0则评论给“撕衣服图片效果的实现”