Processing of bitmap image transparency in Android (taking tearing beautiful clothes as an example ),

Source: Internet
Author: User

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 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.