Android之Canvas撕衣服

来源:互联网 发布:java dataoutputstream 编辑:程序博客网 时间:2024/04/27 04:18

前几篇都涉及到了Canvas,Paint,Bitmap的结合使用,这里就不多说了~

现在我要写的是撕衣服的这个项目~

其实这个项目只是涉及到了一张图片到另一张图片的变换偷笑

先看效果图:


布局文件也是俩张图片,一张穿了衣服的图片在没穿衣服的图片上面~

布局代码:

<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"    >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/after"        android:layout_centerInParent="true"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@drawable/pre"         android:id="@+id/iv"/></RelativeLayout>
MainActivity:

public class MainActivity extends Activity {private ImageView iv;private Bitmap baseBitmap,copyBitmap;private Canvas canvas;private Paint paint;private Matrix matrix;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv=(ImageView) findViewById(R.id.iv);baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pre);copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());canvas=new Canvas(copyBitmap);matrix=new Matrix();paint=new Paint();canvas.drawBitmap(baseBitmap, matrix, paint);    iv.setImageBitmap(copyBitmap);//imageView所在区域被触摸的时候回调iv.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN://按下  1次break;case MotionEvent.ACTION_MOVE://移动  0-N次//getX和getY是以控件左上角为原点的//getRawX和getRawY是以屏幕左上角为原点的int moveX=(int)event.getX();int moveY=(int)event.getY();//event.getRawX();//event.getRawY();//针对单个像素点去说的try {for(int i=-20;i<21;i++){for(int j=-20;j<21;j++){copyBitmap.setPixel(moveX+i, moveY+j, Color.TRANSPARENT);}}//TRANSPARENT是透明//copyBitmap.setPixel(moveX, moveY, Color.TRANSPARENT);iv.setImageBitmap(copyBitmap);} catch (Exception e) {// TODO: handle exception}break;case MotionEvent.ACTION_UP://抬起  1次break;default:break;}return true;//方法被消费}});}}

源码下载

0 0
原创粉丝点击