0

My activity displays images and i want to display two buttons when the user touch the screen, and to disappear these button on next touch.

My activity file ImageViewPager.java is as follows:

package com.pankajvatsa.testfeet;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
// Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);
    mainLay.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                bWallpaperButton.setVisibility(View.VISIBLE); 
                bDownloadButton.setVisibility(View.VISIBLE);
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                bWallpaperButton.setVisibility(View.INVISIBLE); 
                bDownloadButton.setVisibility(View.INVISIBLE);
                return true;
            }
            return true;
        }
    });

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);
}

}

And my xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >

<android.support.v4.view.ViewPager
    android:id="@+id/image_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >
</android.support.v4.view.ViewPager>

<Button
    android:id="@+id/bSetWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/set_wallpaper"
    android:visibility="gone" />

<Button
    android:id="@+id/bSaveToGallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/save_local"
    android:visibility="gone" />
</RelativeLayout>
4

2 回答 2

5

这是一个使用 LinearLayout 的示例,但也可以与 RelativeLayout 一起使用:

  Button btn1;
    boolean gone = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);

        LinearLayout ln = (LinearLayout)findViewById(R.id.LinearLayout01);

        ln.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    if(!gone){
                        btn1.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        btn1.setVisibility(View.VISIBLE);
                        gone = false;

                    }


                }
                return true;
            }
        });

    }
于 2013-07-04T16:23:35.310 回答
1

好吧,它以某种方式从布局中删除 setOnTouchListener() 并将其添加到 ViewPager 上。我更新的代码(以及用于区分点击和滑动的其他更改)是:

package com.pankajvatsa.testfeet;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
    // Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
boolean gone = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);

    viewpager.setOnTouchListener(new View.OnTouchListener() {


        private float pointX;
        private float pointY;
        private int tolerance = 50;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE: 
                return false;
            case MotionEvent.ACTION_DOWN:
                pointX = event.getX();
                pointY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                if(sameX && sameY){
                        //The user "clicked" certain point in the screen or just returned to the same position an raised the finger

                    if(gone == false){
                        bWallpaperButton.setVisibility(View.GONE);
                        bDownloadButton.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        bWallpaperButton.setVisibility(View.VISIBLE);
                        bDownloadButton.setVisibility(View.VISIBLE);
                        gone = false;
                    }
                }
            }
            return false;

        }
    });


}

}
于 2013-07-05T09:23:38.630 回答