«

把android 中图片局部区域改为透明

时间:2024-3-2 19:38     作者:韩俊     分类: Android


import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.ImageView;





public class MainActivity extends Activity {

private ImageView iv_after;

private ImageView iv_before;

private Bitmap beforeBitmap;

private Canvas canvas;





@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

this.iv_after = (ImageView) this.findViewById(R.id.after);

this.iv_before = (ImageView) this.findViewById(R.id.before);





// decodeResource()创建出来的图片属性为只读

Bitmap after = BitmapFactory.decodeResource(getResources(),

R.drawable.after);

Bitmap before = BitmapFactory.decodeResource(getResources(),

R.drawable.before);





// 创建一张可编辑的空白图片

this.beforeBitmap = Bitmap.createBitmap(before.getWidth(),

before.getHeight(), before.getConfig());



this.canvas = new Canvas(beforeBitmap);

this.canvas.drawBitmap(before, new Matrix(), new Paint());



this.iv_after.setImageBitmap(after);

this.iv_before.setImageBitmap(beforeBitmap);





this.iv_before.setOnTouchListener(new OnTouchListener() {





@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_MOVE:





int x = (int) event.getX();

int y = (int) event.getY();

for (int i = -18; i <= 18; i++) {

for (int j = -18; j <= 18; j++) {

if (x + i >= 0 && y + j >= 0

&& x + i < beforeBitmap.getWidth()

&& y + j < beforeBitmap.getHeight()) {

beforeBitmap.setPixel(x + i, y + j,

Color.TRANSPARENT);





}





}

}

iv_before.setImageBitmap(beforeBitmap);





break;





}

return true;

}

});





}




}


整理自ppt 源码

标签: android

热门推荐