1、视图
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 6 tools:context=".MainActivity" > 7 8 <ImageView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:id="@+id/iv_back" /> 12 13 <ImageView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:id="@+id/iv_up" /> 17 18 </FrameLayout>
2、实现代码
1 package com.example.girl; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.BitmapFactory.Options; 8 import android.graphics.Canvas; 9 import android.graphics.Color; 10 import android.graphics.Matrix; 11 import android.graphics.Paint; 12 import android.view.Menu; 13 import android.view.MotionEvent; 14 import android.view.View; 15 import android.view.View.OnTouchListener; 16 import android.widget.ImageView; 17 18 public class MainActivity extends Activity { 19 private ImageView iv_back; 20 private ImageView iv_up; 21 private Bitmap alterBitmap; 22 private Canvas canvas; 23 private Paint paint; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 iv_back = (ImageView) findViewById(R.id.iv_back); 30 iv_up = (ImageView) findViewById(R.id.iv_up); 31 32 BitmapFactory.Options opts = new Options(); 33 opts.inSampleSize = 1; 34 35 Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.back, opts); 36 Bitmap up = BitmapFactory.decodeResource(getResources(), R.drawable.up, opts); 37 38 //可以修改的空白的bitmap 39 alterBitmap = Bitmap.createBitmap(up.getWidth(), up.getHeight(), up.getConfig()); 40 41 canvas = new Canvas(alterBitmap);//画布 42 paint = new Paint();//画笔 43 paint.setStrokeWidth(5); 44 paint.setColor(Color.BLACK); 45 canvas.drawBitmap(up, new Matrix(), paint);//把up的图片放进来,这样上面才可以看到覆盖图 46 iv_back.setImageBitmap(back); 47 iv_up.setImageBitmap(alterBitmap); 48 49 iv_up.setOnTouchListener(new OnTouchListener(){ 50 int startX; 51 int startY; 52 @Override 53 public boolean onTouch(View arg0, MotionEvent event) { 54 // TODO Auto-generated method stub 55 switch(event.getAction()){ 56 case MotionEvent.ACTION_DOWN: 57 startX = (int)event.getX(); 58 startY = (int)event.getY(); 59 break; 60 case MotionEvent.ACTION_MOVE: 61 int newX = (int)event.getX(); 62 int newY = (int)event.getY(); 63 for(int i=-5; i < 5; i++){ 64 for(int j=-5; j < 5; j++){ 65 if(((i+newX) > 0 && (i+newX) < iv_up.getWidth()) && 66 ((j+newY) > 0 && (j+newY) < iv_up.getHeight())//关于做大于0并且小于图片宽高的判断,防止越界 67 ){ 68 alterBitmap.setPixel(i+newX, j+newY, Color.TRANSPARENT);//把颜色设为透明的 69 } 70 } 71 } 72 System.out.println(newX + "----" + newY); 73 iv_up.setImageBitmap(alterBitmap); 74 break; 75 case MotionEvent.ACTION_UP: 76 break; 77 } 78 return true; 79 } 80 81 }); 82 } 83 84 85 }
时间: 2024-10-15 06:54:20