Android -- ×××

首先佈局要用幀佈局

<FrameLayout 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:id="@+id/after"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/b0"
          /> 
       <ImageView
       android:id="@+id/befor"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       />
</FrameLayout>


MainActivity:

public class MainActivity extends Activity {
                                                            
    private ImageView preImageView,aftImageView;
    private Canvas canvas;
    private Paint paint;
    private Bitmap alterbitmap;
                                                             
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*BitmapFactory.Options options =new Options();
        options.inSampleSize=2;*/     
        preImageView=(ImageView) findViewById(R.id.befor);
        aftImageView=(ImageView) findViewById(R.id.after);
                                                                 
        Bitmap befor=BitmapFactory.decodeResource(getResources(), R.drawable.a0);//只讀的
        Bitmap after=BitmapFactory.decodeResource(getResources(), R.drawable.b0);
        //可修改的Bitmap
        alterbitmap=Bitmap.createBitmap(befor.getWidth(), befor.getHeight(), befor.getConfig());
        canvas=new Canvas(alterbitmap);
        paint=new Paint();
        paint.setStrokeWidth(5);//設置畫筆的寬度
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(befor, new Matrix(), paint);
                                                                 
        //設置圖片
        preImageView.setImageBitmap(alterbitmap);
        aftImageView.setImageBitmap(after);
                                                                 
        preImageView.setOnTouchListener(new OnTouchListener() {
                                                                     
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                                                                         
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    int newx=(int) event.getX();
                    int newy=(int) event.getY();
                    //將觸摸點上下8個像素的位置設爲透明
                    for (int i = -8; i <8; i++) {
                        for (int j =-8; j < 8; j++) {
                            alterbitmap.setPixel(newx+i, newy+j, Color.TRANSPARENT);
                        }
                    }
                    preImageView.setImageBitmap(alterbitmap);
                    break;
                }             
                return true;
            }
        });
    }
}

運行結果都懂的,再次不在給出。呵呵(源碼太大,可到下載區下載

注:當手指點擊位置-8超出屏幕位置時會報一個異常,讀者可以自行解決。另外該程序在4.0版本手機運行正常,在2.3版本手機上運行擦除後背景一片漆黑(待解決)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章