Processing of bitmap image transparency in Android (taking tearing beautiful clothes as an example ),
Principle: place two images with different effects in the same position, and change the transparency of the image.
Layout file:
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/iv_bottom" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/iv_top" /></RelativeLayout>
MainActivity
Package cn. seanlou. stripclothes; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. OS. bundle; import android. util. log; import android. view. motionEvent; import android. view. view; import android. widget. imageView; public class MainActivity extends Activity {private ImageView ivBottom; Private ImageView ivTop; private Bitmap imgBlank; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findView (); init () ;}/ *** initialize the control content */private void init () {BitmapFactory. options options = new BitmapFactory. options (); options. inSampleSize = 1; // obtain the image shown above, BitmapFactory. the image obtained by the decodeResource () method is encoded in RGB format and requires Convert to ARGB format. Bitmap imgTop = BitmapFactory. decodeResource (getResources (), R. mipmap. g0000up, options); // obtain the image shown below. Bitmap imgBottom = BitmapFactory. decodeResource (getResources (), R. mipmap. g1_back, options); // create a blank Bitmap image that is displayed on the above image imgTop. The image format is set to ARGB. ImgBlank = Bitmap. createBitmap (imgTop. getWidth (), imgTop. getHeight (), Bitmap. Config. ARGB_4444); // create imgBlank as a canvas. Canvas canvas = new Canvas (imgBlank); // draw imgTop on the canvas. drawBitmap (imgTop, 0, 0, null); ivTop. setImageBitmap (imgBlank); ivBottom. setImageBitmap (imgBottom); ivTop. setOnTouchListener (new MyOnTouchListener ();} private void findView () {ivTop = (ImageView) findViewById (R. id. iv_top); ivBottom = (ImageView) findViewById (R. id. iv_bottom);} private class MyOnTouchListener implements View. onTouchListener {@ Override public boolean onTouch (View v, MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_MOVE) {int x = (int) event. getX (); int y = (int) event. getY (); Log. I ("location", "current location x:" + x + ", y:" + y); for (int I = x-20; I <x + 20; I ++) {for (int j = y-20; j <y + 20; j ++) {// process the image boundary. if (I> = 0 & I = 0 & j