Processing of bitmap picture transparency in Android (take the example of tearing beautiful clothes)

Source: Internet
Author: User

Principle: The two different effects of the picture in the same position, change the transparency of the image above, it can be achieved.

Layout file:

<Relativelayoutxmlns: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 ">    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:id= "@+id/iv_bottom" />    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:id= "@+id/iv_top" /></Relativelayout>

Mainactivity

 Packagecn.seanlou.stripclothes;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateImageView Ivbottom; PrivateImageView Ivtop; PrivateBitmap Imgblank; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Findview ();    Init (); }    /*** Initialize the contents of the control*/    Private voidinit () {bitmapfactory.options Options=Newbitmapfactory.options (); Options.insamplesize= 1; //gets the picture that is displayed in the image above, the Bitmapfactory.decoderesource () method gets the image encoded in RGB format and needs to be converted to ARGB format. Bitmap Imgtop =Bitmapfactory.decoderesource (Getresources (), r.mipmap.g1_up, options); //gets the picture shown in the following. Bitmap Imgbottom =Bitmapfactory.decoderesource (Getresources (), r.mipmap.g1_back, options); //Create a blank bitmap picture that shows the imgtop size of the picture above, the picture formatted in ARGB format. Imgblank =Bitmap.createbitmap (Imgtop.getwidth (), Imgtop.getheight (), Bitmap.Config.ARGB_4444); //Create the Imgblank as a canvas. Canvas Canvas =NewCanvas (Imgblank); //Paint the Imgtop on the canvasCanvas.drawbitmap (imgtop, 0, 0,NULL);        Ivtop.setimagebitmap (Imgblank);        Ivbottom.setimagebitmap (Imgbottom); Ivtop.setontouchlistener (NewMyontouchlistener ()); }    Private voidFindview () {ivtop=(ImageView) Findviewbyid (r.id.iv_top); Ivbottom=(ImageView) Findviewbyid (R.id.iv_bottom); }    Private classMyontouchlistenerImplementsView.ontouchlistener {@Override Public BooleanOnTouch (View V, motionevent event) {if(event.getaction () = =motionevent.action_move) {                intx = (int) Event.getx (); inty = (int) event.gety (); LOG.I ("Location", "Current position x:" + x + ", Y:" +y);  for(inti = x-20; I < x + 20; i++) {                     for(intj = y-20; J < Y + 20; J + +) {                        //dealing with picture boundary problems                        if(I >= 0 && i < imgblank.getwidth () && J >= 0 && J <imgblank.getheight ()) {                            //set the current point to transparentImgblank.setpixel (i, J, color.transparent); }                    }                }                //Show PicturesIvtop.setimagebitmap (Imgblank); }            return true; }    }}

Processing of bitmap picture transparency in Android (take the example of tearing beautiful clothes)

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.