Android multiple ImageView image stacked transparent area Click Event penetration

Source: Internet
Author: User

Multiple transparent image layers are often used, but click events of different images need to be obtained. This article implements click events in the transparent area of images.
:

1. The size of the two images is the same. Put them in the drawable directory:
Figure 1: transparent.png

 
Figure 2: transparent2.png

 
2. layout file, test. xml:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: gravity = "center"
Android: id = "@ + id/mainLayout">

<ImageView android: id = "@ + id/ImageView01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/transparent"/>

<ImageView android: id = "@ + id/ImageView02"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/transparent2"/>

</FrameLayout>

 
3. Upload the Activity file MainActivity. java
 
[Java]
Package org. shuxiang. test;
 
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. drawable. BitmapDrawable;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. Window;
Import android. view. View. OnClickListener;
Import android. view. View. OnTouchListener;
Import android. widget. ImageView;
 
Public class MainActivity extends Activity
{
Private ImageView iv1;
Private ImageView iv2;
Private Bitmap bitmap1, bitmap2;
Private boolean iv1Transparent = false;
Private boolean iv2Transparent = false;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. test );
 
Iv1 = (ImageView) findViewById (R. id. ImageView01 );
Iv2 = (ImageView) findViewById (R. id. ImageView02 );
Bitmap1 = (BitmapDrawable) (iv1.getDrawable (). getBitmap ();
Bitmap2 = (BitmapDrawable) (iv2.getDrawable (). getBitmap ();

Iv1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (iv1Transparent)
{
Log. I ("test", "Figure 1 transparent area ");
}
Else
{
Log. I ("test", "Figure 1 click ");
}
}

});

Iv1.setOnTouchListener (new OnTouchListener ()
{
@ Override
Public boolean onTouch (View arg0, MotionEvent arg1)
{
// TODO Auto-generated method stub
If (bitmap1.getPixel (int) (arg1.getX (), (int) arg1.getY () = 0)
{
Log. I ("test", "Figure 1 transparent area ");
Iv1Transparent = true; // set the transparent area to true.
}
Else
{
Log. I ("test", "Figure 1 entity region ");
Iv1Transparent = false;
}
Return false;
}
});

Iv2.setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (iv2Transparent)
{
Log. I ("test", "Figure 2 transparent area ");
}
Else
{
Log. I ("test", "Figure 2 click ");
}
}

});

Iv2.setOnTouchListener (new OnTouchListener ()
{
@ Override
Public boolean onTouch (View v, MotionEvent event ){
// TODO Auto-generated method stub
If (bitmap2.getPixel (int) (event. getX (), (int) event. getY () = 0)
{
Log. I ("test", "Figure 2 transparent area ");
Iv2Transparent = true; // set the transparent area to true.
Iv1.dispatchTouchEvent (event );
}
Else
{
Log. I ("test", "Figure 2 entity region ");
Iv2Transparent = false;
}
Return false;
}
});
}
}
 

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.