Wednesday, February 19, 2014

Android screen click event


Shetenglebantian own, did not make it, so take advantage of the occasion to work, ask a question, please help solve grateful.

Desired effect: By clicking on the screen, the Gallery to show or hide the controls, the question now is, thanks to the touch screen to switch pictures ImageSwitcher feature event click on the screen do not know how to get, if you imageSwitcher.setOnTouchListener (this) statement commented, events screen, click on it.

The following code can run, upload pictures bad Oh, bother everyone to add their own images, to help debug it, thank you:
 
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/ **
* ImageSwitcherActivity.java
* @ Author cloay
* 2011-7-16
* /
public class Album extends Activity implements OnItemSelectedListener, ViewFactory, OnTouchListener {
private ImageSwitcher imageSwitcher;
private Gallery gallery;

private int selectedTag = 0;
private int downX, upX;

private GestureDetector gestureScanner;

private Integer [] imagesId = new Integer [] {R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.f, R.drawable.g};
private Integer [] selectId = new Integer [] {R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.f, R.drawable.g};

private static final String TAG = "Album";
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);

setContentView (R.layout.main);

imageSwitcher = (ImageSwitcher) findViewById (R.id.switcher);
imageSwitcher.setFactory (this);
Animation
/ / set the picture when switchingimageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this, android.R.anim.fade_out));
imageSwitcher.setOnTouchListener (this);

gallery = (Gallery) findViewById (R.id.gallery);
/ / Custom ImageAdapter inherited from BaseAdapter, is an internal class
gallery.setAdapter (new ImageAdapter (this));
gallery.setOnItemSelectedListener (this);

gestureScanner = new GestureDetector (new SimpleOnGestureListener ()
{

@ Override
public boolean onSingleTapConfirmed (MotionEvent e)
{
Log.i (TAG, "click");
return false;
}
});
}
@ Override
public View makeView () {
ImageView image = new ImageView (this);
image.setScaleType (ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams (new ImageSwitcher.LayoutParams (
LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
return image;
}

@ Override
public void onItemSelected (AdapterView arg0, View arg1, int arg2,
long arg3) {
selectedTag = arg2;
imageSwitcher.setImageResource (imagesId [arg2]);
}

@ Override
public void onNothingSelected (AdapterView arg0) {

}
public class ImageAdapter extends BaseAdapter {
private Context context;
int galleryItemBackground;
public ImageAdapter (Context c) {
context = c;
TypedArray typeArray = obtainStyledAttributes (R.styleable.Gallery);
galleryItemBackground = typeArray.getResourceId (R.styleable.Gallery_android_galleryItemBackground, 0);
typeArray.recycle ();
}
@ Override
public int getCount () {
/ / Return selectId [] length
return selectId.length;
}

@ Override
public Object getItem (int position) {
return position;
}

@ Override
public long getItemId (int position) {
return position;
}

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView (context);
/ / Set the resource picture
imageView.setImageResource (selectId [position]);
imageView.setAdjustViewBounds (true); / / allowed to adjust the border
/ / Set the bottom of the gallery, adaptive size
imageView.setLayoutParams (new Gallery.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
/ / Set the background
galleryimageView.setBackgroundResource (galleryItemBackground);
return imageView;
}

}
@ Override
public boolean onTouch (View v, MotionEvent event) {

if (event.getAction () == MotionEvent.ACTION_DOWN)
{
downX = (int) event.getX (); / / get coordinates x
when pressedreturn true;
}
else if (event.getAction () == MotionEvent.ACTION_UP)
{
upX = (int) event.getX (); / / get coordinates x release when;
if (upX - downX> 100)
{
/ / Drag left and right, before that see a
/ / If this is the first, then go to the tail
if (gallery.getSelectedItemPosition () == 0)
selectedTag = gallery.getCount () - 1;
else
selectedTag = gallery.getSelectedItemPosition () - 1;

imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_out));
}
else if (downX - upX> 100)
{
/ / Right onto the left, that look after a
/ / If this is the end, then go to the first
if (gallery.getSelectedItemPosition ()
== (Gallery.getCount () - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition () + 1;
imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_out));
}

/ / Change the gallery pictures selected automatically trigger ImageSwitcher of setOnItemSelectedListener
gallery.setSelection (selectedTag, true);
return true;
}

return false;
}

@ Override
public boolean onTouchEvent (MotionEvent event) {
return gestureScanner.onTouchEvent (event);
}
}


res \ layout \ main.xml
 

android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: orientation = "vertical">

android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: layout_alignParentLeft = "true"
android: layout_alignParentTop = "true" />

android: layout_width = "fill_parent"
android: layout_alignParentLeft = "true"
android: layout_height = "60dp"
android: spacing = "15dp"
android: layout_alignParentBottom = "true"
android: gravity = "center_vertical"
android: visibility = "invisible" />



res \ values ​​\ attrs.xml
 








res \ anim \ push_left_in.xml
 

<-! Left into the gradient effect ->

android: duration = "1500"
android: fromXDelta = "100% p"
android: toXDelta = "0" />

android: duration = "1500"
android: fromAlpha = "0.1"
android: toAlpha = "1.0" />



res \ anim \ push_left_out.xml
 

<-! Right out of transition effects ->

android: duration = "1500"
android: fromXDelta = "0"
android: toXDelta = "-100% p" />

android: duration = "1500"
android: fromAlpha = "1.0"
android: toAlpha = "0.1" />



res \ anim \ push_right_in.xml
 

<-! Right into the gradient effect ->

android: duration = "1500"
android: fromXDelta = "-100% p"
android: toXDelta = "0" />

android: duration = "1500"
android: fromAlpha = "0.1"
android: toAlpha = "1.0" />



res \ anim \ push_right_out.xml
 

<-! Right out of transition effects ->

android: duration = "1500"
android: fromXDelta = "0"
android: toXDelta = "100% p" />

android: duration = "1500"
android: fromAlpha = "1.0"
android: toAlpha = "0.1" />

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
OnClickListener a click event listener interfaces
Reply:
Inside the onTouch return true; instead return false;

 
@ Override
public boolean onTouch (View v, MotionEvent event) {

if (event.getAction () == MotionEvent.ACTION_DOWN)
{
downX = (int) event.getX (); / / get coordinates x
when pressedreturn false;
}
else if (event.getAction () == MotionEvent.ACTION_UP)
{
upX = (int) event.getX (); / / get coordinates x release when;
if (upX - downX> 100)
{
/ / Drag left and right, before that see a
/ / If this is the first, then go to the tail
if (gallery.getSelectedItemPosition () == 0)
selectedTag = gallery.getCount () - 1;
else
selectedTag = gallery.getSelectedItemPosition () - 1;

imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_out));
}
else if (downX - upX> 100)
{
/ / Right onto the left, that look after a
/ / If this is the end, then go to the first
if (gallery.getSelectedItemPosition ()
== (Gallery.getCount () - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition () + 1;
imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_out));
}

/ / Change the gallery pictures selected automatically trigger ImageSwitcher of setOnItemSelectedListener
gallery.setSelection (selectedTag, true);
return false;
}

return false;
}


Reply:
if (upX - downX> 100)
These things you do not bracketed ah
if ((upX - downX)> 100)
Reply:
reference to the third floor of the reply:
if (upX - downX> 100)
These things you do not bracketed ah
if ((upX - downX)> 100)


- The symbol of high priority or> higher priority.
Say you answer the question I asked with no association. Oh!
Reply:
cited a floor reply:
OnClickListener clicking monitoring interface events

This can be used to click on the screen it?
Reply:
reference to the second floor of the reply:
inside the onTouch return true; instead return false;


Java code


@ Override
public boolean onTouch (View v, MotionEvent event) {

if (event.getAction () == MotionEvent.ACTION_DOWN)
......

Thank you, I tried, useless, making touch-and-click is invalid.
Do you have this Demo it?
Reply:
references 6th Floor reply:
Thank you, I tried, useless, making the touch and click all fail.
Do you have this Demo it?


Thought you had added a gallery to display the hidden code!

Add the following code to your original code:
upX = (int) event.getX (); / / get coordinates x release when;
The front

 
if (gallery.getVisibility () == 4)
{
gallery.setVisibility (0);
}
else
{
gallery.setVisibility (4);
}

Reply:
The following are not handled by the slide.
 
else if (event.getAction () == MotionEvent.ACTION_UP)
{
upX = (int) event.getX (); / / get coordinates x release when;
if (upX - downX> 100)
{
/ / Drag left and right, before that see a
/ / If this is the first, then go to the tail
if (gallery.getSelectedItemPosition () == 0)
selectedTag = gallery.getCount () - 1;
else
selectedTag = gallery.getSelectedItemPosition () - 1;

imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_right_out));
}
else if (downX - upX> 100)
{
/ / Right onto the left, that look after a
/ / If this is the end, then go to the first
if (gallery.getSelectedItemPosition ()
== (Gallery.getCount () - 1))
selectedTag = 0;
else
selectedTag = gallery.getSelectedItemPosition () + 1;
imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_in));
imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,
R.anim.push_left_out));
}
else
{
if (gallery.getVisibility () == 4)
{
gallery.setVisibility (0);
}
else
{
gallery.setVisibility (4);
}
}

/ / Change the gallery pictures selected automatically trigger ImageSwitcher of setOnItemSelectedListener
gallery.setSelection (selectedTag, true);
return true;
}



Reply:
referenced 8th Floor reply:
 
if (gallery.getVisibility () == 4)
{
gallery.setVisibility (0);
}
else
{
gallery.setVisibility (4);
}

Thank you for your dedication ah! The method works, just go to work to solve a problem, huh.
Reply:
Being test
Reply:
references, 4th Floor lyb2518 reply:
[Quote = reference to the third floor of the replies:]
if (upX - downX> 100)
These things you do not bracketed ah
if ((upX - downX)> 100)


- The symbol of high priority or> higher priority.
Say you answer the question I asked with no association. Oh!

No comments:

Post a Comment